PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/phpunit/ProductAPILoggerUnitTest.php

https://github.com/octolab/Ecwid
PHP | 250 lines | 182 code | 13 blank | 55 comment | 16 complexity | 801c87f1bf647ec1a18585b8b19f1070 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\ProductAPILogger,
  10. \Exception;
  11. require 'UnitTest.php';
  12. /**
  13. * @package Ecwid.tests.phpunit
  14. * @since 1.0
  15. */
  16. class ProductAPILoggerUnitTest extends UnitTest
  17. {
  18. /**
  19. * @return array
  20. */
  21. public function levels()
  22. {
  23. return array(
  24. array('debug,error'),
  25. array('debug, error'),
  26. array('debug ,error'),
  27. array('debug , error'),
  28. array('debug, error'),
  29. array('debug ,error'),
  30. array('debug , error'),
  31. array('debug|error'),
  32. array('debug:error'),
  33. );
  34. }
  35. /**
  36. * @param string $task
  37. * @param bool $fully
  38. * @return bool|string
  39. */
  40. public function environment($task = 'install', $fully = false)
  41. {
  42. $log_path = __DIR__ . '/logs';
  43. switch ($task) {
  44. case 'install':
  45. if ( ! file_exists($log_path)) {
  46. mkdir($log_path);
  47. }
  48. return $log_path;
  49. case 'uninstall':
  50. $files = array_diff(scandir($log_path), array('.', '..'));
  51. foreach ($files as $file) {
  52. unlink("{$log_path}/{$file}");
  53. }
  54. if ($fully) {
  55. rmdir($log_path);
  56. }
  57. return true;
  58. }
  59. return false;
  60. }
  61. /**
  62. * Test levels splitter.
  63. *
  64. * @dataProvider levels
  65. * @param string $levels
  66. */
  67. public function testLevelsSplitter($levels)
  68. {
  69. $standard = array('debug', 'error');
  70. $logger = new ProductAPILogger(new ProductAPI());
  71. $this->assertEmpty(array_diff($standard, $logger->setLevels($levels)->getLevels()));
  72. $this->assertEmpty(array_diff($logger->getLevels(), $standard));
  73. }
  74. /**
  75. * Test setters/getters.
  76. */
  77. public function testAccess()
  78. {
  79. $logger = new ProductAPILogger(new ProductAPI());
  80. try {
  81. $logger->setLogPath(uniqid());
  82. var_dump($logger->getLogPath());
  83. } catch (Exception $e) {
  84. try {
  85. $logger->setLevels('debug,info,other');
  86. var_dump($logger->getLevels());
  87. } catch (Exception $e) {
  88. return;
  89. }
  90. $this->fail("Exception for ProductAPILogger::setLevels() wasn't triggered.");
  91. }
  92. $this->fail("Exception for ProductAPILogger::setLogPath() wasn't triggered.");
  93. }
  94. /**
  95. * Test categories fetching.
  96. */
  97. public function testGetCategories()
  98. {
  99. $logger = new ProductAPILogger(new ProductAPI());
  100. $logger->setLogPath($this->environment())->setLevels('debug,error')->getCategories();
  101. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  102. $this->assertNotEmpty($files);
  103. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  104. $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  105. $this->environment('uninstall');
  106. }
  107. /**
  108. * Test category fetching.
  109. */
  110. public function testGetCategory()
  111. {
  112. $logger = new ProductAPILogger(new ProductAPI());
  113. $logger->setLogPath($this->environment())->setLevels('debug,error')->getCategory(1002);
  114. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  115. $this->assertNotEmpty($files);
  116. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  117. $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  118. $this->environment('uninstall');
  119. }
  120. /**
  121. * Test products fetching.
  122. */
  123. public function testGetProducts()
  124. {
  125. $logger = new ProductAPILogger(new ProductAPI());
  126. $logger->setLogPath($this->environment())->setLevels('debug,error')->getProducts();
  127. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  128. $this->assertNotEmpty($files);
  129. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  130. $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  131. $this->environment('uninstall');
  132. }
  133. /**
  134. * Test product fetching.
  135. */
  136. public function testGetProduct()
  137. {
  138. $logger = new ProductAPILogger(new ProductAPI());
  139. $logger->setLogPath($this->environment())->setLevels('debug,error')->getProduct(4006);
  140. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  141. $this->assertNotEmpty($files);
  142. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  143. $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  144. $this->environment('uninstall');
  145. }
  146. /**
  147. * Test random products fetching.
  148. */
  149. public function testGetRandomProducts()
  150. {
  151. $logger = new ProductAPILogger(new ProductAPI());
  152. $logger->setLogPath($this->environment())->setLevels('debug,error')->getRandomProducts(5);
  153. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  154. $this->assertNotEmpty($files);
  155. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  156. $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  157. $this->environment('uninstall');
  158. }
  159. /**
  160. * Test classes fetching.
  161. */
  162. public function testGetClasses()
  163. {
  164. $logger = new ProductAPILogger(new ProductAPI());
  165. $logger->setLogPath($this->environment())->setLevels('debug,error')->getClasses();
  166. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  167. $this->assertNotEmpty($files);
  168. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  169. $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  170. $this->environment('uninstall');
  171. }
  172. /**
  173. * Test class fetching.
  174. */
  175. public function testGetClass()
  176. {
  177. $logger = new ProductAPILogger(new ProductAPI());
  178. $logger->setLogPath($this->environment())->setLevels('debug,error')->getClass(0);
  179. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  180. $this->assertNotEmpty($files);
  181. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  182. $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  183. $this->environment('uninstall');
  184. }
  185. /**
  186. * Test profile fetching.
  187. */
  188. public function testGetProfile()
  189. {
  190. $logger = new ProductAPILogger(new ProductAPI());
  191. $logger->setLogPath($this->environment())->setLevels('debug,error')->getProfile();
  192. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  193. $this->assertNotEmpty($files);
  194. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  195. $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  196. $this->environment('uninstall');
  197. }
  198. /**
  199. * Test run batch.
  200. */
  201. public function testRunBatch()
  202. {
  203. $logger = new ProductAPILogger(new ProductAPI());
  204. $batch = new BatchBuilder($logger->setLogPath($this->environment())->setLevels('debug,error'));
  205. $batch->getCategories('categories')->run();
  206. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  207. $this->assertNotEmpty($files);
  208. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  209. $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  210. $this->environment('uninstall');
  211. }
  212. /**
  213. * Test work on levels.
  214. */
  215. public function testLevels()
  216. {
  217. $logger = new ProductAPILogger(new ProductAPI());
  218. $logger->setLogPath($this->environment())->setLevels('error')->getClass(0);
  219. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  220. $this->assertNotEmpty($files);
  221. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  222. $this->assertEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  223. $this->environment('uninstall');
  224. try {
  225. $response = $logger->setLevels('error')->getClass(-1);
  226. var_dump($response);
  227. } catch (Exception $e) {
  228. $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
  229. $this->assertNotEmpty($files);
  230. $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
  231. $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
  232. $this->environment('uninstall', true);
  233. return;
  234. }
  235. $this->fail("Exception for ProductAPILogger::getClass() wasn't triggered.");
  236. }
  237. }