Skip to content

Commit 3a5c5c2

Browse files
author
Bohdan Korablov
committed
MAGETWO-57820: [GITHUB] php bin/magento i18n:pack creates unwanted dir #6260
1 parent 59f7e96 commit 3a5c5c2

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Setup\Test\Unit\Module\I18n\Pack\Writer\File;
7+
8+
use Magento\Setup\Module\I18n\Context;
9+
use Magento\Setup\Module\I18n\Locale;
10+
use Magento\Setup\Module\I18n\Dictionary;
11+
use Magento\Setup\Module\I18n\Dictionary\Phrase;
12+
use Magento\Setup\Module\I18n\Pack\Writer\File\AbstractFile;
13+
use Magento\Setup\Module\I18n\UnregisteredComponentException;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
15+
16+
class AbstractFileTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
protected $contextMock;
22+
23+
/**
24+
* @var Locale|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $localeMock;
27+
28+
/**
29+
* @var Dictionary|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $dictionaryMock;
32+
33+
/**
34+
* @var Phrase|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $phraseMock;
37+
38+
/**
39+
* @var AbstractFile|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
protected $object;
42+
43+
/**
44+
* @return void
45+
*/
46+
protected function setUp()
47+
{
48+
/** @var ObjectManagerHelper $objectManagerHelper */
49+
$objectManagerHelper = new ObjectManagerHelper($this);
50+
51+
$this->contextMock = $this->getMock(Context::class, [], [], '', false, false);
52+
$this->localeMock = $this->getMock(Locale::class, [], [], '', false, false);
53+
$this->dictionaryMock = $this->getMock(Dictionary::class, [], [], '', false, false);
54+
$this->phraseMock = $this->getMock(Phrase::class, [], [], '', false, false);
55+
56+
$constructorArguments = $objectManagerHelper->getConstructArguments(
57+
AbstractFile::class,
58+
['context' => $this->contextMock]
59+
);
60+
$this->object = $this->getMockBuilder(AbstractFile::class)
61+
->setMethods(['_createDirectoryIfNotExist', '_writeFile'])
62+
->setConstructorArgs($constructorArguments)
63+
->getMockForAbstractClass();
64+
}
65+
66+
/**
67+
* @param string $contextType
68+
* @param array $contextValue
69+
* @dataProvider writeDictionaryWithRuntimeExceptionDataProvider
70+
* @expectedException \RuntimeException
71+
* @return void
72+
*/
73+
public function testWriteDictionaryWithRuntimeException($contextType, $contextValue)
74+
{
75+
$this->configureGeneralPhrasesMock($contextType, $contextValue);
76+
77+
$this->object->expects($this->never())
78+
->method('_createDirectoryIfNotExist');
79+
$this->object->expects($this->never())
80+
->method('_writeFile');
81+
82+
$this->object->writeDictionary($this->dictionaryMock, $this->localeMock);
83+
}
84+
85+
/**
86+
* @return array
87+
*/
88+
public function writeDictionaryWithRuntimeExceptionDataProvider()
89+
{
90+
return [
91+
['', []],
92+
['module', []],
93+
['', ['Magento_Module']]
94+
];
95+
}
96+
97+
/**
98+
* @expectedException \InvalidArgumentException
99+
* @expectedExceptionMessage Some error. Row #1.
100+
* @return void
101+
*/
102+
public function testWriteDictionaryWithInvalidArgumentException()
103+
{
104+
$contextType = 'module';
105+
$contextValue = 'Magento_Module';
106+
107+
$this->configureGeneralPhrasesMock($contextType, [$contextValue]);
108+
109+
$this->object->expects($this->never())
110+
->method('_createDirectoryIfNotExist');
111+
$this->object->expects($this->never())
112+
->method('_writeFile');
113+
114+
$this->contextMock->expects($this->once())
115+
->method('buildPathToLocaleDirectoryByContext')
116+
->with($contextType, $contextValue)
117+
->willThrowException(new \InvalidArgumentException('Some error.'));
118+
119+
$this->object->writeDictionary($this->dictionaryMock, $this->localeMock);
120+
}
121+
122+
/**
123+
* @return void
124+
*/
125+
public function testWriteDictionaryWithUnregisteredComponentException()
126+
{
127+
$contextType = 'module';
128+
$contextValue = 'Magento_Module';
129+
130+
$this->configureGeneralPhrasesMock($contextType, [$contextValue]);
131+
132+
$this->object->expects($this->never())
133+
->method('_createDirectoryIfNotExist');
134+
$this->object->expects($this->never())
135+
->method('_writeFile');
136+
137+
$this->contextMock->expects($this->once())
138+
->method('buildPathToLocaleDirectoryByContext')
139+
->with($contextType, $contextValue)
140+
->willThrowException(new UnregisteredComponentException);
141+
142+
$this->object->writeDictionary($this->dictionaryMock, $this->localeMock);
143+
}
144+
145+
/**
146+
* @return void
147+
*/
148+
public function testWriteDictionary()
149+
{
150+
$contextType = 'module';
151+
$contextValue = 'Magento_Module';
152+
$path = '/some/path/';
153+
$phrase = 'Phrase';
154+
$locale = 'en_EN';
155+
$fileExtension = 'csv';
156+
$file = $path . $locale . '.' . $fileExtension;
157+
158+
$this->configureGeneralPhrasesMock($contextType, [$contextValue]);
159+
160+
$this->phraseMock->expects($this->once())
161+
->method('getPhrase')
162+
->willReturn($phrase);
163+
$this->localeMock->expects($this->once())
164+
->method('__toString')
165+
->willReturn($locale);
166+
167+
$this->object->expects($this->once())
168+
->method('_getFileExtension')
169+
->willReturn($fileExtension);
170+
$this->object->expects($this->once())
171+
->method('_createDirectoryIfNotExist')
172+
->with(dirname($file));
173+
$this->object->expects($this->once())
174+
->method('_writeFile')
175+
->with($file, [$phrase => $this->phraseMock]);
176+
177+
$this->contextMock->expects($this->once())
178+
->method('buildPathToLocaleDirectoryByContext')
179+
->with($contextType, $contextValue)
180+
->willReturn($path);
181+
182+
$this->object->writeDictionary($this->dictionaryMock, $this->localeMock);
183+
}
184+
185+
/**
186+
* @param string $contextType
187+
* @param array $contextValue
188+
* @return void
189+
*/
190+
protected function configureGeneralPhrasesMock($contextType, $contextValue)
191+
{
192+
$this->phraseMock->expects($this->any())
193+
->method('getContextType')
194+
->willReturn($contextType);
195+
$this->phraseMock->expects($this->any())
196+
->method('getContextValue')
197+
->willReturn($contextValue);
198+
199+
$this->dictionaryMock->expects($this->once())
200+
->method('getPhrases')
201+
->willReturn([$this->phraseMock]);
202+
}
203+
}

0 commit comments

Comments
 (0)