Skip to content

Commit a5baad4

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-59003' into pr-develop
2 parents 5c5c43e + a3a1206 commit a5baad4

File tree

7 files changed

+160
-168
lines changed

7 files changed

+160
-168
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\App\View\Deployment;
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\App\State;
11+
use Magento\Framework\App\View\Deployment\Version\Storage\File;
12+
use Magento\Framework\Filesystem\Directory\WriteInterface;
13+
14+
class VersionTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var File
18+
*/
19+
private $fileStorage;
20+
21+
/**
22+
* @var WriteInterface
23+
*/
24+
private $directoryWrite;
25+
26+
/**
27+
* @var string
28+
*/
29+
private $fileName = 'deployed_version.txt';
30+
31+
public function setUp()
32+
{
33+
$this->fileStorage = ObjectManager::getInstance()->create(
34+
File::class,
35+
[
36+
'directoryCode' => DirectoryList::STATIC_VIEW,
37+
'fileName' => $this->fileName
38+
]
39+
);
40+
/** @var \Magento\TestFramework\App\Filesystem $filesystem */
41+
$filesystem = ObjectManager::getInstance()->get(\Magento\TestFramework\App\Filesystem::class);
42+
$this->directoryWrite = $filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
43+
$this->removeDeployVersionFile();
44+
}
45+
46+
/**
47+
* @param string $mode
48+
* @return Version
49+
*/
50+
public function getVersionModel($mode)
51+
{
52+
$appState = ObjectManager::getInstance()->create(
53+
State::class,
54+
[
55+
'mode' => $mode
56+
]
57+
);
58+
return ObjectManager::getInstance()->create(
59+
Version::class,
60+
[
61+
'appState' => $appState
62+
]
63+
);
64+
}
65+
66+
protected function tearDown()
67+
{
68+
$this->removeDeployVersionFile();
69+
}
70+
71+
private function removeDeployVersionFile()
72+
{
73+
if ($this->directoryWrite->isExist($this->fileName)) {
74+
$this->directoryWrite->delete($this->fileName);
75+
}
76+
}
77+
78+
/**
79+
* @expectedException \UnexpectedValueException
80+
*/
81+
public function testGetValueInProductionModeWithoutVersion()
82+
{
83+
$this->assertFalse($this->directoryWrite->isExist($this->fileName));
84+
$this->getVersionModel(State::MODE_PRODUCTION)->getValue();
85+
}
86+
87+
public function testGetValueInDeveloperMode()
88+
{
89+
$this->assertFalse($this->directoryWrite->isExist($this->fileName));
90+
$this->getVersionModel(State::MODE_DEVELOPER)->getValue();
91+
$this->assertTrue($this->directoryWrite->isExist($this->fileName));
92+
}
93+
94+
/**
95+
* Assert that version is not regenerated on each request in developer mode
96+
*/
97+
public function testGetValue()
98+
{
99+
$this->assertFalse($this->directoryWrite->isExist($this->fileName));
100+
$versionModel = $this->getVersionModel(State::MODE_DEVELOPER);
101+
$version = $versionModel->getValue();
102+
$this->assertTrue($this->directoryWrite->isExist($this->fileName));
103+
$this->assertEquals($version, $versionModel->getValue());
104+
}
105+
}

dev/tools/grunt/configs/clean.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ _.each(themes, function(theme, name) {
2121
"<%= path.tmp %>/cache/**/*",
2222
"<%= combo.autopath(\""+name+"\", path.pub ) %>**/*",
2323
"<%= combo.autopath(\""+name+"\", path.tmpLess) %>**/*",
24-
"<%= combo.autopath(\""+name+"\", path.tmpSource) %>**/*"
24+
"<%= combo.autopath(\""+name+"\", path.tmpSource) %>**/*",
25+
"<%= path.deployedVersion %>"
2526
]
2627
}
2728
]
@@ -56,7 +57,8 @@ var cleanOptions = {
5657
"dot": true,
5758
"src": [
5859
"<%= path.pub %>frontend/**/*",
59-
"<%= path.pub %>adminhtml/**/*"
60+
"<%= path.pub %>adminhtml/**/*",
61+
"<%= path.deployedVersion %>"
6062
]
6163
}
6264
]
@@ -73,7 +75,8 @@ var cleanOptions = {
7375
"<%= path.pub %>frontend/**/*.less",
7476
"<%= path.pub %>frontend/**/*.css",
7577
"<%= path.pub %>adminhtml/**/*.less",
76-
"<%= path.pub %>adminhtml/**/*.css"
78+
"<%= path.pub %>adminhtml/**/*.css",
79+
"<%= path.deployedVersion %>"
7780
]
7881
}
7982
]
@@ -102,7 +105,8 @@ var cleanOptions = {
102105
"src": [
103106
"<%= path.pub %>**/*.js",
104107
"<%= path.pub %>**/*.html",
105-
"<%= path.pub %>_requirejs/**/*"
108+
"<%= path.pub %>_requirejs/**/*",
109+
"<%= path.deployedVersion %>"
106110
]
107111
}
108112
]

dev/tools/grunt/configs/path.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
tmpLess: 'var/view_preprocessed/less/',
1414
tmpSource: 'var/view_preprocessed/source/',
1515
tmp: 'var',
16+
deployedVersion: 'pub/static/deployed_version.txt',
1617
css: {
1718
setup: 'setup/pub/styles',
1819
updater: '../magento2-updater/pub/css'

lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/Version/Storage/FileTest.php

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,48 +34,15 @@ protected function setUp()
3434

3535
public function testLoad()
3636
{
37-
$this->directory
38-
->expects($this->once())
39-
->method('readFile')
37+
$this->directory->expects($this->once())
38+
->method('isReadable')
4039
->with('fixture_file.txt')
41-
->will($this->returnValue('123'));
42-
$this->assertEquals('123', $this->object->load());
43-
}
44-
45-
/**
46-
* @expectedException \Exception
47-
* @expectedExceptionMessage Exception to be propagated
48-
*/
49-
public function testLoadExceptionPropagation()
50-
{
51-
$this->directory
52-
->expects($this->once())
40+
->willReturn(true);
41+
$this->directory->expects($this->once())
5342
->method('readFile')
5443
->with('fixture_file.txt')
55-
->will($this->throwException(new \Exception('Exception to be propagated')));
56-
$this->object->load();
57-
}
58-
59-
/**
60-
* @expectedException \UnexpectedValueException
61-
* @expectedExceptionMessage Unable to retrieve deployment version of static files from the file system
62-
*/
63-
public function testLoadExceptionWrapping()
64-
{
65-
$filesystemException = new \Magento\Framework\Exception\FileSystemException(
66-
new \Magento\Framework\Phrase('File does not exist')
67-
);
68-
$this->directory
69-
->expects($this->once())
70-
->method('readFile')
71-
->with('fixture_file.txt')
72-
->will($this->throwException($filesystemException));
73-
try {
74-
$this->object->load();
75-
} catch (\Exception $e) {
76-
$this->assertSame($filesystemException, $e->getPrevious(), 'Wrapping of original exception is expected');
77-
throw $e;
78-
}
44+
->willReturn('123');
45+
$this->assertEquals('123', $this->object->load());
7946
}
8047

8148
public function testSave()

lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/VersionTest.php

Lines changed: 29 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\Framework\App\Test\Unit\View\Deployment;
77

88
use Magento\Framework\App\View\Deployment\Version;
9-
use Magento\Framework\Exception\FileSystemException;
109

1110
/**
1211
* Class VersionTest
@@ -45,17 +44,6 @@ protected function setUp()
4544
$objectManager->setBackwardCompatibleProperty($this->object, 'logger', $this->loggerMock);
4645
}
4746

48-
public function testGetValueDeveloperMode()
49-
{
50-
$this->appStateMock
51-
->expects($this->once())
52-
->method('getMode')
53-
->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
54-
$this->versionStorageMock->expects($this->never())->method($this->anything());
55-
$this->assertInternalType('integer', $this->object->getValue());
56-
$this->object->getValue(); // Ensure computation occurs only once and result is cached in memory
57-
}
58-
5947
/**
6048
* @param string $appMode
6149
* @dataProvider getValueFromStorageDataProvider
@@ -81,106 +69,46 @@ public function getValueFromStorageDataProvider()
8169
];
8270
}
8371

84-
/**
85-
* $param bool $isUnexpectedValueExceptionThrown
86-
* $param bool $isFileSystemExceptionThrown
87-
* @dataProvider getValueDefaultModeDataProvider
88-
*/
89-
public function testGetValueDefaultMode(
90-
$isUnexpectedValueExceptionThrown,
91-
$isFileSystemExceptionThrown = null
92-
) {
93-
$versionType = 'integer';
94-
$this->appStateMock
95-
->expects($this->once())
96-
->method('getMode')
97-
->willReturn(\Magento\Framework\App\State::MODE_DEFAULT);
98-
if ($isUnexpectedValueExceptionThrown) {
99-
$storageException = new \UnexpectedValueException('Does not exist in the storage');
100-
$this->versionStorageMock
101-
->expects($this->once())
102-
->method('load')
103-
->will($this->throwException($storageException));
104-
$this->versionStorageMock->expects($this->once())
105-
->method('save')
106-
->with($this->isType($versionType));
107-
if ($isFileSystemExceptionThrown) {
108-
$fileSystemException = new FileSystemException(
109-
new \Magento\Framework\Phrase('Can not load static content version')
110-
);
111-
$this->versionStorageMock
112-
->expects($this->once())
113-
->method('save')
114-
->will($this->throwException($fileSystemException));
115-
$this->loggerMock->expects($this->once())
116-
->method('critical')
117-
->with('Can not save static content version.');
118-
} else {
119-
$this->loggerMock->expects($this->never())
120-
->method('critical');
121-
}
122-
} else {
123-
$this->versionStorageMock
124-
->expects($this->once())
125-
->method('load')
126-
->willReturn(1475779229);
127-
$this->loggerMock->expects($this->never())
128-
->method('critical');
129-
}
130-
$this->assertInternalType($versionType, $this->object->getValue());
72+
public function testGetValueInNonProductionMode()
73+
{
74+
$version = 123123123123;
75+
$this->versionStorageMock->expects($this->once())
76+
->method('load')
77+
->willReturn($version);
78+
79+
$this->assertEquals($version, $this->object->getValue());
13180
$this->object->getValue();
13281
}
13382

13483
/**
135-
* @return array
84+
* @expectedException \UnexpectedValueException
13685
*/
137-
public function getValueDefaultModeDataProvider()
86+
public function testGetValueWithProductionModeAndException()
13887
{
139-
return [
140-
[false],
141-
[true, false],
142-
[true, true]
143-
];
144-
}
145-
146-
/**
147-
* @param bool $isUnexpectedValueExceptionThrown
148-
* @dataProvider getValueProductionModeDataProvider
149-
*/
150-
public function testGetValueProductionMode(
151-
$isUnexpectedValueExceptionThrown
152-
) {
153-
$this->appStateMock
154-
->expects($this->once())
88+
$this->versionStorageMock->expects($this->once())
89+
->method('load')
90+
->willReturn(false);
91+
$this->appStateMock->expects($this->once())
15592
->method('getMode')
15693
->willReturn(\Magento\Framework\App\State::MODE_PRODUCTION);
157-
if ($isUnexpectedValueExceptionThrown) {
158-
$storageException = new \UnexpectedValueException('Does not exist in the storage');
159-
$this->versionStorageMock
160-
->expects($this->once())
161-
->method('load')
162-
->will($this->throwException($storageException));
163-
$this->loggerMock->expects($this->once())
164-
->method('critical')
165-
->with('Can not load static content version.');
166-
} else {
167-
$this->versionStorageMock
168-
->expects($this->once())
169-
->method('load')
170-
->willReturn(1475779229);
171-
}
172-
$this->assertInternalType('integer', $this->object->getValue());
94+
$this->loggerMock->expects($this->once())
95+
->method('critical')
96+
->with('Can not load static content version.');
97+
17398
$this->object->getValue();
17499
}
175100

176-
/**
177-
* @return array
178-
*/
179-
public function getValueProductionModeDataProvider()
101+
public function testGetValueWithProductionMode()
180102
{
181-
return [
182-
[false],
183-
[true],
184-
];
103+
$this->versionStorageMock->expects($this->once())
104+
->method('load')
105+
->willReturn(false);
106+
$this->appStateMock->expects($this->once())
107+
->method('getMode')
108+
->willReturn(\Magento\Framework\App\State::MODE_DEFAULT);
109+
$this->versionStorageMock->expects($this->once())
110+
->method('save');
111+
112+
$this->assertNotNull($this->object->getValue());
185113
}
186114
}

0 commit comments

Comments
 (0)