CacheTest.php 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace App\Tests\Core;
  19. use App\Core\Cache;
  20. use App\Util\Common;
  21. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  22. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  23. class CacheTest extends KernelTestCase
  24. {
  25. private function doTest(array $adapters, $result_pool, $throws = null)
  26. {
  27. static::bootKernel();
  28. // Setup Common::config to have the values in $conf
  29. $conf = ['cache' => ['adapters' => $adapters, 'early_recompute' => INF]];
  30. $cb = $this->createMock(ContainerBagInterface::class);
  31. static::assertTrue($cb instanceof ContainerBagInterface);
  32. $cb->method('get')
  33. ->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
  34. Common::setupConfig($cb);
  35. if ($throws != null) {
  36. $this->expectException($throws);
  37. }
  38. Cache::setupCache();
  39. $reflector = new \ReflectionClass('App\Core\Cache');
  40. $pools = $reflector->getStaticPropertyValue('pools');
  41. foreach ($result_pool as $name => $type) {
  42. static::assertInstanceOf($type, $pools[$name]);
  43. }
  44. }
  45. public function testConfigurationParsing()
  46. {
  47. self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]);
  48. self::doTest(['default' => 'redis://redis;redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class], \App\Util\Exception\ConfigurationException::class);
  49. self::doTest(['default' => 'redis://redis:6379;redis://redis:6379'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]);
  50. self::doTest(['default' => 'redis://redis,filesystem'], ['default' => \Symfony\Component\Cache\Adapter\ChainAdapter::class]);
  51. self::doTest(['default' => 'redis://redis', 'file' => 'filesystem://test'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class, 'file' => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class]);
  52. }
  53. public function testGeneralImplementation()
  54. {
  55. // Need a connection to run the tests
  56. self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]);
  57. static::assertSame('value', Cache::get('test', function ($i) { return 'value'; }));
  58. Cache::set('test', 'other_value');
  59. static::assertSame('other_value', Cache::get('test', function ($i) { return 'value'; }));
  60. static::assertTrue(Cache::delete('test'));
  61. }
  62. public function testRedisImplementation()
  63. {
  64. self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]);
  65. // Redis supports lists directly, uses different implementation
  66. static::assertSame(['foo', 'bar'], Cache::getList('test', function ($i) { return ['foo', 'bar']; }));
  67. Cache::pushList('test', 'quux');
  68. static::assertSame(['foo', 'bar', 'quux'], Cache::getList('test', function ($i) { return ['foo', 'bar']; }));
  69. static::assertTrue(Cache::deleteList('test'));
  70. }
  71. public function testNonRedisImplementation()
  72. {
  73. self::doTest(['file' => 'filesystem://test'], ['file' => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class]);
  74. $key = 'test' . time();
  75. static::assertSame(['foo', 'bar'], Cache::getList($key, function ($i) { return ['foo', 'bar']; }, pool: 'file'));
  76. Cache::pushList($key, 'quux', pool: 'file');
  77. static::assertSame(['foo', 'bar', 'quux'], Cache::getList($key, function ($i) { return ['foo', 'bar']; }, pool: 'file'));
  78. static::assertTrue(Cache::deleteList($key, pool: 'file'));
  79. }
  80. }