|
| 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