Skip to content

Commit

Permalink
Fix NullCache::getFrontend() and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gnumoksha authored and sergeyklay committed May 31, 2018
1 parent c558747 commit 5d75125
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Library/Phalcon/Cache/Backend/NullCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function stop($stopBuffer = true)

public function getFrontend()
{
return NoneFrontend();
return new NoneFrontend();
}

public function getOptions()
Expand Down
135 changes: 135 additions & 0 deletions tests/unit/Cache/Backend/NullCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Phalcon\Test\Cache\Backend;

use Phalcon\Cache\Backend\NullCache;
use Phalcon\Test\Codeception\UnitTestCase as Test;
use Phalcon\Cache\Frontend\None as NoneFrontend;

/**
* \Phalcon\Test\Cache\Backend\DatabaseTest
* Tests for Phalcon\Cache\Backend\Database component
*
* @copyright (c) 2011-2018 Phalcon Team
* @link http://www.phalconphp.com
* @author Nikita Vershinin <[email protected]>
* @package Phalcon\Test\Cache\Backend
* @group db
*
* The contents of this file are subject to the New BSD License that is
* bundled with this package in the file docs/LICENSE.txt
*
* If you did not receive a copy of the license and are unable to obtain it
* through the world-wide-web, please send an email to [email protected]
* so that we can send you a copy immediately.
*/
class NullCacheTest extends Test
{
public function testStartShouldAlwaysReturnTrue()
{
$nullCache = new NullCache();

$this->assertTrue($nullCache->start('fooBar'));
$this->assertTrue($nullCache->start('fooBar'), 1000);
}

public function testFrontendShouldBeNone()
{
$nullCache = new NullCache();

$this->assertInstanceOf(NoneFrontend::class, $nullCache->getFrontend());
}

public function testGetOptionsShouldReturnEmptyArray()
{
$nullCache = new NullCache();

$this->assertEquals([], $nullCache->getOptions());
}

public function testCacheShouldAlwaysBeFresh()
{
$nullCache = new NullCache();

$this->assertTrue($nullCache->isFresh());
}

public function testCacheShouldAlwaysBeStarted()
{
$nullCache = new NullCache();

$this->assertTrue($nullCache->isStarted());

$nullCache->start('fooBar');
$this->assertTrue($nullCache->isStarted());

$nullCache->stop();
$this->assertTrue($nullCache->isStarted());
}

public function testLastKeyShouldBeEmpty()
{
$nullCache = new NullCache();

$this->assertEquals('', $nullCache->getLastKey());
}

public function testGetSomethingFromCacheShouldAlwaysReturnNull()
{
$nullCache = new NullCache();

$this->assertEquals(null, $nullCache->get('fooBar'));
$this->assertEquals(null, $nullCache->get('fooBar', 1000));
$this->assertEquals(null, $nullCache->get('fooBar', 0));

$nullCache->save('fooBar', 'baz', 1000);
$this->assertEquals(null, $nullCache->get('fooBar'));
$this->assertEquals(null, $nullCache->get('fooBar', 1000));
$this->assertEquals(null, $nullCache->get('fooBar', 0));
}

public function testSaveSomethingToCacheShouldAlwaysReturnTrue()
{
$nullCache = new NullCache();

$this->assertTrue($nullCache->save('fooBar', null));
$this->assertTrue($nullCache->save('fooBar', 'baz'));
$this->assertTrue($nullCache->save('fooBar', 'baz', 1000));
$this->assertTrue($nullCache->save('fooBar', 'baz', 1000, true));
$this->assertTrue($nullCache->save('fooBar', 'baz', 1000, false));
}

public function testDeleteSomethingFromCacheShouldAlwaysReturnTrue()
{
$nullCache = new NullCache();

$this->assertTrue($nullCache->delete('fooBar'));
$this->assertFalse($nullCache->exists('fooBar'));

$randomKey = 'randomKey' . uniqid('NullCache', true);
$this->assertTrue($nullCache->delete($randomKey));
$this->assertFalse($nullCache->exists($randomKey));
}

public function testQueryKeysShouldReturnEmptyArray()
{
$nullCache = new NullCache();

$this->assertEquals([], $nullCache->queryKeys());
$this->assertEquals([], $nullCache->queryKeys('fooBar'));
}

public function testNoKeyWillEverExistsInTheCache()
{
$nullCache = new NullCache();

$this->assertFalse($nullCache->exists('fooBar'));

$this->assertTrue($nullCache->save('fooBar', 'baz'));
$this->assertFalse($nullCache->exists('fooBar'));

$randomKey = 'randomKey' . uniqid('NullCache', true);
$this->assertTrue($nullCache->save('fooBar', $randomKey));
$this->assertFalse($nullCache->exists($randomKey));
}
}

0 comments on commit 5d75125

Please sign in to comment.