1
0

FactoryTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Memcache;
  8. use OC\Memcache\NullCache;
  9. use OCP\Profiler\IProfiler;
  10. use Psr\Log\LoggerInterface;
  11. class Test_Factory_Available_Cache1 extends NullCache {
  12. public function __construct($prefix = '') {
  13. }
  14. public static function isAvailable(): bool {
  15. return true;
  16. }
  17. }
  18. class Test_Factory_Available_Cache2 extends NullCache {
  19. public function __construct($prefix = '') {
  20. }
  21. public static function isAvailable(): bool {
  22. return true;
  23. }
  24. }
  25. class Test_Factory_Unavailable_Cache1 extends NullCache {
  26. public function __construct($prefix = '') {
  27. }
  28. public static function isAvailable(): bool {
  29. return false;
  30. }
  31. }
  32. class Test_Factory_Unavailable_Cache2 extends NullCache {
  33. public function __construct($prefix = '') {
  34. }
  35. public static function isAvailable(): bool {
  36. return false;
  37. }
  38. }
  39. /**
  40. * @group Memcache
  41. */
  42. class FactoryTest extends \Test\TestCase {
  43. public const AVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Available_Cache1';
  44. public const AVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Available_Cache2';
  45. public const UNAVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache1';
  46. public const UNAVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache2';
  47. public function cacheAvailabilityProvider() {
  48. return [
  49. [
  50. // local and distributed available
  51. self::AVAILABLE1, self::AVAILABLE2, null,
  52. self::AVAILABLE1, self::AVAILABLE2, \OC\Memcache\Factory::NULL_CACHE
  53. ],
  54. [
  55. // local and distributed null
  56. null, null, null,
  57. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE
  58. ],
  59. [
  60. // local available, distributed null (most common scenario)
  61. self::AVAILABLE1, null, null,
  62. self::AVAILABLE1, self::AVAILABLE1, \OC\Memcache\Factory::NULL_CACHE
  63. ],
  64. [
  65. // locking cache available
  66. null, null, self::AVAILABLE1,
  67. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, self::AVAILABLE1
  68. ],
  69. [
  70. // locking cache unavailable: no exception here in the factory
  71. null, null, self::UNAVAILABLE1,
  72. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE
  73. ]
  74. ];
  75. }
  76. public function cacheUnavailableProvider() {
  77. return [
  78. [
  79. // local available, distributed unavailable
  80. self::AVAILABLE1, self::UNAVAILABLE1
  81. ],
  82. [
  83. // local unavailable, distributed available
  84. self::UNAVAILABLE1, self::AVAILABLE1
  85. ],
  86. [
  87. // local and distributed unavailable
  88. self::UNAVAILABLE1, self::UNAVAILABLE2
  89. ],
  90. ];
  91. }
  92. /**
  93. * @dataProvider cacheAvailabilityProvider
  94. */
  95. public function testCacheAvailability($localCache, $distributedCache, $lockingCache,
  96. $expectedLocalCache, $expectedDistributedCache, $expectedLockingCache): void {
  97. $logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  98. $profiler = $this->getMockBuilder(IProfiler::class)->getMock();
  99. $factory = new \OC\Memcache\Factory(fn () => 'abc', $logger, $profiler, $localCache, $distributedCache, $lockingCache);
  100. $this->assertTrue(is_a($factory->createLocal(), $expectedLocalCache));
  101. $this->assertTrue(is_a($factory->createDistributed(), $expectedDistributedCache));
  102. $this->assertTrue(is_a($factory->createLocking(), $expectedLockingCache));
  103. }
  104. /**
  105. * @dataProvider cacheUnavailableProvider
  106. */
  107. public function testCacheNotAvailableException($localCache, $distributedCache): void {
  108. $this->expectException(\OCP\HintException::class);
  109. $logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  110. $profiler = $this->getMockBuilder(IProfiler::class)->getMock();
  111. new \OC\Memcache\Factory(fn () => 'abc', $logger, $profiler, $localCache, $distributedCache);
  112. }
  113. public function testCreateInMemory(): void {
  114. $logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  115. $profiler = $this->getMockBuilder(IProfiler::class)->getMock();
  116. $factory = new \OC\Memcache\Factory(fn () => 'abc', $logger, $profiler, null, null, null);
  117. $cache = $factory->createInMemory();
  118. $cache->set('test', 48);
  119. self::assertSame(48, $cache->get('test'));
  120. }
  121. }