Skip to content

Commit d71e45b

Browse files
author
Yuri Kovsher
committed
Merge remote-tracking branch 'tango-ce/MAGETWO-33062' into MAGETWO-33063
2 parents d7d4eb4 + c1fb3cb commit d71e45b

File tree

13 files changed

+700
-16
lines changed

13 files changed

+700
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Test\Tools\I18n\Parser\Adapter\Php\Tokenizer;
7+
8+
use Magento\Framework\ObjectManager;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\PhraseCollector;
11+
12+
/**
13+
* @covers \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\PhraseCollector
14+
*/
15+
class PhraseCollectorTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var PhraseCollector
19+
*/
20+
protected $phraseCollector;
21+
22+
/**
23+
* @var ObjectManager
24+
*/
25+
protected $objectManager;
26+
27+
protected function setUp()
28+
{
29+
$this->objectManager = Bootstrap::getObjectManager();
30+
$this->phraseCollector = $this->objectManager->create(
31+
'Magento\\Tools\\I18n\\Parser\\Adapter\\Php\\Tokenizer\\PhraseCollector'
32+
);
33+
}
34+
35+
/**
36+
* @covers \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\PhraseCollector::parse
37+
*/
38+
public function testParse()
39+
{
40+
$file = __DIR__.'/_files/objectsCode.php.txt';
41+
$this->phraseCollector->setIncludeObjects();
42+
$this->phraseCollector->parse($file);
43+
$expectation = [
44+
[
45+
'phrase' => '\'Testing\'',
46+
'arguments' => 0,
47+
'file' => $file,
48+
'line' => 3
49+
],
50+
[
51+
'phrase' => '\'More testing\'',
52+
'arguments' => 0,
53+
'file' => $file,
54+
'line' => 4
55+
]
56+
];
57+
$this->assertEquals($expectation, $this->phraseCollector->getPhrases());
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Test\Tools\I18n\Parser\Adapter\Php\Tokenizer\Translate;
7+
8+
use Magento\Framework\ObjectManager;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Translate\MethodCollector;
11+
12+
/**
13+
* @covers \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Translate\MethodCollector
14+
*/
15+
class MethodCollectorTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var MethodCollector
19+
*/
20+
protected $methodCollector;
21+
22+
/**
23+
* @var ObjectManager
24+
*/
25+
protected $objectManager;
26+
27+
protected function setUp()
28+
{
29+
$this->objectManager = Bootstrap::getObjectManager();
30+
$this->methodCollector = $this->objectManager->create(
31+
'Magento\\Tools\\I18n\\Parser\\Adapter\\Php\\Tokenizer\\Translate\\MethodCollector'
32+
);
33+
}
34+
35+
/**
36+
* @covers \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Translate\MethodCollector::parse
37+
*/
38+
public function testParse()
39+
{
40+
$file = __DIR__.'/../_files/methodsCode.php.txt';
41+
$this->methodCollector->parse($file);
42+
$expectation = [
43+
[
44+
'phrase' => '\'Some string\'',
45+
'arguments' => 0,
46+
'file' => $file,
47+
'line' => 4
48+
],
49+
[
50+
'phrase' => '\'One more string\'',
51+
'arguments' => 0,
52+
'file' => $file,
53+
'line' => 5
54+
]
55+
];
56+
$this->assertEquals($expectation, $this->methodCollector->getPhrases());
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$obj1->___toString('Irrelevant string');
4+
$obj2->__('Some string');
5+
$obj3->__('One more string');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
new \Magento\Framework\Phrase('Testing');
4+
new Phrase('More testing');
5+
new \Magento\Framework\Object();
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer;
7+
8+
use Magento\TestFramework\Helper\ObjectManager;
9+
use Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer;
10+
11+
/**
12+
* @covers \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\PhraseCollector
13+
*/
14+
class PhraseCollectorTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var PhraseCollector
18+
*/
19+
protected $phraseCollector;
20+
21+
/**
22+
* @var ObjectManager
23+
*/
24+
protected $objectManager;
25+
26+
/**
27+
* @var Tokenizer|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $tokenizerMock;
30+
31+
protected function setUp()
32+
{
33+
$this->objectManager = new ObjectManager($this);
34+
$this->tokenizerMock = $this->getMockBuilder('Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer')
35+
->disableOriginalConstructor()
36+
->getMock();
37+
$this->phraseCollector = $this->objectManager->getObject(
38+
'Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\PhraseCollector',
39+
[
40+
'tokenizer' => $this->tokenizerMock
41+
]
42+
);
43+
}
44+
45+
/**
46+
* @covers \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\PhraseCollector::parse
47+
*
48+
* @param string $file
49+
* @param array $isEndOfLoopReturnValues
50+
* @param array $getNextRealTokenReturnValues
51+
* @param array $getFunctionArgumentsTokensReturnValues
52+
* @param array $isMatchingClassReturnValues
53+
* @param array $result
54+
* @dataProvider testParseDataProvider
55+
*/
56+
public function testParse(
57+
$file,
58+
array $isEndOfLoopReturnValues,
59+
array $getNextRealTokenReturnValues,
60+
array $getFunctionArgumentsTokensReturnValues,
61+
array $isMatchingClassReturnValues,
62+
array $result
63+
) {
64+
$matchingClass = 'Phrase';
65+
66+
$this->tokenizerMock->expects($this->once())
67+
->method('parse')
68+
->with($file);
69+
$this->tokenizerMock->expects($this->atLeastOnce())
70+
->method('isEndOfLoop')
71+
->will(call_user_func_array(
72+
[$this, 'onConsecutiveCalls'],
73+
$isEndOfLoopReturnValues
74+
));
75+
$this->tokenizerMock->expects($this->any())
76+
->method('getNextRealToken')
77+
->will(call_user_func_array(
78+
[$this, 'onConsecutiveCalls'],
79+
$getNextRealTokenReturnValues
80+
));
81+
$this->tokenizerMock->expects($this->any())
82+
->method('getFunctionArgumentsTokens')
83+
->will(call_user_func_array(
84+
[$this, 'onConsecutiveCalls'],
85+
$getFunctionArgumentsTokensReturnValues
86+
));
87+
$this->tokenizerMock->expects($this->any())
88+
->method('isMatchingClass')
89+
->with($matchingClass)
90+
->will(call_user_func_array(
91+
[$this, 'onConsecutiveCalls'],
92+
$isMatchingClassReturnValues
93+
));
94+
95+
$this->phraseCollector->setIncludeObjects();
96+
$this->phraseCollector->parse($file);
97+
$this->assertEquals($result, $this->phraseCollector->getPhrases());
98+
}
99+
100+
/**
101+
* @return array
102+
*/
103+
public function testParseDataProvider()
104+
{
105+
$file = 'path/to/file.php';
106+
$line = 110;
107+
return [
108+
/* Test simulates parsing of the following code:
109+
*
110+
* $phrase1 = new \Magento\Framework\Phrase('Testing');
111+
* $phrase2 = __('More testing');
112+
*/
113+
'two phrases' => [
114+
'file' => $file,
115+
'isEndOfLoopReturnValues' => [
116+
false, //before $phrase1
117+
false, //at $phrase1
118+
false, //at =
119+
false, //at new
120+
false, //at ;
121+
false, //at $phrase2
122+
false, //at =
123+
false, //at __
124+
false, //at ;
125+
true //after ;
126+
],
127+
'getNextRealTokenReturnValues' => [
128+
$this->createToken(false, false, false, false, '$phrase1'),
129+
$this->createToken(false, false, false, false, '='),
130+
$this->createToken(false, false, true, false, 'new', $line),
131+
$this->createToken(false, false, false, false, ';'),
132+
$this->createToken(false, false, false, false, '$phrase2'),
133+
$this->createToken(false, false, false, false, '='),
134+
$this->createToken(true, false, false, false, '__', $line),
135+
$this->createToken(false, true, false, false, '('),
136+
$this->createToken(false, false, false, false, ';'),
137+
false
138+
],
139+
'getFunctionArgumentsTokensReturnValues' => [
140+
[[$this->createToken(false, false, false, true, '\'Testing\'')]], // 'Testing')
141+
[[$this->createToken(false, false, false, true, '\'More testing\'')]] // 'More testing')
142+
],
143+
'isMatchingClassReturnValues' => [
144+
true // \Magento\Framework\Phrase(
145+
],
146+
'result' => [
147+
[
148+
'phrase' => '\'Testing\'',
149+
'arguments' => 0,
150+
'file' => $file,
151+
'line' => $line
152+
],
153+
[
154+
'phrase' => '\'More testing\'',
155+
'arguments' => 0,
156+
'file' => $file,
157+
'line' => $line
158+
]
159+
]
160+
]
161+
];
162+
}
163+
164+
/**
165+
* @param bool $isEqualFunctionReturnValue
166+
* @param bool $isOpenBraceReturnValue
167+
* @param bool $isNewReturnValue
168+
* @param bool $isConstantEncapsedString
169+
* @param string $value
170+
* @param int|null $line
171+
* @return Token|\PHPUnit_Framework_MockObject_MockObject
172+
*/
173+
protected function createToken(
174+
$isEqualFunctionReturnValue,
175+
$isOpenBraceReturnValue,
176+
$isNewReturnValue,
177+
$isConstantEncapsedString,
178+
$value,
179+
$line = null
180+
) {
181+
$token = $this->getMockBuilder('Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Token')
182+
->disableOriginalConstructor()
183+
->getMock();
184+
$token->expects($this->any())
185+
->method('isEqualFunction')
186+
->with('__')
187+
->willReturn($isEqualFunctionReturnValue);
188+
$token->expects($this->any())
189+
->method('isOpenBrace')
190+
->willReturn($isOpenBraceReturnValue);
191+
$token->expects($this->any())
192+
->method('isNew')
193+
->willReturn($isNewReturnValue);
194+
$token->expects($this->any())
195+
->method('isConstantEncapsedString')
196+
->willReturn($isConstantEncapsedString);
197+
$token->expects($this->any())
198+
->method('getValue')
199+
->willReturn($value);
200+
$token->expects($this->any())
201+
->method('getLine')
202+
->willReturn($line);
203+
return $token;
204+
}
205+
}

0 commit comments

Comments
 (0)