FactoryTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. class FactoryTest extends \Test\TestCase {
  54. public const AVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Available_Cache1';
  55. public const AVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Available_Cache2';
  56. public const UNAVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache1';
  57. public const UNAVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache2';
  58. public function cacheAvailabilityProvider() {
  59. return [
  60. [
  61. // local and distributed available
  62. self::AVAILABLE1, self::AVAILABLE2, null,
  63. self::AVAILABLE1, self::AVAILABLE2, \OC\Memcache\Factory::NULL_CACHE
  64. ],
  65. [
  66. // local and distributed null
  67. null, null, null,
  68. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE
  69. ],
  70. [
  71. // local available, distributed null (most common scenario)
  72. self::AVAILABLE1, null, null,
  73. self::AVAILABLE1, self::AVAILABLE1, \OC\Memcache\Factory::NULL_CACHE
  74. ],
  75. [
  76. // locking cache available
  77. null, null, self::AVAILABLE1,
  78. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, self::AVAILABLE1
  79. ],
  80. [
  81. // locking cache unavailable: no exception here in the factory
  82. null, null, self::UNAVAILABLE1,
  83. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE
  84. ]
  85. ];
  86. }
  87. public function cacheUnavailableProvider() {
  88. return [
  89. [
  90. // local available, distributed unavailable
  91. self::AVAILABLE1, self::UNAVAILABLE1
  92. ],
  93. [
  94. // local unavailable, distributed available
  95. self::UNAVAILABLE1, self::AVAILABLE1
  96. ],
  97. [
  98. // local and distributed unavailable
  99. self::UNAVAILABLE1, self::UNAVAILABLE2
  100. ],
  101. ];
  102. }
  103. /**
  104. * @dataProvider cacheAvailabilityProvider
  105. */
  106. public function testCacheAvailability($localCache, $distributedCache, $lockingCache,
  107. $expectedLocalCache, $expectedDistributedCache, $expectedLockingCache) {
  108. $logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  109. $profiler = $this->getMockBuilder(IProfiler::class)->getMock();
  110. $factory = new \OC\Memcache\Factory('abc', $logger, $profiler, $localCache, $distributedCache, $lockingCache);
  111. $this->assertTrue(is_a($factory->createLocal(), $expectedLocalCache));
  112. $this->assertTrue(is_a($factory->createDistributed(), $expectedDistributedCache));
  113. $this->assertTrue(is_a($factory->createLocking(), $expectedLockingCache));
  114. }
  115. /**
  116. * @dataProvider cacheUnavailableProvider
  117. */
  118. public function testCacheNotAvailableException($localCache, $distributedCache) {
  119. $this->expectException(\OCP\HintException::class);
  120. $logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  121. $profiler = $this->getMockBuilder(IProfiler::class)->getMock();
  122. new \OC\Memcache\Factory('abc', $logger, $profiler, $localCache, $distributedCache);
  123. }
  124. }