123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Deploy\Test\Unit\Model;
- use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
- use Magento\Config\Console\Command\ConfigSet\ProcessorFacade;
- use Magento\Config\Console\Command\EmulatedAdminhtmlAreaProcessor;
- use Magento\Deploy\App\Mode\ConfigProvider;
- use Magento\Deploy\Model\Filesystem;
- use Magento\Deploy\Model\Mode;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Framework\App\Console\MaintenanceModeEnabler;
- use Magento\Framework\App\DeploymentConfig\Reader;
- use Magento\Framework\App\DeploymentConfig\Writer;
- use Magento\Framework\App\MaintenanceMode;
- use Magento\Framework\App\State;
- use PHPUnit_Framework_MockObject_MockObject as Mock;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Magento\Framework\Config\File\ConfigFilePool;
- use Magento\Framework\Exception\LocalizedException;
- /**
- * @inheritdoc
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class ModeTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Mode
- */
- private $model;
- /**
- * @var Reader|Mock
- */
- private $readerMock;
- /**
- * @var InputInterface|Mock
- */
- private $inputMock;
- /**
- * @var OutputInterface|Mock
- */
- private $outputMock;
- /**
- * @var Writer|Mock
- */
- private $writerMock;
- /**
- * @var MaintenanceMode|Mock
- */
- private $maintenanceMock;
- /**
- * @var Filesystem|Mock
- */
- private $filesystemMock;
- /**
- * @var ConfigProvider|Mock
- */
- private $configProvider;
- /**
- * @var ProcessorFacadeFactory|Mock
- */
- private $processorFacadeFactory;
- /**
- * @var ProcessorFacade|Mock
- */
- private $processorFacade;
- /**
- * @var EmulatedAdminhtmlAreaProcessor|Mock
- */
- private $emulatedAreaProcessor;
- protected function setUp()
- {
- $this->inputMock = $this->getMockBuilder(InputInterface::class)
- ->getMockForAbstractClass();
- $this->outputMock = $this->getMockBuilder(OutputInterface::class)
- ->getMockForAbstractClass();
- $this->writerMock = $this->getMockBuilder(Writer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->readerMock = $this->getMockBuilder(Reader::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->maintenanceMock = $this->getMockBuilder(MaintenanceMode::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->configProvider = $this->getMockBuilder(ConfigProvider::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->processorFacadeFactory = $this->getMockBuilder(ProcessorFacadeFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMockForAbstractClass();
- $this->processorFacade = $this->getMockBuilder(ProcessorFacade::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->emulatedAreaProcessor = $this->getMockBuilder(EmulatedAdminhtmlAreaProcessor::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->model = new Mode(
- $this->inputMock,
- $this->outputMock,
- $this->writerMock,
- $this->readerMock,
- $this->maintenanceMock,
- $this->filesystemMock,
- $this->configProvider,
- $this->processorFacadeFactory,
- $this->emulatedAreaProcessor,
- new MaintenanceModeEnabler($this->maintenanceMock)
- );
- }
- public function testGetMode()
- {
- $this->readerMock->expects($this->exactly(2))
- ->method('load')
- ->willReturnOnConsecutiveCalls(
- [],
- [State::PARAM_MODE => State::MODE_DEVELOPER]
- );
- $this->assertSame(null, $this->model->getMode());
- $this->assertSame(State::MODE_DEVELOPER, $this->model->getMode());
- }
- /**
- * Test that production mode will be enabled before static generation call.
- * We need this to be sure that "min" files will be generated.
- */
- public function testEnableProductionMode()
- {
- $mode = State::MODE_DEVELOPER;
- $modeModel = $this->model;
- $dataStorage = [
- ConfigFilePool::APP_ENV => [
- State::PARAM_MODE => State::MODE_DEVELOPER,
- ],
- ];
- $this->configProvider->expects($this->any())
- ->method('getConfigs')
- ->willReturn([]);
- $this->writerMock->expects($this->once())
- ->method("saveConfig")
- ->willReturnCallback(function ($data) use (&$dataStorage) {
- $dataStorage = $data;
- });
- $this->readerMock->expects($this->any())
- ->method('load')
- ->willReturnCallback(function () use (&$dataStorage) {
- return $dataStorage[ConfigFilePool::APP_ENV];
- });
- $this->filesystemMock->expects($this->once())
- ->method("regenerateStatic")
- ->willReturnCallback(function () use (&$modeModel, &$mode) {
- $mode = $modeModel->getMode();
- });
- $this->model->enableProductionMode();
- $this->assertEquals(State::MODE_PRODUCTION, $mode);
- }
- /**
- * Test that previous mode will be enabled after error during static generation call.
- * We need this to be sure that mode will be reverted to it previous tate.
- *
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testEnableDeveloperModeOnFail()
- {
- $mode = State::MODE_DEVELOPER;
- $dataStorage = [
- ConfigFilePool::APP_ENV => [
- State::PARAM_MODE => State::MODE_DEVELOPER,
- ],
- ];
- $this->readerMock->expects($this->any())
- ->method('load')
- ->willReturn([State::PARAM_MODE => State::MODE_DEVELOPER]);
- $this->configProvider->expects($this->any())
- ->method('getConfigs')
- ->willReturn([]);
- $this->writerMock->expects($this->exactly(2))
- ->method("saveConfig")
- ->withConsecutive(
- [$this->equalTo([ConfigFilePool::APP_ENV => [State::PARAM_MODE => State::MODE_PRODUCTION]])],
- [$this->equalTo([ConfigFilePool::APP_ENV => [State::PARAM_MODE => State::MODE_DEVELOPER]])]
- )
- ->willReturnCallback(function ($data) use (&$dataStorage) {
- $dataStorage = $data;
- });
- $this->readerMock->expects($this->any())
- ->method('load')
- ->willReturnCallback(function () use (&$dataStorage) {
- return $dataStorage[ConfigFilePool::APP_ENV];
- });
- $this->filesystemMock->expects($this->once())
- ->method("regenerateStatic")
- ->willThrowException(new LocalizedException(__('Exception')));
- $this->model->enableProductionMode();
- $this->assertEquals(State::MODE_PRODUCTION, $mode);
- }
- public function testEnableProductionModeMinimal()
- {
- $this->readerMock->expects($this->once())
- ->method('load')
- ->willReturn([State::PARAM_MODE => State::MODE_DEVELOPER]);
- $this->configProvider->expects($this->once())
- ->method('getConfigs')
- ->with('developer', 'production')
- ->willReturn([
- 'dev/debug/debug_logging' => 0,
- ]);
- $this->emulatedAreaProcessor->expects($this->once())
- ->method('process')
- ->willReturnCallback(function (\Closure $closure) {
- return $closure->call($this->model);
- });
- $this->processorFacadeFactory->expects($this->once())
- ->method('create')
- ->willReturn($this->processorFacade);
- $this->processorFacade
- ->expects($this->once())
- ->method('processWithLockTarget')
- ->with(
- 'dev/debug/debug_logging',
- 0,
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
- null,
- true
- );
- $this->outputMock->expects($this->once())
- ->method('writeln')
- ->with('Config "dev/debug/debug_logging = 0" has been saved.');
- $this->model->enableProductionModeMinimal();
- }
- }
|