Skip to content

Commit 740293b

Browse files
author
Yuri Kovsher
committed
MAGETWO-33062: Update dictionary search tool to support \Magento\Framework\Phrase
1 parent 926d4ba commit 740293b

File tree

5 files changed

+427
-8
lines changed

5 files changed

+427
-8
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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->parse($file);
96+
$this->assertEquals($result, $this->phraseCollector->getPhrases());
97+
}
98+
99+
/**
100+
* @return array
101+
*/
102+
public function testParseDataProvider()
103+
{
104+
$file = 'path/to/file.php';
105+
$line = 110;
106+
return [
107+
/* Test simulates parsing of the following code:
108+
*
109+
* $phrase1 = new \Magento\Framework\Phrase('Testing');
110+
* $phrase2 = __('More testing');
111+
*/
112+
'two phrases' => [
113+
'file' => $file,
114+
'isEndOfLoopReturnValues' => [
115+
false, //before $phrase1
116+
false, //before =
117+
false, //before new \Magento\Framework\Phrase('Testing')
118+
false, //before ;
119+
false, //before $phrase2
120+
false, //before =
121+
false, //before __('More testing')
122+
false, //before ;
123+
true //end of loop
124+
],
125+
'getNextRealTokenReturnValues' => [
126+
$this->createToken(false, false, false, false, '$phrase1'),
127+
$this->createToken(false, false, false, false, '='),
128+
$this->createToken(false, false, true, false, 'new', $line),
129+
$this->createToken(false, false, false, false, ';'),
130+
$this->createToken(false, false, false, false, '$phrase2'),
131+
$this->createToken(false, false, false, false, '='),
132+
$this->createToken(true, false, false, false, '__', $line),
133+
$this->createToken(false, true, false, false, '(')
134+
],
135+
'getFunctionArgumentsTokensReturnValues' => [
136+
[[$this->createToken(false, false, false, true, 'Testing')]], // 'Testing')
137+
[[$this->createToken(false, false, false, true, 'More testing')]] // 'More testing')
138+
],
139+
'isMatchingClassReturnValues' => [
140+
true // \Magento\Framework\Phrase(
141+
],
142+
'result' => [
143+
[
144+
'phrase' => 'Testing',
145+
'arguments' => 0,
146+
'file' => $file,
147+
'line' => $line
148+
],
149+
[
150+
'phrase' => 'More testing',
151+
'arguments' => 0,
152+
'file' => $file,
153+
'line' => $line
154+
]
155+
]
156+
]
157+
];
158+
}
159+
160+
/**
161+
* @param bool $isEqualFunctionReturnValue
162+
* @param bool $isOpenBraceReturnValue
163+
* @param bool $isNewReturnValue
164+
* @param bool $isConstantEncapsedString
165+
* @param string $value
166+
* @param int|null $line
167+
* @return Token|\PHPUnit_Framework_MockObject_MockObject
168+
*/
169+
protected function createToken(
170+
$isEqualFunctionReturnValue,
171+
$isOpenBraceReturnValue,
172+
$isNewReturnValue,
173+
$isConstantEncapsedString,
174+
$value,
175+
$line = null
176+
) {
177+
$token = $this->getMockBuilder('Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Token')
178+
->disableOriginalConstructor()
179+
->getMock();
180+
$token->expects($this->any())
181+
->method('isEqualFunction')
182+
->with('__')
183+
->willReturn($isEqualFunctionReturnValue);
184+
$token->expects($this->any())
185+
->method('isOpenBrace')
186+
->willReturn($isOpenBraceReturnValue);
187+
$token->expects($this->any())
188+
->method('isNew')
189+
->willReturn($isNewReturnValue);
190+
$token->expects($this->any())
191+
->method('isConstantEncapsedString')
192+
->willReturn($isConstantEncapsedString);
193+
$token->expects($this->any())
194+
->method('getValue')
195+
->willReturn($value);
196+
$token->expects($this->any())
197+
->method('getLine')
198+
->willReturn($line);
199+
return $token;
200+
}
201+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
10+
/**
11+
* @covers \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Token
12+
*/
13+
class TokenTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var ObjectManager
17+
*/
18+
protected $objectManager;
19+
20+
protected function setUp()
21+
{
22+
$this->objectManager = new ObjectManager($this);
23+
}
24+
25+
/**
26+
* @covers \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Token::isNew
27+
*
28+
* @param int $name
29+
* @param string $value
30+
* @param bool $result
31+
* @dataProvider testIsNewDataProvider
32+
*/
33+
public function testIsNew($name, $value, $result)
34+
{
35+
$token = $this->createToken($name, $value);
36+
$this->assertEquals($result, $token->isNew());
37+
}
38+
39+
/**
40+
* @covers \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Token::isNamespaceSeparator
41+
*
42+
* @param int $name
43+
* @param string $value
44+
* @param bool $result
45+
* @dataProvider testIsNamespaceSeparatorDataProvider
46+
*/
47+
public function testIsNamespaceSeparator($name, $value, $result)
48+
{
49+
$token = $this->createToken($name, $value);
50+
$this->assertEquals($result, $token->isNamespaceSeparator());
51+
}
52+
53+
/**
54+
* @covers \Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Token::isIdentifier
55+
*
56+
* @param int $name
57+
* @param string $value
58+
* @param bool $result
59+
* @dataProvider testIsIdentifierDataProvider
60+
*/
61+
public function testIsIdentifier($name, $value, $result)
62+
{
63+
$token = $this->createToken($name, $value);
64+
$this->assertEquals($result, $token->isIdentifier());
65+
}
66+
67+
/**
68+
* @return array
69+
*/
70+
public function testIsNewDataProvider()
71+
{
72+
return [
73+
'new' => ['name' => T_NEW, 'value' => 'new', 'result' => true],
74+
'namespace' => ['name' => T_NS_SEPARATOR, 'value' => '\\', 'result' => false],
75+
'identifier' => ['name' => T_STRING, 'value' => '__', 'result' => false]
76+
];
77+
}
78+
79+
/**
80+
* @return array
81+
*/
82+
public function testIsNamespaceSeparatorDataProvider()
83+
{
84+
return [
85+
'new' => ['name' => T_NEW, 'value' => 'new', 'result' => false],
86+
'namespace' => ['name' => T_NS_SEPARATOR, 'value' => '\\', 'result' => true],
87+
'identifier' => ['name' => T_STRING, 'value' => '__', 'result' => false]
88+
];
89+
}
90+
91+
/**
92+
* @return array
93+
*/
94+
public function testIsIdentifierDataProvider()
95+
{
96+
return [
97+
'new' => ['name' => T_NEW, 'value' => 'new', 'result' => false],
98+
'namespace' => ['name' => T_NS_SEPARATOR, 'value' => '\\', 'result' => false],
99+
'identifier' => ['name' => T_STRING, 'value' => '__', 'result' => true]
100+
];
101+
}
102+
103+
/**
104+
* @param int $name
105+
* @param string $value
106+
* @return Token
107+
*/
108+
protected function createToken($name, $value)
109+
{
110+
$line = 110;
111+
return $this->objectManager->getObject(
112+
'Magento\Tools\I18n\Parser\Adapter\Php\Tokenizer\Token',
113+
[
114+
'name' => $name,
115+
'value' => $value,
116+
'line' => $line
117+
]
118+
);
119+
}
120+
}

0 commit comments

Comments
 (0)