FactoryTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Memcache;
  22. use OC\Memcache\NullCache;
  23. use Psr\Log\LoggerInterface;
  24. use OCP\Profiler\IProfiler;
  25. class Test_Factory_Available_Cache1 extends NullCache {
  26. public function __construct($prefix = '') {
  27. }
  28. public static function isAvailable(): bool {
  29. return true;
  30. }
  31. }
  32. class Test_Factory_Available_Cache2 extends NullCache {
  33. public function __construct($prefix = '') {
  34. }
  35. public static function isAvailable(): bool {
  36. return true;
  37. }
  38. }
  39. class Test_Factory_Unavailable_Cache1 extends NullCache {
  40. public function __construct($prefix = '') {
  41. }
  42. public static function isAvailable(): bool {
  43. return false;
  44. }
  45. }
  46. class Test_Factory_Unavailable_Cache2 extends NullCache {
  47. public function __construct($prefix = '') {
  48. }
  49. public static function isAvailable(): bool {
  50. return false;
  51. }
  52. }
  53. /**
  54. * @group Memcache
  55. */
  56. class FactoryTest extends \Test\TestCase {
  57. public const AVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Available_Cache1';
  58. public const AVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Available_Cache2';
  59. public const UNAVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache1';
  60. public const UNAVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache2';
  61. public function cacheAvailabilityProvider() {
  62. return [
  63. [
  64. // local and distributed available
  65. self::AVAILABLE1, self::AVAILABLE2, null,
  66. self::AVAILABLE1, self::AVAILABLE2, \OC\Memcache\Factory::NULL_CACHE
  67. ],
  68. [
  69. // local and distributed null
  70. null, null, null,
  71. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE
  72. ],
  73. [
  74. // local available, distributed null (most common scenario)
  75. self::AVAILABLE1, null, null,
  76. self::AVAILABLE1, self::AVAILABLE1, \OC\Memcache\Factory::NULL_CACHE
  77. ],
  78. [
  79. // locking cache available
  80. null, null, self::AVAILABLE1,
  81. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, self::AVAILABLE1
  82. ],
  83. [
  84. // locking cache unavailable: no exception here in the factory
  85. null, null, self::UNAVAILABLE1,
  86. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE
  87. ]
  88. ];
  89. }
  90. public function cacheUnavailableProvider() {
  91. return [
  92. [
  93. // local available, distributed unavailable
  94. self::AVAILABLE1, self::UNAVAILABLE1
  95. ],
  96. [
  97. // local unavailable, distributed available
  98. self::UNAVAILABLE1, self::AVAILABLE1
  99. ],
  100. [
  101. // local and distributed unavailable
  102. self::UNAVAILABLE1, self::UNAVAILABLE2
  103. ],
  104. ];
  105. }
  106. /**
  107. * @dataProvider cacheAvailabilityProvider
  108. */
  109. public function testCacheAvailability($localCache, $distributedCache, $lockingCache,
  110. $expectedLocalCache, $expectedDistributedCache, $expectedLockingCache) {
  111. $logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  112. $profiler = $this->getMockBuilder(IProfiler::class)->getMock();
  113. $factory = new \OC\Memcache\Factory('abc', $logger, $profiler, $localCache, $distributedCache, $lockingCache);
  114. $this->assertTrue(is_a($factory->createLocal(), $expectedLocalCache));
  115. $this->assertTrue(is_a($factory->createDistributed(), $expectedDistributedCache));
  116. $this->assertTrue(is_a($factory->createLocking(), $expectedLockingCache));
  117. }
  118. /**
  119. * @dataProvider cacheUnavailableProvider
  120. */
  121. public function testCacheNotAvailableException($localCache, $distributedCache) {
  122. $this->expectException(\OCP\HintException::class);
  123. $logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  124. $profiler = $this->getMockBuilder(IProfiler::class)->getMock();
  125. new \OC\Memcache\Factory('abc', $logger, $profiler, $localCache, $distributedCache);
  126. }
  127. }