From 28bd1a16d833241e72fa76b59159a1ed9c4cae27 Mon Sep 17 00:00:00 2001
From: Igor Melnikov <imelnikov@magento.com>
Date: Mon, 21 Nov 2016 15:15:41 -0600
Subject: [PATCH] MAGETWO-61240: Fix
 \Magento\Sniffs\Translation\ConstantUsageSniff

Fixing static test
---
 .../Sniffs/Translation/ConstantUsageSniff.php |   2 +-
 .../Translation/ConstantUsageSniffTest.php    | 132 ++++++++++++++++++
 2 files changed, 133 insertions(+), 1 deletion(-)
 create mode 100644 dev/tests/static/framework/tests/unit/testsuite/Magento/Sniffs/Translation/ConstantUsageSniffTest.php

diff --git a/dev/tests/static/framework/Magento/Sniffs/Translation/ConstantUsageSniff.php b/dev/tests/static/framework/Magento/Sniffs/Translation/ConstantUsageSniff.php
index 260f731aecf..b62e88c7ced 100644
--- a/dev/tests/static/framework/Magento/Sniffs/Translation/ConstantUsageSniff.php
+++ b/dev/tests/static/framework/Magento/Sniffs/Translation/ConstantUsageSniff.php
@@ -30,7 +30,7 @@ class ConstantUsageSniff extends \Generic_Sniffs_Files_LineLengthSniff
         $previousLineMatch = preg_match($previousLineRegexp, $this->previousLineContent) !== 0;
         $this->previousLineContent = $lineContent;
         $error = 'Constants are not allowed as the first argument of translation function, use string literal instead';
-        $constantRegexp = '[^\'"]+::[A-Z_0-9]+.*';
+        $constantRegexp = '[^\$\'"]+::[A-Z_0-9]+.*';
         if ($currentLineMatch) {
             $variableRegexp = "~__\({$constantRegexp}\)|Phrase\({$constantRegexp}\)~";
             if (preg_match($variableRegexp, $lineContent) !== 0) {
diff --git a/dev/tests/static/framework/tests/unit/testsuite/Magento/Sniffs/Translation/ConstantUsageSniffTest.php b/dev/tests/static/framework/tests/unit/testsuite/Magento/Sniffs/Translation/ConstantUsageSniffTest.php
new file mode 100644
index 00000000000..cf0e3092b25
--- /dev/null
+++ b/dev/tests/static/framework/tests/unit/testsuite/Magento/Sniffs/Translation/ConstantUsageSniffTest.php
@@ -0,0 +1,132 @@
+<?php
+/**
+ * Copyright © 2016 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+namespace Magento\Sniffs\Translation;
+
+class ConstantUsageSniffTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @var \PHP_CodeSniffer_File|\PHPUnit_Framework_MockObject_MockObject
+     */
+    private $fileMock;
+
+    /**
+     * @var ConstantUsageSniff
+     */
+    private $constantUsageSniff;
+
+    protected function setUp()
+    {
+        $this->fileMock = $this->getMock(\PHP_CodeSniffer_File::class, [], [], '', false);
+        $this->constantUsageSniff = new ConstantUsageSniff();
+    }
+
+    /**
+     * @param string $line
+     * @dataProvider checkLineLengthCorrectArguments
+     */
+    public function testCheckLineLengthCorrectArguments($line)
+    {
+        $this->fileMock->expects($this->never())
+            ->method('addError');
+        $this->checkLineLength(10, $line);
+    }
+
+    /**
+     * @return array
+     */
+    public function checkLineLengthCorrectArguments()
+    {
+        return [
+            [
+                '__($item)'
+            ],
+            [
+                '__($item[ConfigConverter::KEY_TITLE])'
+            ],
+            [
+                '__($item[\'value\'])'
+            ],
+            [
+                '__($item->getValue())'
+            ],
+            [
+                'Phrase($item)'
+            ],
+            [
+                'Phrase($item[ConfigConverter::KEY_TITLE])'
+            ],
+            [
+                'Phrase($item[\'value\'])'
+            ],
+            [
+                'Phrase($item->getValue())'
+            ],
+            [
+                '\Magento\Framework\Phrase($item)'
+            ]
+        ];
+    }
+
+    /**
+     * @param string $line
+     * @dataProvider checkLineLengthIncorrectArguments
+     */
+    public function testCheckLineLengthIncorrectArguments($line)
+    {
+        $lineNumber = 10;
+        $this->fileMock->expects($this->once())
+            ->method('addError')
+            ->with(
+                'Constants are not allowed as the first argument of translation function, use string literal instead',
+                $lineNumber,
+                'VariableTranslation'
+            );
+        $this->checkLineLength($lineNumber, $line);
+    }
+
+    /**
+     * @return array
+     */
+    public function checkLineLengthIncorrectArguments()
+    {
+        return [
+            [
+                '$item[ConfigConverter::KEY_TITLE] = __(Converter::KEY_TITLE)'
+            ],
+            [
+                '$item[ConfigConverter::KEY_TITLE] = __(self::KEY_TITLE)'
+            ],
+            [
+                '$item[ConfigConverter::KEY_TITLE] = __(\Magento\Support\Model\Report\Config\Converter::KEY_TITLE)'
+            ],
+            [
+                'Phrase(Converter::KEY_TITLE)'
+            ],
+            [
+                'Phrase(self::KEY_TITLE)'
+            ],
+            [
+                'Phrase(\Magento\Support\Model\Report\Config\Converter::KEY_TITLE)'
+            ],
+            [
+                '\Magento\Framework\Phrase(Converter::KEY_TITLE)'
+            ]
+        ];
+    }
+
+    /**
+     * Call checkLineLength method
+     *
+     * @param int $lineNumber
+     * @param string $line
+     */
+    private function checkLineLength($lineNumber, $line)
+    {
+        $reflectionMethod = new \ReflectionMethod(ConstantUsageSniff::class, 'checkLineLength');
+        $reflectionMethod->setAccessible(true);
+        $reflectionMethod->invoke($this->constantUsageSniff, $this->fileMock, $lineNumber, $line);
+    }
+}
-- 
GitLab