Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ModeTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
20 use PHPUnit_Framework_MockObject_MockObject as Mock;
21 use Symfony\Component\Console\Input\InputInterface;
22 use Symfony\Component\Console\Output\OutputInterface;
25 
30 class ModeTest extends \PHPUnit\Framework\TestCase
31 {
35  private $model;
36 
40  private $readerMock;
41 
45  private $inputMock;
46 
50  private $outputMock;
51 
55  private $writerMock;
56 
60  private $maintenanceMock;
61 
65  private $filesystemMock;
66 
70  private $configProvider;
71 
75  private $processorFacadeFactory;
76 
80  private $processorFacade;
81 
85  private $emulatedAreaProcessor;
86 
87  protected function setUp()
88  {
89  $this->inputMock = $this->getMockBuilder(InputInterface::class)
90  ->getMockForAbstractClass();
91  $this->outputMock = $this->getMockBuilder(OutputInterface::class)
92  ->getMockForAbstractClass();
93  $this->writerMock = $this->getMockBuilder(Writer::class)
94  ->disableOriginalConstructor()
95  ->getMock();
96  $this->readerMock = $this->getMockBuilder(Reader::class)
97  ->disableOriginalConstructor()
98  ->getMock();
99  $this->maintenanceMock = $this->getMockBuilder(MaintenanceMode::class)
100  ->disableOriginalConstructor()
101  ->getMock();
102  $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105  $this->configProvider = $this->getMockBuilder(ConfigProvider::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108  $this->processorFacadeFactory = $this->getMockBuilder(ProcessorFacadeFactory::class)
109  ->disableOriginalConstructor()
110  ->setMethods(['create'])
111  ->getMockForAbstractClass();
112  $this->processorFacade = $this->getMockBuilder(ProcessorFacade::class)
113  ->disableOriginalConstructor()
114  ->getMock();
115  $this->emulatedAreaProcessor = $this->getMockBuilder(EmulatedAdminhtmlAreaProcessor::class)
116  ->disableOriginalConstructor()
117  ->getMock();
118 
119  $this->model = new Mode(
120  $this->inputMock,
121  $this->outputMock,
122  $this->writerMock,
123  $this->readerMock,
124  $this->maintenanceMock,
125  $this->filesystemMock,
126  $this->configProvider,
127  $this->processorFacadeFactory,
128  $this->emulatedAreaProcessor,
129  new MaintenanceModeEnabler($this->maintenanceMock)
130  );
131  }
132 
133  public function testGetMode()
134  {
135  $this->readerMock->expects($this->exactly(2))
136  ->method('load')
137  ->willReturnOnConsecutiveCalls(
138  [],
140  );
141 
142  $this->assertSame(null, $this->model->getMode());
143  $this->assertSame(State::MODE_DEVELOPER, $this->model->getMode());
144  }
145 
150  public function testEnableProductionMode()
151  {
153  $modeModel = $this->model;
154  $dataStorage = [
157  ],
158  ];
159  $this->configProvider->expects($this->any())
160  ->method('getConfigs')
161  ->willReturn([]);
162  $this->writerMock->expects($this->once())
163  ->method("saveConfig")
164  ->willReturnCallback(function ($data) use (&$dataStorage) {
165  $dataStorage = $data;
166  });
167  $this->readerMock->expects($this->any())
168  ->method('load')
169  ->willReturnCallback(function () use (&$dataStorage) {
170  return $dataStorage[ConfigFilePool::APP_ENV];
171  });
172  $this->filesystemMock->expects($this->once())
173  ->method("regenerateStatic")
174  ->willReturnCallback(function () use (&$modeModel, &$mode) {
175  $mode = $modeModel->getMode();
176  });
177  $this->model->enableProductionMode();
178  $this->assertEquals(State::MODE_PRODUCTION, $mode);
179  }
180 
188  {
190  $dataStorage = [
193  ],
194  ];
195  $this->readerMock->expects($this->any())
196  ->method('load')
197  ->willReturn([State::PARAM_MODE => State::MODE_DEVELOPER]);
198  $this->configProvider->expects($this->any())
199  ->method('getConfigs')
200  ->willReturn([]);
201  $this->writerMock->expects($this->exactly(2))
202  ->method("saveConfig")
203  ->withConsecutive(
206  )
207  ->willReturnCallback(function ($data) use (&$dataStorage) {
208  $dataStorage = $data;
209  });
210  $this->readerMock->expects($this->any())
211  ->method('load')
212  ->willReturnCallback(function () use (&$dataStorage) {
213  return $dataStorage[ConfigFilePool::APP_ENV];
214  });
215  $this->filesystemMock->expects($this->once())
216  ->method("regenerateStatic")
217  ->willThrowException(new LocalizedException(__('Exception')));
218  $this->model->enableProductionMode();
219  $this->assertEquals(State::MODE_PRODUCTION, $mode);
220  }
221 
223  {
224  $this->readerMock->expects($this->once())
225  ->method('load')
226  ->willReturn([State::PARAM_MODE => State::MODE_DEVELOPER]);
227  $this->configProvider->expects($this->once())
228  ->method('getConfigs')
229  ->with('developer', 'production')
230  ->willReturn([
231  'dev/debug/debug_logging' => 0,
232  ]);
233  $this->emulatedAreaProcessor->expects($this->once())
234  ->method('process')
235  ->willReturnCallback(function (\Closure $closure) {
236  return $closure->call($this->model);
237  });
238 
239  $this->processorFacadeFactory->expects($this->once())
240  ->method('create')
241  ->willReturn($this->processorFacade);
242  $this->processorFacade
243  ->expects($this->once())
244  ->method('processWithLockTarget')
245  ->with(
246  'dev/debug/debug_logging',
247  0,
249  null,
250  true
251  );
252  $this->outputMock->expects($this->once())
253  ->method('writeln')
254  ->with('Config "dev/debug/debug_logging = 0" has been saved.');
255 
256  $this->model->enableProductionModeMinimal();
257  }
258 }
__()
Definition: __.php:13
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15