Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShortEchoSyntaxSniffTest.php
Go to the documentation of this file.
1 <?php
7 
8 class ShortEchoSyntaxSniffTest extends \PHPUnit\Framework\TestCase
9 {
13  private $fileMock;
14 
18  private $shortEchoUsageSniff;
19 
20  protected function setUp()
21  {
22  $this->fileMock = $this->getMock(\PHP_CodeSniffer_File::class, [], [], '', false);
23  $this->shortEchoUsageSniff = new ShortEchoSyntaxSniff();
24  }
25 
31  public function testEchoTagSniff($file, $stackPtr, $incorrectUsages)
32  {
33  $fileContent = file_get_contents(__DIR__ . '/_files/' . $file);
34  $tokens = $this->tokenizeString($fileContent);
35 
36  $this->fileMock->expects($this->any())
37  ->method('findNext')
38  ->with([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT], $stackPtr + 1, null, true)
39  ->willReturn($stackPtr + 1);
40 
41  $this->fileMock->expects($this->once())
42  ->method('getTokens')
43  ->willReturn($tokens);
44 
45  $this->fileMock->expects($this->exactly($incorrectUsages))
46  ->method('addError')
47  ->with('Short echo tag syntax must be used; expected "<?=" but found "<?php echo"');
48 
49  $this->shortEchoUsageSniff->process($this->fileMock, $stackPtr);
50  }
51 
58  private function tokenizeString($fileContent)
59  {
60  $tokens = token_get_all($fileContent);
61  $snifferTokens = [];
62  for ($i = 0; $i < count($tokens); $i++) {
63  $code = is_array($tokens[$i]) ? $tokens[$i][0] : $tokens[$i];
64  $content = is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
65  $snifferTokens[$i]['code'] = $code;
66  $snifferTokens[$i]['content'] = $content;
67  }
68  return $snifferTokens;
69  }
70 
74  public function processDataProvider()
75  {
76  return [
77  ['incorrect_echotag.txt', 1, 1],
78  ['correct_noecho.txt', 1, 0],
79  ['correct_echotag.txt', 1, 0]
80  ];
81  }
82 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$i
Definition: gallery.phtml:31
$tokens
Definition: cards_list.phtml:9
$code
Definition: info.phtml:12