OfflineTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Rackspace
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Service/Rackspace/Files.php';
  22. require_once 'Zend/Http/Client/Adapter/Test.php';
  23. /**
  24. * Test helper
  25. */
  26. /**
  27. * @category Zend
  28. * @package Zend\Service\Rackspace\Files
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend\Service
  33. * @group Zend\Service\Rackspace
  34. * @group Zend\Service\Rackspace\Files
  35. */
  36. class Zend_Service_Rackspace_OfflineTest extends PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * Reference to RackspaceFiles
  40. *
  41. * @var Zend_Service_Rackspace_Files
  42. */
  43. protected $_files;
  44. /**
  45. * HTTP client adapter for testing
  46. *
  47. * @var Zend_Http_Client_Adapter_Test
  48. */
  49. protected $_httpClientAdapterTest;
  50. /**
  51. * Path to test data files
  52. *
  53. * @var string
  54. */
  55. protected $_filesPath;
  56. /**
  57. * Sets up this test case
  58. *
  59. * @return void
  60. */
  61. public function setUp()
  62. {
  63. $this->_files = new Zend_Service_Rackspace_Files('foo','bar');
  64. $this->_filesPath = dirname(__FILE__) . '/_files';
  65. $this->_httpClientAdapterTest = new Zend_Http_Client_Adapter_Test();
  66. }
  67. /**
  68. * Utility method for returning a string HTTP response, which is loaded from a file
  69. *
  70. * @param string $name
  71. * @return string
  72. */
  73. protected function _loadResponse($name)
  74. {
  75. return file_get_contents("$this->_filesPath/$name.response");
  76. }
  77. /**
  78. * Ensures that __construct() throws an exception when given an empty key attribute
  79. *
  80. * @return void
  81. */
  82. public function testConstructExceptionMissingUserAttribute()
  83. {
  84. $this->setExpectedException(
  85. 'Zend_Service_Rackspace_Exception',
  86. 'The user cannot be empty'
  87. );
  88. $file= new Zend_Service_Rackspace_Files(null,'bar');
  89. }
  90. /**
  91. * Ensures that __construct() throws an exception when given an empty secret attribute
  92. *
  93. * @return void
  94. */
  95. public function testConstructExceptionMissingKeyAttribute()
  96. {
  97. $this->setExpectedException(
  98. 'Zend_Service_Rackspace_Exception',
  99. 'The key cannot be empty'
  100. );
  101. $file= new Zend_Service_Rackspace_Files('foo',null);
  102. }
  103. /**
  104. * Test the default authentication URL
  105. *
  106. * @return void
  107. */
  108. public function testDefaultAuthUrl()
  109. {
  110. $this->assertEquals($this->_files->getAuthUrl(),Zend_Service_Rackspace_Files::US_AUTH_URL,'The default Authentication URL is changed');
  111. }
  112. /**
  113. * Test the set of the key
  114. *
  115. * @return void
  116. */
  117. public function testSetKey()
  118. {
  119. $key= '1234567890';
  120. $this->_files->setKey($key);
  121. $this->assertEquals($this->_files->getKey(),$key);
  122. }
  123. /**
  124. * Test the set of the user
  125. *
  126. * @return void
  127. */
  128. public function testSetUser()
  129. {
  130. $user= 'test';
  131. $this->_files->setUser($user);
  132. $this->assertEquals($this->_files->getUser(),$user);
  133. }
  134. /**
  135. * Test the set of an invalid authentication URL
  136. *
  137. * @return void
  138. */
  139. public function testSetInvalidAuthUrl()
  140. {
  141. $this->setExpectedException(
  142. 'Zend_Service_Rackspace_Exception',
  143. 'The authentication URL is not valid'
  144. );
  145. $this->_files->setAuthUrl('http://test');
  146. }
  147. /**
  148. * Check the authentication and the results (token, storage_url, cdn_url)
  149. *
  150. * @return void
  151. */
  152. public function testAuthenticate()
  153. {
  154. $this->_files->getHttpClient()
  155. ->setAdapter($this->_httpClientAdapterTest);
  156. $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__));
  157. $this->assertTrue($this->_files->authenticate(),'Authentication failed');
  158. $this->assertTrue($this->_files->isSuccessful(),'Authentication call failed');
  159. $this->assertEquals($this->_files->getToken(),'0f0223cd-f157-4d04-bb2d-ccda1a5643af','The token is not valid');
  160. $this->assertEquals($this->_files->getStorageUrl(),'https://storage101.ord1.clouddrive.com/v1/test','The storage URL is not valid');
  161. $this->assertEquals($this->_files->getCdnUrl(),'https://cdn2.clouddrive.com/v1/test','The CDN URL is not valid');
  162. }
  163. /**
  164. * Test the authentication error (401 Unauthorized - Bad username or password)
  165. *
  166. * @return void
  167. */
  168. public function testAuthenticateError()
  169. {
  170. $this->_files->getHttpClient()
  171. ->setAdapter($this->_httpClientAdapterTest);
  172. $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__));
  173. $this->assertFalse($this->_files->authenticate());
  174. $this->assertFalse($this->_files->isSuccessful());
  175. $this->assertEquals($this->_files->getErrorCode(),'401');
  176. $this->assertEquals($this->_files->getErrorMsg(),'Bad username or password');
  177. }
  178. }