Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConstantUsageSniffTest.php
Go to the documentation of this file.
1 <?php
7 
8 use PHP_CodeSniffer\Files\File;
9 
10 class ConstantUsageSniffTest extends \PHPUnit\Framework\TestCase
11 {
15  private $fileMock;
16 
20  private $constantUsageSniff;
21 
22  protected function setUp()
23  {
24  $this->fileMock = $this->createMock(File::class);
25  $this->constantUsageSniff = new ConstantUsageSniff();
26  }
27 
33  public function testProcessIncorrectArguments($file, $numIncorrectUsages)
34  {
35  $stackPtr = 10;
36  $fileContent = file_get_contents(__DIR__ . '/_files/' . $file);
37  $tokens = $this->tokenizeString($fileContent);
38  $this->fileMock->expects($this->once())
39  ->method('findPrevious')
40  ->with(
41  T_OPEN_TAG,
42  $stackPtr - 1
43  )
44  ->willReturn(false);
45  $this->fileMock->expects($this->once())
46  ->method('getTokens')
47  ->willReturn($tokens);
48  $this->fileMock->numTokens = count($tokens);
49  $this->fileMock->expects($this->exactly($numIncorrectUsages))
50  ->method('addError')
51  ->with(
52  'Constants are not allowed as the first argument of translation function, use string literal instead',
53  $this->anything(),
54  'VariableTranslation'
55  );
56  $this->constantUsageSniff->process($this->fileMock, $stackPtr);
57  }
58 
65  private function tokenizeString($fileContent)
66  {
67  $lineNumber = 1;
68  $tokens = token_get_all($fileContent);
69  $snifferTokens = [];
70  for ($i = 0; $i < count($tokens); $i++) {
71  $content = is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
72  $snifferTokens[$i]['line'] = $lineNumber;
73  $snifferTokens[$i]['content'] = $content;
74  $trimmedContent = trim($content, ' ');
75  if ($trimmedContent == PHP_EOL || $trimmedContent == PHP_EOL . PHP_EOL) {
76  $lineNumber++;
77  }
78  }
79  return $snifferTokens;
80  }
81 
85  public function processDataProvider()
86  {
87  return [
88  [
89  'incorrect_arguments.txt',
90  9
91  ],
92  [
93  'correct_arguments.txt',
94  0
95  ]
96  ];
97  }
98 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$i
Definition: gallery.phtml:31
$tokens
Definition: cards_list.phtml:9