/tests/phpunit/ProductAPILoggerUnitTest.php
PHP | 250 lines | 182 code | 13 blank | 55 comment | 16 complexity | 801c87f1bf647ec1a18585b8b19f1070 MD5 | raw file
- <?php
- /**
- * @author Samigullin Kamil <feedback@kamilsk.com>
- * @link http://www.kamilsk.com/
- */
- namespace Ecwid\tests\phpunit;
- use Ecwid\api\ProductAPI,
- Ecwid\api\BatchBuilder,
- Ecwid\decorators\ProductAPILogger,
- \Exception;
- require 'UnitTest.php';
- /**
- * @package Ecwid.tests.phpunit
- * @since 1.0
- */
- class ProductAPILoggerUnitTest extends UnitTest
- {
- /**
- * @return array
- */
- public function levels()
- {
- return array(
- array('debug,error'),
- array('debug, error'),
- array('debug ,error'),
- array('debug , error'),
- array('debug, error'),
- array('debug ,error'),
- array('debug , error'),
- array('debug|error'),
- array('debug:error'),
- );
- }
- /**
- * @param string $task
- * @param bool $fully
- * @return bool|string
- */
- public function environment($task = 'install', $fully = false)
- {
- $log_path = __DIR__ . '/logs';
- switch ($task) {
- case 'install':
- if ( ! file_exists($log_path)) {
- mkdir($log_path);
- }
- return $log_path;
- case 'uninstall':
- $files = array_diff(scandir($log_path), array('.', '..'));
- foreach ($files as $file) {
- unlink("{$log_path}/{$file}");
- }
- if ($fully) {
- rmdir($log_path);
- }
- return true;
- }
- return false;
- }
- /**
- * Test levels splitter.
- *
- * @dataProvider levels
- * @param string $levels
- */
- public function testLevelsSplitter($levels)
- {
- $standard = array('debug', 'error');
- $logger = new ProductAPILogger(new ProductAPI());
- $this->assertEmpty(array_diff($standard, $logger->setLevels($levels)->getLevels()));
- $this->assertEmpty(array_diff($logger->getLevels(), $standard));
- }
- /**
- * Test setters/getters.
- */
- public function testAccess()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- try {
- $logger->setLogPath(uniqid());
- var_dump($logger->getLogPath());
- } catch (Exception $e) {
- try {
- $logger->setLevels('debug,info,other');
- var_dump($logger->getLevels());
- } catch (Exception $e) {
- return;
- }
- $this->fail("Exception for ProductAPILogger::setLevels() wasn't triggered.");
- }
- $this->fail("Exception for ProductAPILogger::setLogPath() wasn't triggered.");
- }
- /**
- * Test categories fetching.
- */
- public function testGetCategories()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- $logger->setLogPath($this->environment())->setLevels('debug,error')->getCategories();
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall');
- }
- /**
- * Test category fetching.
- */
- public function testGetCategory()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- $logger->setLogPath($this->environment())->setLevels('debug,error')->getCategory(1002);
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall');
- }
- /**
- * Test products fetching.
- */
- public function testGetProducts()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- $logger->setLogPath($this->environment())->setLevels('debug,error')->getProducts();
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall');
- }
- /**
- * Test product fetching.
- */
- public function testGetProduct()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- $logger->setLogPath($this->environment())->setLevels('debug,error')->getProduct(4006);
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall');
- }
- /**
- * Test random products fetching.
- */
- public function testGetRandomProducts()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- $logger->setLogPath($this->environment())->setLevels('debug,error')->getRandomProducts(5);
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall');
- }
- /**
- * Test classes fetching.
- */
- public function testGetClasses()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- $logger->setLogPath($this->environment())->setLevels('debug,error')->getClasses();
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall');
- }
- /**
- * Test class fetching.
- */
- public function testGetClass()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- $logger->setLogPath($this->environment())->setLevels('debug,error')->getClass(0);
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall');
- }
- /**
- * Test profile fetching.
- */
- public function testGetProfile()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- $logger->setLogPath($this->environment())->setLevels('debug,error')->getProfile();
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall');
- }
- /**
- * Test run batch.
- */
- public function testRunBatch()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- $batch = new BatchBuilder($logger->setLogPath($this->environment())->setLevels('debug,error'));
- $batch->getCategories('categories')->run();
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall');
- }
- /**
- * Test work on levels.
- */
- public function testLevels()
- {
- $logger = new ProductAPILogger(new ProductAPI());
- $logger->setLogPath($this->environment())->setLevels('error')->getClass(0);
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall');
- try {
- $response = $logger->setLevels('error')->getClass(-1);
- var_dump($response);
- } catch (Exception $e) {
- $files = array_diff(scandir($logger->getLogPath()), array('.', '..'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $this->assertNotEmpty(file_get_contents("{$logger->getLogPath()}/{$file}"));
- $this->environment('uninstall', true);
- return;
- }
- $this->fail("Exception for ProductAPILogger::getClass() wasn't triggered.");
- }
- }