FactoryTest.php 3.9 KB

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