PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Resources/public/ckeditor4/ckeditor/plugins/uploadcare/uploadcare-php/tests/5.2/ApiTest.php

https://bitbucket.org/ZephyrQG2/sf2bundleeditable
PHP | 311 lines | 213 code | 42 blank | 56 comment | 16 complexity | af84bd8c752e3fd5bab2fffbc1ae7e52 MD5 | raw file
  1. <?php
  2. require_once dirname(__FILE__).'/config.php';
  3. require_once dirname(__FILE__).'/../../uploadcare/lib/5.2/Uploadcare.php';
  4. class ApiTest extends PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * Setup test
  8. * @return void
  9. */
  10. public function setUp() {
  11. }
  12. /**
  13. * Tear down
  14. * @return void
  15. */
  16. public function tearDown() {
  17. }
  18. /**
  19. * Test for constants not to be misspelled
  20. */
  21. public function testConstantValid()
  22. {
  23. $this->assertTrue(API_TYPE_RAW == 'raw');
  24. $this->assertTrue(API_TYPE_ACCOUNT == 'account');
  25. $this->assertTrue(API_TYPE_STORE == 'store');
  26. $this->assertTrue(API_TYPE_FILES == 'files');
  27. $this->assertTrue(API_TYPE_FILE == 'file');
  28. $this->assertTrue(REQUEST_TYPE_POST == 'post');
  29. $this->assertTrue(REQUEST_TYPE_PUT == 'put');
  30. $this->assertTrue(REQUEST_TYPE_DELETE == 'delete');
  31. $this->assertTrue(REQUEST_TYPE_GET == 'get');
  32. $this->assertTrue(REQUEST_TYPE_HEAD == 'head');
  33. $this->assertTrue(REQUEST_TYPE_OPTIONS == 'options');
  34. $this->assertTrue(UC_PARAM_FILE_ID == 'file_id');
  35. }
  36. /**
  37. * This is just some simple test to check that classes are right.
  38. */
  39. public function testChildObjectsValid()
  40. {
  41. $api = new Uploadcare_Api(UC_PUBLIC_KEY, UC_SECRET_KEY);
  42. $this->assertTrue(get_class($api->widget) == 'Uploadcare_Widget');
  43. $this->assertTrue(get_class($api->uploader) == 'Uploadcare_Uploader');
  44. }
  45. /**
  46. * Is public key valid?
  47. */
  48. public function testPublicKeyValid()
  49. {
  50. $api = new Uploadcare_Api(UC_PUBLIC_KEY, UC_SECRET_KEY);
  51. $this->assertTrue($api->getPublicKey() == 'demopublickey', 'This is true');
  52. }
  53. /**
  54. * Test that getFilesList mehtod returns array
  55. * and each item of array is an object of Uploadcare_File class
  56. */
  57. public function testFileList()
  58. {
  59. $api = new Uploadcare_Api(UC_PUBLIC_KEY, UC_SECRET_KEY);
  60. $files = $api->getFileList();
  61. $this->assertTrue(is_array($files));
  62. foreach ($files as $file) {
  63. $this->assertTrue(get_class($file) == 'Uploadcare_File');
  64. }
  65. }
  66. /**
  67. * Test requests for exceptions to "raw" url
  68. */
  69. public function testRequestsRaw()
  70. {
  71. $api = new Uploadcare_Api(UC_PUBLIC_KEY, UC_SECRET_KEY);
  72. // this are request to https://api.uploadcare.com/ url.
  73. // no exceptions should be thrown
  74. try {
  75. $result = $api->request('GET', '/');
  76. $api->request('HEAD', '/');
  77. $api->request('OPTIONS', '/');
  78. } catch (Exception $e) {
  79. $this->fail('An unexpected exception thrown');
  80. }
  81. // let's check we have a "resources"
  82. $this->assertTrue(is_array($result->resources));
  83. // this are requests to https://api.uploadcare.com/ url.
  84. // But this requests are now allowed but this url and we must have an exception
  85. try {
  86. $api->request('POST', '/');
  87. $this->fail('We must get an exception but everything worked fine!');
  88. } catch (Exception $e) {
  89. }
  90. try {
  91. $api->request('PUT', '/');
  92. $this->fail('We must get an exception but everything worked fine!');
  93. } catch (Exception $e) {
  94. }
  95. try {
  96. $api->request('DELETE', '/');
  97. $this->fail('We must get an exception but everything worked fine!');
  98. } catch (Exception $e) {
  99. }
  100. }
  101. /**
  102. * Test requests to "account" url
  103. */
  104. public function testRequestsAccount()
  105. {
  106. $api = new Uploadcare_Api(UC_PUBLIC_KEY, UC_SECRET_KEY);
  107. // this are request to https://api.uploadcare.com/account/ url.
  108. // no exceptions should be thrown
  109. try {
  110. $result = $api->request('GET', '/account/');
  111. $api->request('HEAD', '/account/');
  112. $api->request('OPTIONS', '/account/');
  113. } catch (Exception $e) {
  114. $this->fail('An unexpected exception thrown');
  115. }
  116. // we have some data, let's check it
  117. $this->assertEquals($result->username, 'demo');
  118. $this->assertEquals($result->pub_key, 'demopublickey');
  119. $this->assertEquals($result->email, 'demo@uploadcare.com');
  120. // this are requests to https://api.uploadcare.com/account/ url.
  121. // But this requests are now allowed but this url and we must have an exception
  122. try {
  123. $api->request('POST', '/account/');
  124. $this->fail('We must get an exception but everything worked fine!');
  125. } catch (Exception $e) {
  126. }
  127. try {
  128. $api->request('PUT', '/account/');
  129. $this->fail('We must get an exception but everything worked fine!');
  130. } catch (Exception $e) {
  131. }
  132. try {
  133. $api->request('DELETE', '/account/');
  134. $this->fail('We must get an exception but everything worked fine!');
  135. } catch (Exception $e) {
  136. }
  137. }
  138. /**
  139. * Test request to "files"
  140. */
  141. public function testRequestsFiles()
  142. {
  143. $api = new Uploadcare_Api(UC_PUBLIC_KEY, UC_SECRET_KEY);
  144. // this are request to https://api.uploadcare.com/files/ url.
  145. // no exceptions should be thrown
  146. try {
  147. $result = $api->request('GET', '/files/');
  148. $api->request('HEAD', '/files/');
  149. $api->request('OPTIONS', '/files/');
  150. } catch (Exception $e) {
  151. $this->fail('An unexpected exception thrown');
  152. }
  153. // let's check we have an array of raw file data
  154. $this->assertTrue(is_array($result->results));
  155. $this->assertGreaterThan(0, count($result->results));
  156. $file_raw = (array)$result->results[0];
  157. $this->assertArrayHasKey('size', $file_raw);
  158. $this->assertArrayHasKey('upload_date', $file_raw);
  159. $this->assertArrayHasKey('is_image', $file_raw);
  160. $this->assertArrayHasKey('file_id', $file_raw);
  161. $this->assertArrayHasKey('original_filename', $file_raw);
  162. $this->assertArrayHasKey('mime_type', $file_raw);
  163. // this are requests to https://api.uploadcare.com/files/ url.
  164. // But this requests are now allowed but this url and we must have an exception
  165. try {
  166. $api->request('POST', '/files/');
  167. $this->fail('We must get an exception but everything worked fine!');
  168. } catch (Exception $e) {
  169. }
  170. try {
  171. $api->request('PUT', '/files/');
  172. $this->fail('We must get an exception but everything worked fine!');
  173. } catch (Exception $e) {
  174. }
  175. try {
  176. $api->request('DELETE', '/files/');
  177. $this->fail('We must get an exception but everything worked fine!');
  178. } catch (Exception $e) {
  179. }
  180. }
  181. /**
  182. * Let's check the file operations and check for correct urls
  183. */
  184. public function testFile()
  185. {
  186. $api = new Uploadcare_Api(UC_PUBLIC_KEY, UC_SECRET_KEY);
  187. $file = $api->getFile('4bd3a897-f489-4b9f-b643-961b1c9f657e');
  188. $this->assertEquals(get_class($file), 'Uploadcare_File');
  189. $this->assertEquals($file->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/');
  190. $this->assertEquals($file->resize(400, 400)->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/resize/400x400/');
  191. $this->assertEquals($file->resize(400, false)->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/resize/400x/');
  192. $this->assertEquals($file->resize(false, 400)->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/resize/x400/');
  193. $this->assertEquals($file->crop(400, 400)->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/crop/400x400/');
  194. $this->assertEquals($file->crop(400, 400, true)->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/crop/400x400/center/');
  195. $this->assertEquals($file->crop(400, 400, true, 'ff0000')->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/crop/400x400/center/ff0000/');
  196. $this->assertEquals($file->crop(400, 400, false, 'ff0000')->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/crop/400x400/ff0000/');
  197. $this->assertEquals($file->scaleCrop(400, 400)->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/scale_crop/400x400/');
  198. $this->assertEquals($file->scaleCrop(400, 400, true)->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/scale_crop/400x400/center/');
  199. $this->assertEquals($file->effect('flip')->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/effect/flip/');
  200. $this->assertEquals($file->effect('grayscale')->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/effect/grayscale/');
  201. $this->assertEquals($file->effect('invert')->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/effect/invert/');
  202. $this->assertEquals($file->effect('mirror')->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/effect/mirror/');
  203. $this->assertEquals($file->effect('flip')->effect('mirror')->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/effect/flip/-/effect/mirror/');
  204. $this->assertEquals($file->effect('mirror')->effect('flip')->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/effect/mirror/-/effect/flip/');
  205. $this->assertEquals($file->resize(400, 400)->scaleCrop(200, 200, true)->effect('mirror')->getUrl(), 'https://ucarecdn.com/4bd3a897-f489-4b9f-b643-961b1c9f657e/-/resize/400x400/-/scale_crop/200x200/center/-/effect/mirror/');
  206. }
  207. /**
  208. * Test uploading and deleting
  209. */
  210. public function testUploadAndDelete()
  211. {
  212. $api = new Uploadcare_Api(UC_PUBLIC_KEY, UC_SECRET_KEY);
  213. // upload form url
  214. try {
  215. $file = $api->uploader->fromUrl('http://www.baysflowers.co.nz/Images/tangerine-delight.jpg');
  216. } catch (Exception $e) {
  217. $this->fail('We get an unexpected exception trying to upload from url '.$e->getMessage());
  218. }
  219. $this->assertEquals(get_class($file), 'Uploadcare_File');
  220. try {
  221. $file->store();
  222. } catch (Exception $e) {
  223. $this->fail('We get an unexpected exception trying to store uploaded file from url'.$e->getMessage());
  224. }
  225. // upload from path
  226. try {
  227. $file = $api->uploader->fromPath(dirname(__FILE__).'/test.jpg');
  228. } catch (Exception $e) {
  229. $this->fail('We get an unexpected exception trying to upload from path');
  230. }
  231. try {
  232. $file->store();
  233. } catch (Exception $e) {
  234. $this->fail('We get an unexpected exception trying to store uploaded file from path'.$e->getMessage());
  235. }
  236. // upload from resource
  237. try {
  238. $fp = fopen(dirname(__FILE__).'/test.jpg', 'r');
  239. $file = $api->uploader->fromResource($fp);
  240. } catch (Exception $e) {
  241. $this->fail('We get an unexpected exception trying to upload from resource'.$e->getMessage());
  242. }
  243. try {
  244. $file->store();
  245. } catch (Exception $e) {
  246. $this->fail('We get an unexpected exception trying to store uploaded file from resource'.$e->getMessage());
  247. }
  248. // upload from raw
  249. try {
  250. $content = "This is some text I want to upload";
  251. $file = $api->uploader->fromContent($content, 'text/plain');
  252. } catch (Exception $e) {
  253. $this->fail('We get an unexpected exception trying to upload from contents'.$e->getMessage());
  254. }
  255. try {
  256. $file->store();
  257. } catch (Exception $e) {
  258. $this->fail('We get an unexpected exception trying to store uploaded file from contents'.$e->getMessage());
  259. }
  260. $text = file_get_contents($file->getUrl());
  261. $this->assertEquals($text, "This is some text I want to upload");
  262. // test file delete
  263. try {
  264. $file->delete();
  265. } catch (Exception $e) {
  266. $this->fail('We get an unexpected exception trying to delete file'.$e->getMessage());
  267. }
  268. }
  269. }