/tests/phpunit/ProductAPICachingUnitTest.php
PHP | 305 lines | 252 code | 10 blank | 43 comment | 22 complexity | fcf1f03e010f6a82815dd32dc372ef4a 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\ProductAPICaching,
- \Exception;
- require 'UnitTest.php';
- /**
- * @package Ecwid.tests.phpunit
- * @since 1.0
- */
- class ProductAPICachingUnitTest extends UnitTest
- {
- /**
- * @param string $task
- * @param bool $fully
- * @return bool|string
- */
- public function environment($task = 'install', $fully = false)
- {
- $cache_path = __DIR__ . '/cache';
- switch ($task) {
- case 'install':
- if ( ! file_exists($cache_path)) {
- mkdir($cache_path);
- }
- return $cache_path;
- case 'uninstall':
- $folders = array_diff(scandir($cache_path), array('.', '..'));
- foreach ($folders as $folder) {
- $folder = "{$cache_path}/{$folder}";
- $files = array_diff(scandir($folder), array('.', '..'));
- foreach ($files as $file) {
- unlink("{$folder}/{$file}");
- }
- rmdir($folder);
- }
- if ($fully) {
- rmdir($cache_path);
- }
- return true;
- }
- return false;
- }
- /**
- * Test setters/getters.
- */
- public function testAccess()
- {
- $cache = new ProductAPICaching(new ProductAPI());
- try {
- $cache->setCachePath(uniqid());
- var_dump($cache->getCachePath());
- } catch (Exception $e) {
- try {
- $cache->setLifetime(-10000);
- var_dump($cache->getLifetime());
- } catch (Exception $e) {
- return;
- }
- $this->fail("Exception for ProductAPICaching::setLifetime() wasn't triggered.");
- }
- $this->fail("Exception for ProductAPICaching::setCachePath() wasn't triggered.");
- }
- /**
- * Test categories fetching.
- */
- public function testGetCategories()
- {
- $cache = new ProductAPICaching(new ProductAPI());
- $cache->setCachePath($this->environment())->setLifetime(3)->getCategories();
- $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
- $this->assertNotEmpty($folders);
- $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
- $folder = "{$cache->getCachePath()}/{$folder}";
- $files = array_diff(scandir($folder), array('..', '.'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $file = "{$folder}/{$file}";
- $content = @file_get_contents($file);
- $this->assertNotEmpty($content);
- sleep(2);
- $cache->getCategories();
- $this->assertEquals($content, @file_get_contents($file));
- sleep(2);
- $cache->getCategories();
- $this->assertNotEquals($content, @file_get_contents($file));
- $this->environment('uninstall');
- }
- /**
- * Test category fetching.
- */
- public function testGetCategory()
- {
- $cache = new ProductAPICaching(new ProductAPI());
- $cache->setCachePath($this->environment())->setLifetime(3)->getCategory(1002);
- $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
- $this->assertNotEmpty($folders);
- $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
- $folder = "{$cache->getCachePath()}/{$folder}";
- $files = array_diff(scandir($folder), array('..', '.'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $file = "{$folder}/{$file}";
- $content = @file_get_contents($file);
- $this->assertNotEmpty($content);
- sleep(2);
- $cache->getCategory(1002);
- $this->assertEquals($content, @file_get_contents($file));
- sleep(2);
- $cache->getCategory(1002);
- $this->assertNotEquals($content, @file_get_contents($file));
- $this->environment('uninstall');
- }
- /**
- * Test products fetching.
- */
- public function testGetProducts()
- {
- $cache = new ProductAPICaching(new ProductAPI());
- $cache->setCachePath($this->environment())->setLifetime(3)->getProducts();
- $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
- $this->assertNotEmpty($folders);
- $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
- $folder = "{$cache->getCachePath()}/{$folder}";
- $files = array_diff(scandir($folder), array('..', '.'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $file = "{$folder}/{$file}";
- $content = @file_get_contents($file);
- $this->assertNotEmpty($content);
- sleep(2);
- $cache->getProducts();
- $this->assertEquals($content, @file_get_contents($file));
- sleep(2);
- $cache->getProducts();
- $this->assertNotEquals($content, @file_get_contents($file));
- $this->environment('uninstall');
- }
- /**
- * Test product fetching.
- */
- public function testGetProduct()
- {
- $cache = new ProductAPICaching(new ProductAPI());
- $cache->setCachePath($this->environment())->setLifetime(3)->getProduct(4006);
- $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
- $this->assertNotEmpty($folders);
- $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
- $folder = "{$cache->getCachePath()}/{$folder}";
- $files = array_diff(scandir($folder), array('..', '.'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $file = "{$folder}/{$file}";
- $content = @file_get_contents($file);
- $this->assertNotEmpty($content);
- sleep(2);
- $cache->getProduct(4006);
- $this->assertEquals($content, @file_get_contents($file));
- sleep(2);
- $cache->getProduct(4006);
- $this->assertNotEquals($content, @file_get_contents($file));
- $this->environment('uninstall');
- }
- /**
- * Test random products fetching.
- */
- public function testGetRandomProducts()
- {
- $cache = new ProductAPICaching(new ProductAPI());
- $cache->setCachePath($this->environment())->setLifetime(3)->getRandomProducts(5);
- $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
- $this->assertNotEmpty($folders);
- $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
- $folder = "{$cache->getCachePath()}/{$folder}";
- $files = array_diff(scandir($folder), array('..', '.'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $file = "{$folder}/{$file}";
- $content = @file_get_contents($file);
- $this->assertNotEmpty($content);
- sleep(2);
- $cache->getRandomProducts(5);
- $this->assertEquals($content, @file_get_contents($file));
- sleep(2);
- $cache->getRandomProducts(5);
- $this->assertNotEquals($content, @file_get_contents($file));
- $this->environment('uninstall');
- }
- /**
- * Test classes fetching.
- */
- public function testGetClasses()
- {
- $cache = new ProductAPICaching(new ProductAPI());
- $cache->setCachePath($this->environment())->setLifetime(3)->getClasses();
- $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
- $this->assertNotEmpty($folders);
- $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
- $folder = "{$cache->getCachePath()}/{$folder}";
- $files = array_diff(scandir($folder), array('..', '.'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $file = "{$folder}/{$file}";
- $content = @file_get_contents($file);
- $this->assertNotEmpty($content);
- sleep(2);
- $cache->getClasses();
- $this->assertEquals($content, @file_get_contents($file));
- sleep(2);
- $cache->getClasses();
- $this->assertNotEquals($content, @file_get_contents($file));
- $this->environment('uninstall');
- }
- /**
- * Test class fetching.
- */
- public function testGetClass()
- {
- $cache = new ProductAPICaching(new ProductAPI());
- $cache->setCachePath($this->environment())->setLifetime(3)->getClass(0);
- $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
- $this->assertNotEmpty($folders);
- $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
- $folder = "{$cache->getCachePath()}/{$folder}";
- $files = array_diff(scandir($folder), array('..', '.'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $file = "{$folder}/{$file}";
- $content = @file_get_contents($file);
- $this->assertNotEmpty($content);
- sleep(2);
- $cache->getClass(0);
- $this->assertEquals($content, @file_get_contents($file));
- sleep(2);
- $cache->getClass(0);
- $this->assertNotEquals($content, @file_get_contents($file));
- $this->environment('uninstall');
- }
- /**
- * Test profile fetching.
- */
- public function testGetProfile()
- {
- $cache = new ProductAPICaching(new ProductAPI());
- $cache->setCachePath($this->environment())->setLifetime(3)->getProfile();
- $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
- $this->assertNotEmpty($folders);
- $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
- $folder = "{$cache->getCachePath()}/{$folder}";
- $files = array_diff(scandir($folder), array('..', '.'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $file = "{$folder}/{$file}";
- $content = @file_get_contents($file);
- $this->assertNotEmpty($content);
- sleep(2);
- $cache->getProfile();
- $this->assertEquals($content, @file_get_contents($file));
- sleep(2);
- $cache->getProfile();
- $this->assertNotEquals($content, @file_get_contents($file));
- $this->environment('uninstall');
- }
- /**
- * Test run batch.
- */
- public function testRunBatch()
- {
- $cache = new ProductAPICaching(new ProductAPI());
- $batch = new BatchBuilder($cache->setCachePath($this->environment())->setLifetime(3));
- $batch->getCategories('categories')->run();
- $folders = array_diff(scandir($cache->getCachePath()), array('.', '..'));
- $this->assertNotEmpty($folders);
- $this->assertTrue(false !== ($folder = current($folders)) && count($folders) === 1);
- $folder = "{$cache->getCachePath()}/{$folder}";
- $files = array_diff(scandir($folder), array('..', '.'));
- $this->assertNotEmpty($files);
- $this->assertTrue(false !== ($file = current($files)) && count($files) === 1);
- $file = "{$folder}/{$file}";
- $content = @file_get_contents($file);
- $this->assertNotEmpty($content);
- sleep(2);
- $batch->getCategories('categories')->run();
- $this->assertEquals($content, @file_get_contents($file));
- sleep(2);
- $batch->getCategories('categories')->run();
- $this->assertNotEquals($content, @file_get_contents($file));
- $this->environment('uninstall', true);
- }
- }