Skip to content

Commit 4de3aa6

Browse files
author
Mykola Palamar
committed
MAGETWO-70869: Console errors after turning on CSS merging/minification
1 parent 54998a2 commit 4de3aa6

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

app/code/Magento/Deploy/Model/Mode.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\Config\File\ConfigFilePool;
1515
use Symfony\Component\Console\Input\InputInterface;
1616
use Symfony\Component\Console\Output\OutputInterface;
17+
use Magento\Framework\Exception\LocalizedException;
1718

1819
/**
1920
* A class to manage Magento modes
@@ -75,13 +76,23 @@ public function __construct(
7576
/**
7677
* Enable production mode
7778
*
79+
* @throws LocalizedException
7880
* @return void
7981
*/
8082
public function enableProductionMode()
8183
{
8284
$this->enableMaintenanceMode($this->output);
83-
$this->filesystem->regenerateStatic($this->output);
84-
$this->setStoreMode(State::MODE_PRODUCTION);
85+
$previousMode = $this->getMode();
86+
try {
87+
// We have to turn on production mode before generation.
88+
// We need this to enable generation of the "min" files.
89+
$this->setStoreMode(State::MODE_PRODUCTION);
90+
$this->filesystem->regenerateStatic($this->output);
91+
} catch (LocalizedException $e) {
92+
// We have to return store mode to previous state in case of error.
93+
$this->setStoreMode($previousMode);
94+
throw $e;
95+
}
8596
$this->disableMaintenanceMode($this->output);
8697
}
8798

app/code/Magento/Deploy/Test/Unit/Model/ModeTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use PHPUnit_Framework_MockObject_MockObject as Mock;
1515
use Symfony\Component\Console\Input\InputInterface;
1616
use Symfony\Component\Console\Output\OutputInterface;
17+
use Magento\Framework\Config\File\ConfigFilePool;
18+
use Magento\Framework\Exception\LocalizedException;
1719

1820
/**
1921
* @inheritdoc
@@ -96,4 +98,72 @@ public function testGetMode()
9698
$this->assertSame(null, $this->model->getMode());
9799
$this->assertSame(State::MODE_DEVELOPER, $this->model->getMode());
98100
}
101+
102+
/**
103+
* Test that production mode will be enabled before static generation call.
104+
* We need this to be sure that "min" files will be generated.
105+
*/
106+
public function testEnableProductionMode()
107+
{
108+
$mode = State::MODE_DEVELOPER;
109+
$modeModel = $this->model;
110+
$dataStorage = [
111+
ConfigFilePool::APP_ENV => [
112+
State::PARAM_MODE => State::MODE_DEVELOPER,
113+
],
114+
];
115+
$this->writerMock->expects($this->once())
116+
->method("saveConfig")
117+
->willReturnCallback(function($data) use (&$dataStorage){
118+
$dataStorage = $data;
119+
});
120+
$this->readerMock->expects($this->any())
121+
->method('load')
122+
->willReturnCallback(function() use (&$dataStorage){
123+
return $dataStorage[ConfigFilePool::APP_ENV];
124+
});
125+
$this->filesystemMock->expects($this->once())
126+
->method("regenerateStatic")
127+
->willReturnCallback(function() use (&$modeModel, &$mode){
128+
$mode = $modeModel->getMode();
129+
});
130+
$this->model->enableProductionMode();
131+
$this->assertEquals(State::MODE_PRODUCTION, $mode);
132+
}
133+
134+
/**
135+
* Test that previous mode will be enabled after error during static generation call.
136+
* We need this to be sure that mode will be reverted to it previous tate.
137+
*
138+
* @expectedException \Magento\Framework\Exception\LocalizedException
139+
*/
140+
public function testEnableDeveloperModeOnFail()
141+
{
142+
$mode = State::MODE_DEVELOPER;
143+
$modeModel = $this->model;
144+
$dataStorage = [
145+
ConfigFilePool::APP_ENV => [
146+
State::PARAM_MODE => State::MODE_DEVELOPER,
147+
],
148+
];
149+
$this->writerMock->expects($this->exactly(2))
150+
->method("saveConfig")
151+
->withConsecutive(
152+
[$this->equalTo([ConfigFilePool::APP_ENV => [State::PARAM_MODE => State::MODE_PRODUCTION]])],
153+
[$this->equalTo([ConfigFilePool::APP_ENV => [State::PARAM_MODE => State::MODE_DEVELOPER]])]
154+
)
155+
->willReturnCallback(function($data) use (&$dataStorage){
156+
$dataStorage = $data;
157+
});
158+
$this->readerMock->expects($this->any())
159+
->method('load')
160+
->willReturnCallback(function() use (&$dataStorage){
161+
return $dataStorage[ConfigFilePool::APP_ENV];
162+
});
163+
$this->filesystemMock->expects($this->once())
164+
->method("regenerateStatic")
165+
->willThrowException(new LocalizedException(__('Exception')));
166+
$this->model->enableProductionMode();
167+
$this->assertEquals(State::MODE_PRODUCTION, $mode);
168+
}
99169
}

0 commit comments

Comments
 (0)