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/lib/classes | |
| parent | 88a5f717f90e0e2c31599d159144559ae5be795e (diff) | |
fixes #237
Diffstat (limited to 'tests/unit/lib/classes')
| -rw-r--r-- | tests/unit/lib/classes/StudipCachedArrayTest.php | 216 |
1 files changed, 8 insertions, 208 deletions
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; } |
