CacheTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, $recompute = INF)
  26. {
  27. static::bootKernel();
  28. // Setup Common::config to have the values in $conf
  29. $conf = ['cache' => ['adapters' => $adapters, 'early_recompute' => $recompute]];
  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 testConfigurationParsingSingle()
  46. {
  47. self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]);
  48. }
  49. public function testConfigurationParsingCluster1()
  50. {
  51. self::doTest(['default' => 'redis://redis;redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class], \App\Util\Exception\ConfigurationException::class);
  52. }
  53. // /**
  54. // * This requires extra server configuration, but the code was tested
  55. // * manually and works, so it'll be excluded from automatic tests, for now, at least
  56. // */
  57. // public function testConfigurationParsingCluster2()
  58. // {
  59. // self::doTest(['default' => 'redis://redis:6379;redis://redis:6379'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]);
  60. // }
  61. public function testConfigurationParsingFallBack()
  62. {
  63. self::doTest(['default' => 'redis://redis,filesystem'], ['default' => \Symfony\Component\Cache\Adapter\ChainAdapter::class]);
  64. }
  65. public function testConfigurationParsingMultiple()
  66. {
  67. self::doTest(['default' => 'redis://redis', 'file' => 'filesystem://test'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class, 'file' => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class]);
  68. }
  69. public function testGeneralImplementation()
  70. {
  71. // Need a connection to run the tests
  72. self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class]);
  73. static::assertSame('value', Cache::get('test', function ($i) { return 'value'; }));
  74. Cache::set('test', 'other_value');
  75. static::assertSame('other_value', Cache::get('test', function ($i) { return 'value'; }));
  76. static::assertTrue(Cache::delete('test'));
  77. }
  78. private function _testRedis($recompute = INF)
  79. {
  80. self::doTest(['default' => 'redis://redis'], ['default' => \Symfony\Component\Cache\Adapter\RedisAdapter::class], throws: null, recompute: $recompute);
  81. // Redis supports lists directly, uses different implementation
  82. $key = 'test' . time();
  83. static::assertSame(['foo', 'bar'], Cache::getList($key, function ($i) { return ['foo', 'bar']; }));
  84. Cache::pushList($key, 'quux');
  85. static::assertSame(['foo', 'bar', 'quux'], Cache::getList($key, function ($i) { $this->assertFalse('should not be called'); }));
  86. Cache::pushList($key, 'foobar', pool: 'default', max_count: 2);
  87. static::assertSame(['quux', 'foobar'], Cache::getList($key, function ($i) { $this->assertFalse('should not be called'); }));
  88. static::assertTrue(Cache::deleteList($key));
  89. }
  90. public function testRedisImplementation()
  91. {
  92. $this->_testRedis();
  93. }
  94. public function testRedisImplementationNoEarlyRecompute()
  95. {
  96. $this->_testRedis(null);
  97. }
  98. public function testNonRedisImplementation()
  99. {
  100. self::doTest(['file' => 'filesystem://test'], ['file' => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class]);
  101. $key = 'test' . time();
  102. Cache::setList("{$key}-other", ['foo', 'bar'], pool: 'file');
  103. static::assertSame(['foo', 'bar'], Cache::getList("{$key}-other", function ($i) { $this->assertFalse('should not be called'); }, pool: 'file'));
  104. static::assertSame(['foo', 'bar'], Cache::getList($key, fn ($i) => ['foo', 'bar'], pool: 'file'));
  105. Cache::pushList($key, 'quux', pool: 'file');
  106. static::assertSame(['foo', 'bar', 'quux'], Cache::getList($key, function ($i) { $this->assertFalse('should not be called'); }, pool: 'file'));
  107. Cache::pushList($key, 'foobar', pool: 'file', max_count: 2);
  108. static::assertSame(['quux', 'foobar'], Cache::getList($key, function ($i) { $this->assertFalse('should not be called'); }, pool: 'file'));
  109. static::assertTrue(Cache::deleteList($key, pool: 'file'));
  110. }
  111. }