PageRenderTime 84ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/phpunit/ProductAPICachingUnitTest.php

https://github.com/octolab/Ecwid
PHP | 305 lines | 252 code | 10 blank | 43 comment | 22 complexity | fcf1f03e010f6a82815dd32dc372ef4a MD5 | raw file
  1. <?php
  2. /**
  3. * @author Samigullin Kamil <feedback@kamilsk.com>
  4. * @link http://www.kamilsk.com/
  5. */
  6. namespace Ecwid\tests\phpunit;
  7. use Ecwid\api\ProductAPI,
  8. Ecwid\api\BatchBuilder,
  9. Ecwid\decorators\ProductAPICaching,
  10. \Exception;
  11. require 'UnitTest.php';
  12. /**
  13. * @package Ecwid.tests.phpunit
  14. * @since 1.0
  15. */
  16. class ProductAPICachingUnitTest extends UnitTest
  17. {
  18. /**
  19. * @param string $task
  20. * @param bool $fully
  21. * @return bool|string
  22. */
  23. public function environment($task = 'install', $fully = false)
  24. {
  25. $cache_path = __DIR__ . '/cache';
  26. switch ($task) {
  27. case 'install':
  28. if ( ! file_exists($cache_path)) {
  29. mkdir($cache_path);
  30. }
  31. return $cache_path;
  32. case 'uninstall':
  33. $folders = array_diff(scandir($cache_path), array('.', '..'));
  34. foreach ($folders as $folder) {
  35. $folder = "{$cache_path}/{$folder}";
  36. $files = array_diff(scandir($folder), array('.', '..'));
  37. foreach ($files as $file) {
  38. unlink("{$folder}/{$file}");
  39. }
  40. rmdir($folder);
  41. }
  42. if ($fully) {
  43. rmdir($cache_path);
  44. }
  45. return true;
  46. }
  47. return false;
  48. }
  49. /**
  50. * Test setters/getters.
  51. */
  52. public function testAccess()
  53. {
  54. $cache = new ProductAPICaching(new ProductAPI());
  55. try {
  56. $cache->setCachePath(uniqid());
  57. var_dump($cache->getCachePath());
  58. } catch (Exception $e) {
  59. try {
  60. $cache->setLifetime(-10000);
  61. var_dump($cache->getLifetime());
  62. } catch (Exception $e) {
  63. return;
  64. }
  65. $this->fail("Exception for ProductAPICaching::setLifetime() wasn't triggered.");
  66. }
  67. $this->fail("Exception for ProductAPICaching::setCachePath() wasn't triggered.");
  68. }
  69. /**
  70. * Test categories fetching.
  71. */
  72. public function testGetCategories()
  73. {
  74. $cache = new ProductAPICaching(new ProductAPI());
  75. $cache->setCachePath($this->environment())->setLifetime(3)->getCategories();
  76. $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
  77. $this->assertNotEmpty($folders);
  78. $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
  79. $folder = "{$cache->getCachePath()}/{$folder}";
  80. $files = array_diff(scandir($folder), array('..', '.'));
  81. $this->assertNotEmpty($files);
  82. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  83. $file = "{$folder}/{$file}";
  84. $content = @file_get_contents($file);
  85. $this->assertNotEmpty($content);
  86. sleep(2);
  87. $cache->getCategories();
  88. $this->assertEquals($content, @file_get_contents($file));
  89. sleep(2);
  90. $cache->getCategories();
  91. $this->assertNotEquals($content, @file_get_contents($file));
  92. $this->environment('uninstall');
  93. }
  94. /**
  95. * Test category fetching.
  96. */
  97. public function testGetCategory()
  98. {
  99. $cache = new ProductAPICaching(new ProductAPI());
  100. $cache->setCachePath($this->environment())->setLifetime(3)->getCategory(1002);
  101. $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
  102. $this->assertNotEmpty($folders);
  103. $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
  104. $folder = "{$cache->getCachePath()}/{$folder}";
  105. $files = array_diff(scandir($folder), array('..', '.'));
  106. $this->assertNotEmpty($files);
  107. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  108. $file = "{$folder}/{$file}";
  109. $content = @file_get_contents($file);
  110. $this->assertNotEmpty($content);
  111. sleep(2);
  112. $cache->getCategory(1002);
  113. $this->assertEquals($content, @file_get_contents($file));
  114. sleep(2);
  115. $cache->getCategory(1002);
  116. $this->assertNotEquals($content, @file_get_contents($file));
  117. $this->environment('uninstall');
  118. }
  119. /**
  120. * Test products fetching.
  121. */
  122. public function testGetProducts()
  123. {
  124. $cache = new ProductAPICaching(new ProductAPI());
  125. $cache->setCachePath($this->environment())->setLifetime(3)->getProducts();
  126. $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
  127. $this->assertNotEmpty($folders);
  128. $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
  129. $folder = "{$cache->getCachePath()}/{$folder}";
  130. $files = array_diff(scandir($folder), array('..', '.'));
  131. $this->assertNotEmpty($files);
  132. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  133. $file = "{$folder}/{$file}";
  134. $content = @file_get_contents($file);
  135. $this->assertNotEmpty($content);
  136. sleep(2);
  137. $cache->getProducts();
  138. $this->assertEquals($content, @file_get_contents($file));
  139. sleep(2);
  140. $cache->getProducts();
  141. $this->assertNotEquals($content, @file_get_contents($file));
  142. $this->environment('uninstall');
  143. }
  144. /**
  145. * Test product fetching.
  146. */
  147. public function testGetProduct()
  148. {
  149. $cache = new ProductAPICaching(new ProductAPI());
  150. $cache->setCachePath($this->environment())->setLifetime(3)->getProduct(4006);
  151. $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
  152. $this->assertNotEmpty($folders);
  153. $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
  154. $folder = "{$cache->getCachePath()}/{$folder}";
  155. $files = array_diff(scandir($folder), array('..', '.'));
  156. $this->assertNotEmpty($files);
  157. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  158. $file = "{$folder}/{$file}";
  159. $content = @file_get_contents($file);
  160. $this->assertNotEmpty($content);
  161. sleep(2);
  162. $cache->getProduct(4006);
  163. $this->assertEquals($content, @file_get_contents($file));
  164. sleep(2);
  165. $cache->getProduct(4006);
  166. $this->assertNotEquals($content, @file_get_contents($file));
  167. $this->environment('uninstall');
  168. }
  169. /**
  170. * Test random products fetching.
  171. */
  172. public function testGetRandomProducts()
  173. {
  174. $cache = new ProductAPICaching(new ProductAPI());
  175. $cache->setCachePath($this->environment())->setLifetime(3)->getRandomProducts(5);
  176. $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
  177. $this->assertNotEmpty($folders);
  178. $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
  179. $folder = "{$cache->getCachePath()}/{$folder}";
  180. $files = array_diff(scandir($folder), array('..', '.'));
  181. $this->assertNotEmpty($files);
  182. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  183. $file = "{$folder}/{$file}";
  184. $content = @file_get_contents($file);
  185. $this->assertNotEmpty($content);
  186. sleep(2);
  187. $cache->getRandomProducts(5);
  188. $this->assertEquals($content, @file_get_contents($file));
  189. sleep(2);
  190. $cache->getRandomProducts(5);
  191. $this->assertNotEquals($content, @file_get_contents($file));
  192. $this->environment('uninstall');
  193. }
  194. /**
  195. * Test classes fetching.
  196. */
  197. public function testGetClasses()
  198. {
  199. $cache = new ProductAPICaching(new ProductAPI());
  200. $cache->setCachePath($this->environment())->setLifetime(3)->getClasses();
  201. $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
  202. $this->assertNotEmpty($folders);
  203. $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
  204. $folder = "{$cache->getCachePath()}/{$folder}";
  205. $files = array_diff(scandir($folder), array('..', '.'));
  206. $this->assertNotEmpty($files);
  207. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  208. $file = "{$folder}/{$file}";
  209. $content = @file_get_contents($file);
  210. $this->assertNotEmpty($content);
  211. sleep(2);
  212. $cache->getClasses();
  213. $this->assertEquals($content, @file_get_contents($file));
  214. sleep(2);
  215. $cache->getClasses();
  216. $this->assertNotEquals($content, @file_get_contents($file));
  217. $this->environment('uninstall');
  218. }
  219. /**
  220. * Test class fetching.
  221. */
  222. public function testGetClass()
  223. {
  224. $cache = new ProductAPICaching(new ProductAPI());
  225. $cache->setCachePath($this->environment())->setLifetime(3)->getClass(0);
  226. $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
  227. $this->assertNotEmpty($folders);
  228. $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
  229. $folder = "{$cache->getCachePath()}/{$folder}";
  230. $files = array_diff(scandir($folder), array('..', '.'));
  231. $this->assertNotEmpty($files);
  232. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  233. $file = "{$folder}/{$file}";
  234. $content = @file_get_contents($file);
  235. $this->assertNotEmpty($content);
  236. sleep(2);
  237. $cache->getClass(0);
  238. $this->assertEquals($content, @file_get_contents($file));
  239. sleep(2);
  240. $cache->getClass(0);
  241. $this->assertNotEquals($content, @file_get_contents($file));
  242. $this->environment('uninstall');
  243. }
  244. /**
  245. * Test profile fetching.
  246. */
  247. public function testGetProfile()
  248. {
  249. $cache = new ProductAPICaching(new ProductAPI());
  250. $cache->setCachePath($this->environment())->setLifetime(3)->getProfile();
  251. $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
  252. $this->assertNotEmpty($folders);
  253. $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
  254. $folder = "{$cache->getCachePath()}/{$folder}";
  255. $files = array_diff(scandir($folder), array('..', '.'));
  256. $this->assertNotEmpty($files);
  257. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  258. $file = "{$folder}/{$file}";
  259. $content = @file_get_contents($file);
  260. $this->assertNotEmpty($content);
  261. sleep(2);
  262. $cache->getProfile();
  263. $this->assertEquals($content, @file_get_contents($file));
  264. sleep(2);
  265. $cache->getProfile();
  266. $this->assertNotEquals($content, @file_get_contents($file));
  267. $this->environment('uninstall');
  268. }
  269. /**
  270. * Test run batch.
  271. */
  272. public function testRunBatch()
  273. {
  274. $cache = new ProductAPICaching(new ProductAPI());
  275. $batch = new BatchBuilder($cache->setCachePath($this->environment())->setLifetime(3));
  276. $batch->getCategories('categories')->run();
  277. $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
  278. $this->assertNotEmpty($folders);
  279. $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
  280. $folder = "{$cache->getCachePath()}/{$folder}";
  281. $files = array_diff(scandir($folder), array('..', '.'));
  282. $this->assertNotEmpty($files);
  283. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  284. $file = "{$folder}/{$file}";
  285. $content = @file_get_contents($file);
  286. $this->assertNotEmpty($content);
  287. sleep(2);
  288. $batch->getCategories('categories')->run();
  289. $this->assertEquals($content, @file_get_contents($file));
  290. sleep(2);
  291. $batch->getCategories('categories')->run();
  292. $this->assertNotEquals($content, @file_get_contents($file));
  293. $this->environment('uninstall', true);
  294. }
  295. }