Changed Caches Tests

This commit is contained in:
Florian Bouillon 2019-04-26 15:27:28 +02:00
parent 1f687b8cd9
commit 1f6d957aa1
3 changed files with 240 additions and 91 deletions

View File

@ -0,0 +1,91 @@
<?php
namespace Tests\Cache;
use PHPUnit\Framework\TestCase;
use DeltaCMS\Cache\SessionCache;
use DeltaCMS\Cache\InvalidArgumentException;
class AbstractCacheTest extends TestCase
{
private $session;
public function __construct()
{
$this->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!"));
}
}

View File

@ -0,0 +1,149 @@
<?php
namespace Tests;
use PHPUnit\Framework\TestCase;
use DeltaCMS\Cache\SessionCache;
use DeltaCMS\Cache\InvalidArgumentException;
use DateInterval;
final class SessionCacheTest extends TestCase
{
private $session;
public function __construct()
{
$this->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");
}
}

View File

@ -1,91 +0,0 @@
<?php
namespace Tests;
use PHPUnit\Framework\TestCase;
use DeltaCMS\Cache\SessionCache;
final class SessionCacheTest extends TestCase
{
private $session;
public function __construct()
{
parent::__construct();
$this->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"
))
);
}
}