MaintenanceModeEnablerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Console;
  7. use Magento\Framework\App\Console\MaintenanceModeEnabler;
  8. use Magento\Framework\App\MaintenanceMode;
  9. use PHPUnit\Framework\TestCase;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. class MaintenanceModeEnablerTest extends TestCase
  12. {
  13. /**
  14. * @dataProvider initialAppStateProvider
  15. */
  16. public function testSuccessfulTask(bool $maintenanceModeEnabledInitially)
  17. {
  18. $maintenanceMode = $this->createMaintenanceMode($maintenanceModeEnabledInitially);
  19. $enabler = new MaintenanceModeEnabler($maintenanceMode);
  20. $successTask = function () {
  21. // do nothing
  22. };
  23. $enabler->executeInMaintenanceMode(
  24. $successTask,
  25. $this->createOutput(),
  26. true
  27. );
  28. $this->assertEquals(
  29. $maintenanceModeEnabledInitially,
  30. $maintenanceMode->isOn(),
  31. 'Initial state is not restored'
  32. );
  33. }
  34. /**
  35. * @dataProvider initialAppStateProvider
  36. */
  37. public function testFailedTaskWithMaintenanceModeOnFailure(bool $maintenanceModeEnabledInitially)
  38. {
  39. $maintenanceMode = $this->createMaintenanceMode($maintenanceModeEnabledInitially);
  40. $enabler = new MaintenanceModeEnabler($maintenanceMode);
  41. $failedTask = function () {
  42. throw new \Exception('Woops!');
  43. };
  44. try {
  45. $enabler->executeInMaintenanceMode(
  46. $failedTask,
  47. $this->createOutput(),
  48. true
  49. );
  50. } catch (\Exception $e) {
  51. $this->assertEquals(
  52. true,
  53. $maintenanceMode->isOn(),
  54. 'Maintenance mode is not active after failure'
  55. );
  56. }
  57. }
  58. /**
  59. * @dataProvider initialAppStateProvider
  60. */
  61. public function testFailedTaskWithRestoredModeOnFailure(bool $maintenanceModeEnabledInitially)
  62. {
  63. $maintenanceMode = $this->createMaintenanceMode($maintenanceModeEnabledInitially);
  64. $enabler = new MaintenanceModeEnabler($maintenanceMode);
  65. $failedTask = function () {
  66. throw new \Exception('Woops!');
  67. };
  68. try {
  69. $enabler->executeInMaintenanceMode(
  70. $failedTask,
  71. $this->createOutput(),
  72. false
  73. );
  74. } catch (\Exception $e) {
  75. $this->assertEquals(
  76. $maintenanceModeEnabledInitially,
  77. $maintenanceMode->isOn(),
  78. 'Initial state is not restored'
  79. );
  80. }
  81. }
  82. /**
  83. * @return array
  84. */
  85. public function initialAppStateProvider()
  86. {
  87. return [
  88. 'Maintenance mode disabled initially' => [false],
  89. 'Maintenance mode enabled initially' => [true],
  90. ];
  91. }
  92. /**
  93. * @param bool $isOn
  94. * @return MaintenanceMode
  95. */
  96. private function createMaintenanceMode(bool $isOn): MaintenanceMode
  97. {
  98. $maintenanceMode = $this->getMockBuilder(MaintenanceMode::class)
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $maintenanceMode->method('isOn')->willReturnCallback(function () use (&$isOn) {
  102. return $isOn;
  103. });
  104. $maintenanceMode->method('set')->willReturnCallback(function ($newValue) use (&$isOn) {
  105. $isOn = (bool)$newValue;
  106. return true;
  107. });
  108. return $maintenanceMode;
  109. }
  110. /**
  111. * @return OutputInterface
  112. */
  113. private function createOutput(): OutputInterface
  114. {
  115. $output = $this->getMockBuilder(OutputInterface::class)
  116. ->getMockForAbstractClass();
  117. return $output;
  118. }
  119. }