factory.php 3.9 KB

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