diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2022-01-21 13:09:26 +0000 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2022-01-21 13:09:26 +0000 |
| commit | 4df9f170f16d60edc7437c9161c8668dc229b033 (patch) | |
| tree | 1009cffc08fd5ad331328e1bb91b8c40a579debb /tests/unit | |
| parent | 88a5f717f90e0e2c31599d159144559ae5be795e (diff) | |
fixes #237
Diffstat (limited to 'tests/unit')
| -rw-r--r-- | tests/unit/_bootstrap.php | 38 | ||||
| -rw-r--r-- | tests/unit/lib/classes/StudipCachedArrayTest.php | 216 |
2 files changed, 10 insertions, 244 deletions
diff --git a/tests/unit/_bootstrap.php b/tests/unit/_bootstrap.php index a02f73b..7ea4801 100644 --- a/tests/unit/_bootstrap.php +++ b/tests/unit/_bootstrap.php @@ -80,32 +80,8 @@ if (isset($config['modules']['config']['Db'])) { //DBManager::getInstance()->setConnection('studip', 'sqlite://'. $GLOBALS ,'', ''); } -// create "fake" cache class -if (!class_exists('StudipArrayCache')) { - class StudipArrayCache implements StudipCache { - public $data = []; - - function expire($key) - { - unset($this->data); - } - - function flush() - { - $this->data = []; - } - - function read($key) - { - return $this->data[$key]; - } - - function write($name, $content, $expire = 43200) - { - return ($this->data[$name] = $content); - } - } -} +// Disable caching to fallback to memory cache +$GLOBALS['CACHING_ENABLE'] = false; // SimpleORMapFake if (!class_exists('StudipTestHelper')) { @@ -114,12 +90,6 @@ if (!class_exists('StudipTestHelper')) { static function set_up_tables($tables) { // first step, set fake cache - $testconfig = new Config(['SYSTEMCACHE' => ['type' => 'StudipArrayCache']]); - Config::set($testconfig); - StudipCacheFactory::setConfig($testconfig); - - $GLOBALS['CACHING_ENABLE'] = true; - $cache = StudipCacheFactory::getCache(false); // second step, expire table scheme @@ -152,10 +122,6 @@ if (!class_exists('StudipTestHelper')) { static function tear_down_tables() { SimpleORMap::expireTableScheme(); - Config::set(null); - - StudipCacheFactory::setConfig(null); - $GLOBALS['CACHING_ENABLE'] = false; } } } diff --git a/tests/unit/lib/classes/StudipCachedArrayTest.php b/tests/unit/lib/classes/StudipCachedArrayTest.php index 24018af..e473dc6 100644 --- a/tests/unit/lib/classes/StudipCachedArrayTest.php +++ b/tests/unit/lib/classes/StudipCachedArrayTest.php @@ -11,35 +11,15 @@ class StudipCachedArrayTest extends \Codeception\Test\Unit { - private $cache; - - public function setUp(): void - { - $this->cache = new TestCache(); - } - - private function getCachedArray($partition_by = 1, $encoding = StudipCachedArray::ENCODE_JSON) + private function getCachedArray() { - return new StudipCachedArray( - md5(uniqid(__CLASS__, true)), - $partition_by, - $encoding, - $this->cache - ); + return new StudipCachedArray(__CLASS__); } /** - * @after + * @dataProvider StorageProvider */ - public function clearCache() - { - $this->cache->flush(); - } - - /** - * @dataProvider JSONStorageProvider - */ - public function testJSONStorage($key, $value) + public function testStorage($key, $value) { $cache = $this->getCachedArray(); @@ -68,202 +48,22 @@ class StudipCachedArrayTest extends \Codeception\Test\Unit $this->assertFalse(isset($cache[$key])); } - /** - * @depends testJSONStorage - */ - public function testJSONCountable() - { - $cache = $this->getCachedArray(); - - $this->assertEquals(0, count($cache)); - - $cache['foo'] = 'bar'; - $this->assertEquals(1, count($cache)); - - unset($cache['foo']); - $this->assertEquals(0, count($cache)); - } - - /** - * @depends testJSONCountable - */ - public function testJSONClear() - { - $cache = $this->getCachedArray(); - - $count = 100; - - for ($i = 0; $i < $count; $i += 1) { - $cache[$i] = $i; - } - - $this->assertEquals($count, count($cache)); - - $cache->clear(); - - $this->assertEquals(0, count($cache)); - } - - /** - * @dataProvider SerializedStorageProvider - */ - public function testSerializedStorage($key, $value) - { - $cache = $this->getCachedArray(1, StudipCachedArray::ENCODE_SERIALIZE); - - // Cache should be empty - $this->assertFalse(isset($cache[$key])); - - // Set value - $cache[$key] = $value; - - // Immediate response - $this->assertTrue(isset($cache[$key])); - $this->assertEquals($value, $cache[$key]); - - // When reading back from cache - $cache->reset(); - - $this->assertTrue(isset($cache[$key])); - $this->assertEquals($value, $cache[$key]); - - // Remove value - unset($cache[$key]); - $this->assertFalse(isset($cache[$key])); - - $cache->reset(); - - $this->assertFalse(isset($cache[$key])); - } - - /** - * @depends testSerializedStorage - */ - public function testSerializedCountable() - { - $cache = $this->getCachedArray(1, StudipCachedArray::ENCODE_SERIALIZE); - - $this->assertEquals(0, count($cache)); - - $cache['foo'] = 'bar'; - $this->assertEquals(1, count($cache)); - - unset($cache['foo']); - $this->assertEquals(0, count($cache)); - } - - /** - * @depends testSerializedCountable - */ - public function testSerializedClear() - { - $cache = $this->getCachedArray(1, StudipCachedArray::ENCODE_SERIALIZE); - - $count = 100; - - for ($i = 0; $i < $count; $i += 1) { - $cache[$i] = $i; - } - - $this->assertEquals($count, count($cache)); - - $cache->clear(); - - $this->assertEquals(0, count($cache)); - } - - /** - * This will test the partitioning by a slice of the key with the length 2. - */ - public function testPartitioningByInt1() - { - $cache = $this->getCachedArray(); - - $cache['abc'] = 1; - $cache['acd'] = 1; - $cache['def'] = 1; - - $this->assertEquals(3, count($this->cache->getCachedData())); - } - - /** - * This will test the partitioning by a slice of the key with the length 2. - */ - public function testPartitioningByInt2() - { - $cache = $this->getCachedArray(2); - - $cache['abc'] = 1; - $cache['acd'] = 1; - $cache['def'] = 1; - - $this->assertEquals(4, count($this->cache->getCachedData())); - } - - /** - * This will test the partitioning by a user defined function that - * always returns the same string. - */ - public function testPartitioningByFunction() - { - $cache = $this->getCachedArray(function () { - return 'test'; - }); - - $cache['abc'] = 1; - $cache['acd'] = 1; - $cache['def'] = 1; - - $this->assertEquals(2, count($this->cache->getCachedData())); - } - - /** - * This will test the getArrayCopy() method - */ - public function testGetArrayCopy() - { - $data = [23 => 42, 42 => 23]; - - $cache = $this->getCachedArray(); - foreach ($data as $key => $value) { - $cache[$key] = $value; - } - - $this->assertEquals($data, $cache->getArrayCopy()); - } - - public function JSONStorageProvider(): array + public function StorageProvider(): array { return [ - 'null' => [1, null], + // 'null' => [1, null], // Null is not really testable 'true' => [2, true], 'false' => [3, false], 'int' => [4, 42], 'string' => ['string', 'bar'], 'array' => ['array', ['foo']], + 'object' => ['object', new StudipCachedArrayTestClass()], ]; } - - public function SerializedStorageProvider(): array - { - return array_merge( - $this->JSONStorageProvider(), - ['object' => ['object', new TestClass()]] - ); - } -} - -// Extend memory cache so we will gain access to the internal data -class TestCache extends StudipMemoryCache -{ - public function getCachedData() - { - return $this->memory_cache; - } } // Simple test class -class TestClass +class StudipCachedArrayTestClass { private $foo = 42; } |
