From 1f6d957aa11d3032d0cb2620a1575292191f8059 Mon Sep 17 00:00:00 2001 From: Avior Date: Fri, 26 Apr 2019 15:27:28 +0200 Subject: [PATCH] Changed Caches Tests --- tests/Cache/AbstractCacheTest.php | 91 ++++++++++++++++++ tests/Cache/SessionCacheTest.php | 149 ++++++++++++++++++++++++++++++ tests/SessionCacheTest.php | 91 ------------------ 3 files changed, 240 insertions(+), 91 deletions(-) create mode 100644 tests/Cache/AbstractCacheTest.php create mode 100644 tests/Cache/SessionCacheTest.php delete mode 100644 tests/SessionCacheTest.php diff --git a/tests/Cache/AbstractCacheTest.php b/tests/Cache/AbstractCacheTest.php new file mode 100644 index 0000000..917e841 --- /dev/null +++ b/tests/Cache/AbstractCacheTest.php @@ -0,0 +1,91 @@ +session = new SessionCache(); + parent::__construct(); + } + + public function testCanSetGetDeleteMultiple() + { + $array = array( + "key1" => "value", + "key2" => true, + "key3" => 1 + ); + $this->assertTrue( + $this->session->setMultiple($array) + ); + + $this->assertEquals( + $array, + $this->session->getMultiple(array_keys($array)) + ); + + $this->assertTrue( + $this->session->deleteMultiple(array_keys($array)) + ); + + $this->assertContainsOnly( + "string", + $this->session->getMultiple(array_keys($array), "default") + ); + } + + + /** + * Exceptions :p + */ + + public function testGetMultipleIterable() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Keys are not iterable'); + $this->session->getMultiple("I have some spaces!", "I am a value"); + } + + public function testSetMultipleIterable() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Values are not Iterable'); + $this->session->setMultiple("I have some spaces!", "I am a value"); + } + + public function testDeleteMultipleIterable() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Keys are not iterable'); + $this->session->deleteMultiple("I have some spaces!", "I am a value"); + } + + public function testGetMultipleInvalid() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid Key'); + $this->session->getMultiple(array("I have some spaces!"), "I am a value"); + } + + public function testSetMultipleInvalid() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid Key'); + $this->session->setMultiple(array("I have some spaces!" => "test")); + } + + public function testDeleteMultipleInvalid() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid Key'); + $this->session->deleteMultiple(array("I have some spaces!")); + } +} diff --git a/tests/Cache/SessionCacheTest.php b/tests/Cache/SessionCacheTest.php new file mode 100644 index 0000000..d80d061 --- /dev/null +++ b/tests/Cache/SessionCacheTest.php @@ -0,0 +1,149 @@ +session = new SessionCache(); + parent::__construct(); + } + + public function testCacheHasStarted() + { + $this->assertEquals( + session_status(), + PHP_SESSION_ACTIVE + ); + } + + public function testCanSetAndGet() + { + $this; + $value = "value"; + $this->assertTrue( + $this->session->set("key", $value) + ); + + $this->assertEquals( + $value, + $this->session->get("key") + ); + } + + public function testCanGetDefaultIfTTL() + { + $this->session->set("ttling", "yes of course", 0); + + $this->assertFalse( + $this->session->get("ttling", false) + ); + + $this->session->set("ttling", "yes of course", DateInterval::createFromDateString("2019-02-10")); + + $this->assertFalse( + $this->session->get("ttling", false) + ); + } + + public function testCanGetDefaultIfNotSet() + { + $values = array( + 'test', + true, + 1, + array() + ); + foreach ($values as $value) { + $this->assertEquals( + $value, + $this->session->get("random_be_with_me", $value) + ); + } + } + + + public function testCanDelete() + { + $this->session->set("key", "value"); + $this->assertTrue( + $this->session->delete('key') + ); + $this->assertNull( + $this->session->get('key') + ); + } + + public function testCanClearCache() + { + $array = array( + "key1" => "value", + "key2" => true, + "key3" => 1 + ); + $this->assertTrue( + $this->session->setMultiple($array) + ); + $this->assertTrue( + $this->session->clear() + ); + $this->assertContainsOnly( + "string", + $this->session->getMultiple(array_keys($array), "default") + ); + } + + public function testHas() + { + $this->assertFalse( + $this->session->has("pokemon") + ); + + $this->session->set("pokemon", "go"); + + $this->assertTrue( + $this->session->has("pokemon") + ); + } + + + + + /** + * Exceptions :p + */ + + public function testInvalidKeySet() + { + $this->expectException(InvalidArgumentException::class); + $this->session->set("I have some spaces!", "I am a value"); + } + + public function testInvalidKeyGet() + { + $this->expectException(InvalidArgumentException::class); + $this->session->get("I have some spaces!", "I am a value"); + } + + public function testInvalidKeyDelete() + { + $this->expectException(InvalidArgumentException::class); + $this->session->delete("I have some spaces!", "I am a value"); + } + + public function testInvalidKeyHas() + { + $this->expectException(InvalidArgumentException::class); + $this->session->has("I have some spaces!", "I am a value"); + } +} diff --git a/tests/SessionCacheTest.php b/tests/SessionCacheTest.php deleted file mode 100644 index 40a18f5..0000000 --- a/tests/SessionCacheTest.php +++ /dev/null @@ -1,91 +0,0 @@ -session = new SessionCache(); - } - - public function testCacheCanStart() - { - $this->assertEquals( - session_status(), - PHP_SESSION_ACTIVE - ); - } - - public function testCanSet() - { - - $this->assertEquals( - true, - $this->session->set("key", "value") - ); - } - - public function testCanGet() - { - $this->assertTrue( - $this->session->set("key", "value") - ); - $this->assertEquals( - 'value', - $this->session->get('key') - ); - } - - - public function testCanDelete() - { - $this->assertTrue( - $this->session->set("key", "value") - ); - $this->assertTrue( - $this->session->delete('key') - ); - $this->assertNull( - $this->session->get('key') - ); - } - - public function testCanSetGetMultiple() - { - - $this->assertTrue( - $this->session->setMultiple(array( - "key1" => "value", - "key2" => "value" - )) - ); - - $result = $this->session->getMultiple(array( - "key1", - "key2" - )); - $this->assertEquals( - "value", - $result["key1"] - ); - $this->assertEquals( - "value", - $result["key2"] - ); - - $this->assertTrue( - $this->session->deleteMultiple(array( - "key1", - "key2" - )) - ); - } -}