StateTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\Cache;
  7. use \Magento\Framework\App\Cache\State;
  8. use Magento\Framework\Config\File\ConfigFilePool;
  9. class StateTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \PHPUnit_Framework_MockObject_MockObject
  13. */
  14. private $config;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $writer;
  19. protected function setUp()
  20. {
  21. $this->config = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
  22. $this->writer =
  23. $this->createPartialMock(\Magento\Framework\App\DeploymentConfig\Writer::class, ['update', 'saveConfig']);
  24. }
  25. /**
  26. * @param string $cacheType
  27. * @param array $config
  28. * @param bool $banAll
  29. * @param bool $expectedIsEnabled
  30. * @dataProvider isEnabledDataProvider
  31. */
  32. public function testIsEnabled($cacheType, $config, $banAll, $expectedIsEnabled)
  33. {
  34. $model = new State($this->config, $this->writer, $banAll);
  35. if ($banAll) {
  36. $this->config->expects($this->never())->method('getConfigData');
  37. } else {
  38. $this->config->expects($this->once())->method('getConfigData')->willReturn($config);
  39. }
  40. $this->writer->expects($this->never())->method('update');
  41. $actualIsEnabled = $model->isEnabled($cacheType);
  42. $this->assertEquals($expectedIsEnabled, $actualIsEnabled);
  43. }
  44. /**
  45. * @return array
  46. */
  47. public static function isEnabledDataProvider()
  48. {
  49. return [
  50. 'enabled' => [
  51. 'cacheType' => 'cache_type',
  52. 'config' => ['some_type' => false, 'cache_type' => true],
  53. 'banAll' => false,
  54. 'expectedIsEnabled' => true,
  55. ],
  56. 'disabled' => [
  57. 'cacheType' => 'cache_type',
  58. 'config' => ['some_type' => true, 'cache_type' => false],
  59. 'banAll' => false,
  60. 'expectedIsEnabled' => false,
  61. ],
  62. 'unknown is disabled' => [
  63. 'cacheType' => 'unknown_cache_type',
  64. 'config' => ['some_type' => true],
  65. 'banAll' => false,
  66. 'expectedIsEnabled' => false,
  67. ],
  68. 'disabled, when all caches are banned' => [
  69. 'cacheType' => 'cache_type',
  70. 'config' => ['cache_type' => true],
  71. 'banAll' => true,
  72. 'expectedIsEnabled' => false,
  73. ]
  74. ];
  75. }
  76. public function testSetEnabled()
  77. {
  78. $model = new State($this->config, $this->writer);
  79. $this->assertFalse($model->isEnabled('cache_type'));
  80. $model->setEnabled('cache_type', true);
  81. $this->assertTrue($model->isEnabled('cache_type'));
  82. $model->setEnabled('cache_type', false);
  83. $this->assertFalse($model->isEnabled('cache_type'));
  84. }
  85. public function testPersist()
  86. {
  87. $model = new State($this->config, $this->writer);
  88. $this->config->expects($this->once())->method('getConfigData')->willReturn(['test_cache_type' => true]);
  89. $configValue = [ConfigFilePool::APP_ENV => ['cache_types' => ['test_cache_type' => true]]];
  90. $this->writer->expects($this->once())->method('saveConfig')->with($configValue);
  91. $model->persist();
  92. }
  93. }