FactoryTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 OCP\ILogger;
  24. class Test_Factory_Available_Cache1 extends NullCache {
  25. public function __construct($prefix = '') {
  26. }
  27. public static function isAvailable() {
  28. return true;
  29. }
  30. }
  31. class Test_Factory_Available_Cache2 extends NullCache {
  32. public function __construct($prefix = '') {
  33. }
  34. public static function isAvailable() {
  35. return true;
  36. }
  37. }
  38. class Test_Factory_Unavailable_Cache1 extends NullCache {
  39. public function __construct($prefix = '') {
  40. }
  41. public static function isAvailable() {
  42. return false;
  43. }
  44. }
  45. class Test_Factory_Unavailable_Cache2 extends NullCache {
  46. public function __construct($prefix = '') {
  47. }
  48. public static function isAvailable() {
  49. return false;
  50. }
  51. }
  52. class FactoryTest extends \Test\TestCase {
  53. public const AVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Available_Cache1';
  54. public const AVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Available_Cache2';
  55. public const UNAVAILABLE1 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache1';
  56. public const UNAVAILABLE2 = '\\Test\\Memcache\\Test_Factory_Unavailable_Cache2';
  57. public function cacheAvailabilityProvider() {
  58. return [
  59. [
  60. // local and distributed available
  61. self::AVAILABLE1, self::AVAILABLE2, null,
  62. self::AVAILABLE1, self::AVAILABLE2, \OC\Memcache\Factory::NULL_CACHE
  63. ],
  64. [
  65. // local and distributed null
  66. null, null, null,
  67. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE
  68. ],
  69. [
  70. // local available, distributed null (most common scenario)
  71. self::AVAILABLE1, null, null,
  72. self::AVAILABLE1, self::AVAILABLE1, \OC\Memcache\Factory::NULL_CACHE
  73. ],
  74. [
  75. // locking cache available
  76. null, null, self::AVAILABLE1,
  77. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, self::AVAILABLE1
  78. ],
  79. [
  80. // locking cache unavailable: no exception here in the factory
  81. null, null, self::UNAVAILABLE1,
  82. \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE, \OC\Memcache\Factory::NULL_CACHE
  83. ]
  84. ];
  85. }
  86. public function cacheUnavailableProvider() {
  87. return [
  88. [
  89. // local available, distributed unavailable
  90. self::AVAILABLE1, self::UNAVAILABLE1
  91. ],
  92. [
  93. // local unavailable, distributed available
  94. self::UNAVAILABLE1, self::AVAILABLE1
  95. ],
  96. [
  97. // local and distributed unavailable
  98. self::UNAVAILABLE1, self::UNAVAILABLE2
  99. ],
  100. ];
  101. }
  102. /**
  103. * @dataProvider cacheAvailabilityProvider
  104. */
  105. public function testCacheAvailability($localCache, $distributedCache, $lockingCache,
  106. $expectedLocalCache, $expectedDistributedCache, $expectedLockingCache) {
  107. $logger = $this->getMockBuilder(ILogger::class)->getMock();
  108. $factory = new \OC\Memcache\Factory('abc', $logger, $localCache, $distributedCache, $lockingCache);
  109. $this->assertTrue(is_a($factory->createLocal(), $expectedLocalCache));
  110. $this->assertTrue(is_a($factory->createDistributed(), $expectedDistributedCache));
  111. $this->assertTrue(is_a($factory->createLocking(), $expectedLockingCache));
  112. }
  113. /**
  114. * @dataProvider cacheUnavailableProvider
  115. */
  116. public function testCacheNotAvailableException($localCache, $distributedCache) {
  117. $this->expectException(\OC\HintException::class);
  118. $logger = $this->getMockBuilder(ILogger::class)->getMock();
  119. new \OC\Memcache\Factory('abc', $logger, $localCache, $distributedCache);
  120. }
  121. }