From c539f17ba55e82605b520371821dafaa6b15fce5 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 1 Apr 2021 22:24:39 +0000 Subject: [PATCH] [TESTS] Add App\Core\Cache test --- src/Util/Exception/ConfigurationException.php | 39 ++++++++ tests/Core/CacheTest.php | 97 +++++++++++++++++++ tests/Core/DB/UpdateListenerTest.php | 2 +- 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 src/Util/Exception/ConfigurationException.php create mode 100644 tests/Core/CacheTest.php diff --git a/src/Util/Exception/ConfigurationException.php b/src/Util/Exception/ConfigurationException.php new file mode 100644 index 0000000000..b7b944a2b0 --- /dev/null +++ b/src/Util/Exception/ConfigurationException.php @@ -0,0 +1,39 @@ +. +// }}} + +/** + * Nickname empty exception + * + * @category Exception + * @package GNUsocial + * + * @author Hugo Sales + * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ + +namespace App\Util\Exception; + +class ConfigurationException extends ServerException +{ + public function __construct(string $m) + { + parent::__construct($m); + } +} diff --git a/tests/Core/CacheTest.php b/tests/Core/CacheTest.php new file mode 100644 index 0000000000..dd0e6aef13 --- /dev/null +++ b/tests/Core/CacheTest.php @@ -0,0 +1,97 @@ +. + +// }}} + +namespace App\Tests\Core; + +use App\Core\Cache; +use App\Util\Common; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; + +class CacheTest extends KernelTestCase +{ + private function doTest(array $adapters, $result_pool, $throws = null) + { + static::bootKernel(); + + // Setup Common::config to have the values in $conf + $conf = ['cache' => ['adapters' => $adapters]]; + $cb = $this->createMock(ContainerBagInterface::class); + static::assertTrue($cb instanceof ContainerBagInterface); + $cb->method('get') + ->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]); + Common::setupConfig($cb); + + if ($throws != null) { + $this->expectException($throws); + } + + Cache::setupCache(); + + $reflector = new \ReflectionClass('App\Core\Cache'); + $pools = $reflector->getStaticPropertyValue('pools'); + foreach ($result_pool as $name => $type) { + static::assertInstanceOf($type, $pools[$name]); + } + } + + public function testConfigurationParsing() + { + self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]); + self::doTest(['default' => 'redis://redis;redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class], \App\Util\Exception\ConfigurationException::class); + self::doTest(['default' => 'redis://redis:6379;redis://redis:6379'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]); + self::doTest(['default' => 'redis://redis,filesystem'], ['default' => \Symfony\Component\Cache\Adapter\ChainAdapter::class]); + self::doTest(['default' => 'redis://redis', 'file' => 'filesystem://test'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class, 'file' => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class]); + } + + public function testGeneralImplementation() + { + // Need a connection to run the tests + self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]); + + static::assertSame('value', Cache::get('test', function ($i) { return 'value'; })); + Cache::set('test', 'other_value'); + static::assertSame('other_value', Cache::get('test', function ($i) { return 'value'; })); + static::assertTrue(Cache::delete('test')); + } + + public function testRedisImplementation() + { + self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]); + + // Redis supports lists directly, uses different implementation + static::assertSame(['foo', 'bar'], Cache::getList('test', function ($i) { return ['foo', 'bar']; })); + Cache::pushList('test', 'quux'); + static::assertSame(['foo', 'bar', 'quux'], Cache::getList('test', function ($i) { return ['foo', 'bar']; })); + static::assertTrue(Cache::deleteList('test')); + } + + public function testNonRedisImplementation() + { + self::doTest(['file' => 'filesystem://test'], ['file' => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class]); + + $key = 'test' . time(); + static::assertSame(['foo', 'bar'], Cache::getList($key, function ($i) { return ['foo', 'bar']; }, pool: 'file')); + Cache::pushList($key, 'quux', pool: 'file'); + static::assertSame(['foo', 'bar', 'quux'], Cache::getList($key, function ($i) { return ['foo', 'bar']; }, pool: 'file')); + static::assertTrue(Cache::deleteList($key, pool: 'file')); + } +} diff --git a/tests/Core/DB/UpdateListenerTest.php b/tests/Core/DB/UpdateListenerTest.php index b902d8677b..5ea25fdd3e 100644 --- a/tests/Core/DB/UpdateListenerTest.php +++ b/tests/Core/DB/UpdateListenerTest.php @@ -17,7 +17,7 @@ // along with GNU social. If not, see . // }}} -namespace App\Tests\EventListener; +namespace App\Tests\Core\DB; use App\Core\DB\UpdateListener; use App\Entity\GSActor;