Skip to content

Commit 28bd1a1

Browse files
author
Igor Melnikov
committed
MAGETWO-61240: Fix \Magento\Sniffs\Translation\ConstantUsageSniff
Fixing static test
1 parent 71f4018 commit 28bd1a1

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

dev/tests/static/framework/Magento/Sniffs/Translation/ConstantUsageSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function checkLineLength(\PHP_CodeSniffer_File $phpcsFile, $stackPtr,
3030
$previousLineMatch = preg_match($previousLineRegexp, $this->previousLineContent) !== 0;
3131
$this->previousLineContent = $lineContent;
3232
$error = 'Constants are not allowed as the first argument of translation function, use string literal instead';
33-
$constantRegexp = '[^\'"]+::[A-Z_0-9]+.*';
33+
$constantRegexp = '[^\$\'"]+::[A-Z_0-9]+.*';
3434
if ($currentLineMatch) {
3535
$variableRegexp = "~__\({$constantRegexp}\)|Phrase\({$constantRegexp}\)~";
3636
if (preg_match($variableRegexp, $lineContent) !== 0) {
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sniffs\Translation;
7+
8+
class ConstantUsageSniffTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \PHP_CodeSniffer_File|\PHPUnit_Framework_MockObject_MockObject
12+
*/
13+
private $fileMock;
14+
15+
/**
16+
* @var ConstantUsageSniff
17+
*/
18+
private $constantUsageSniff;
19+
20+
protected function setUp()
21+
{
22+
$this->fileMock = $this->getMock(\PHP_CodeSniffer_File::class, [], [], '', false);
23+
$this->constantUsageSniff = new ConstantUsageSniff();
24+
}
25+
26+
/**
27+
* @param string $line
28+
* @dataProvider checkLineLengthCorrectArguments
29+
*/
30+
public function testCheckLineLengthCorrectArguments($line)
31+
{
32+
$this->fileMock->expects($this->never())
33+
->method('addError');
34+
$this->checkLineLength(10, $line);
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function checkLineLengthCorrectArguments()
41+
{
42+
return [
43+
[
44+
'__($item)'
45+
],
46+
[
47+
'__($item[ConfigConverter::KEY_TITLE])'
48+
],
49+
[
50+
'__($item[\'value\'])'
51+
],
52+
[
53+
'__($item->getValue())'
54+
],
55+
[
56+
'Phrase($item)'
57+
],
58+
[
59+
'Phrase($item[ConfigConverter::KEY_TITLE])'
60+
],
61+
[
62+
'Phrase($item[\'value\'])'
63+
],
64+
[
65+
'Phrase($item->getValue())'
66+
],
67+
[
68+
'\Magento\Framework\Phrase($item)'
69+
]
70+
];
71+
}
72+
73+
/**
74+
* @param string $line
75+
* @dataProvider checkLineLengthIncorrectArguments
76+
*/
77+
public function testCheckLineLengthIncorrectArguments($line)
78+
{
79+
$lineNumber = 10;
80+
$this->fileMock->expects($this->once())
81+
->method('addError')
82+
->with(
83+
'Constants are not allowed as the first argument of translation function, use string literal instead',
84+
$lineNumber,
85+
'VariableTranslation'
86+
);
87+
$this->checkLineLength($lineNumber, $line);
88+
}
89+
90+
/**
91+
* @return array
92+
*/
93+
public function checkLineLengthIncorrectArguments()
94+
{
95+
return [
96+
[
97+
'$item[ConfigConverter::KEY_TITLE] = __(Converter::KEY_TITLE)'
98+
],
99+
[
100+
'$item[ConfigConverter::KEY_TITLE] = __(self::KEY_TITLE)'
101+
],
102+
[
103+
'$item[ConfigConverter::KEY_TITLE] = __(\Magento\Support\Model\Report\Config\Converter::KEY_TITLE)'
104+
],
105+
[
106+
'Phrase(Converter::KEY_TITLE)'
107+
],
108+
[
109+
'Phrase(self::KEY_TITLE)'
110+
],
111+
[
112+
'Phrase(\Magento\Support\Model\Report\Config\Converter::KEY_TITLE)'
113+
],
114+
[
115+
'\Magento\Framework\Phrase(Converter::KEY_TITLE)'
116+
]
117+
];
118+
}
119+
120+
/**
121+
* Call checkLineLength method
122+
*
123+
* @param int $lineNumber
124+
* @param string $line
125+
*/
126+
private function checkLineLength($lineNumber, $line)
127+
{
128+
$reflectionMethod = new \ReflectionMethod(ConstantUsageSniff::class, 'checkLineLength');
129+
$reflectionMethod->setAccessible(true);
130+
$reflectionMethod->invoke($this->constantUsageSniff, $this->fileMock, $lineNumber, $line);
131+
}
132+
}

0 commit comments

Comments
 (0)