ObjectCacheTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. class ObjectCacheTest extends MediaWikiTestCase {
  3. protected function setUp() {
  4. // Parent calls ObjectCache::clear() among other things
  5. parent::setUp();
  6. $this->setCacheConfig();
  7. $this->setMwGlobals( [
  8. 'wgMainCacheType' => CACHE_NONE,
  9. 'wgMessageCacheType' => CACHE_NONE,
  10. 'wgParserCacheType' => CACHE_NONE,
  11. ] );
  12. }
  13. private function setCacheConfig( $arr = [] ) {
  14. $defaults = [
  15. CACHE_NONE => [ 'class' => EmptyBagOStuff::class ],
  16. CACHE_DB => [ 'class' => SqlBagOStuff::class ],
  17. CACHE_ANYTHING => [ 'factory' => 'ObjectCache::newAnything' ],
  18. // Mock ACCEL with 'hash' as being installed.
  19. // This makes tests deterministic regardless of APC.
  20. CACHE_ACCEL => [ 'class' => HashBagOStuff::class ],
  21. 'hash' => [ 'class' => HashBagOStuff::class ],
  22. ];
  23. $this->setMwGlobals( 'wgObjectCaches', $arr + $defaults );
  24. }
  25. /** @covers ObjectCache::newAnything */
  26. public function testNewAnythingNothing() {
  27. $this->assertInstanceOf(
  28. SqlBagOStuff::class,
  29. ObjectCache::newAnything( [] ),
  30. 'No available types. Fallback to DB'
  31. );
  32. }
  33. /** @covers ObjectCache::newAnything */
  34. public function testNewAnythingHash() {
  35. $this->setMwGlobals( [
  36. 'wgMainCacheType' => 'hash'
  37. ] );
  38. $this->assertInstanceOf(
  39. HashBagOStuff::class,
  40. ObjectCache::newAnything( [] ),
  41. 'Use an available type (hash)'
  42. );
  43. }
  44. /** @covers ObjectCache::newAnything */
  45. public function testNewAnythingAccel() {
  46. $this->setMwGlobals( [
  47. 'wgMainCacheType' => CACHE_ACCEL
  48. ] );
  49. $this->assertInstanceOf(
  50. HashBagOStuff::class,
  51. ObjectCache::newAnything( [] ),
  52. 'Use an available type (CACHE_ACCEL)'
  53. );
  54. }
  55. /** @covers ObjectCache::newAnything */
  56. public function testNewAnythingNoAccel() {
  57. $this->setMwGlobals( [
  58. 'wgMainCacheType' => CACHE_ACCEL
  59. ] );
  60. $this->setCacheConfig( [
  61. // Mock APC not being installed (T160519, T147161)
  62. CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
  63. ] );
  64. $this->assertInstanceOf(
  65. SqlBagOStuff::class,
  66. ObjectCache::newAnything( [] ),
  67. 'Fallback to DB if available types fall back to Empty'
  68. );
  69. }
  70. /** @covers ObjectCache::newAnything */
  71. public function testNewAnythingNoAccelNoDb() {
  72. $this->overrideMwServices(); // Ensures restore on tear down
  73. MediaWiki\MediaWikiServices::disableStorageBackend();
  74. $this->setMwGlobals( [
  75. 'wgMainCacheType' => CACHE_ACCEL
  76. ] );
  77. $this->setCacheConfig( [
  78. // Mock APC not being installed (T160519, T147161)
  79. CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
  80. ] );
  81. $this->assertInstanceOf(
  82. EmptyBagOStuff::class,
  83. ObjectCache::newAnything( [] ),
  84. 'Fallback to none if available types and DB are unavailable'
  85. );
  86. }
  87. /** @covers ObjectCache::newAnything */
  88. public function testNewAnythingNothingNoDb() {
  89. $this->overrideMwServices();
  90. MediaWiki\MediaWikiServices::disableStorageBackend();
  91. $this->assertInstanceOf(
  92. EmptyBagOStuff::class,
  93. ObjectCache::newAnything( [] ),
  94. 'No available types or DB. Fallback to none.'
  95. );
  96. }
  97. }