Skip to content

Commit

Permalink
Added tests for the AbstractArraySniff class (currently failing due t…
Browse files Browse the repository at this point in the history
…o bug #2745)
  • Loading branch information
gsherwood committed Jan 14, 2020
1 parent f4662e8 commit 6a3b033
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 0 deletions.
11 changes: 11 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="" name="RuleInclusionAbsoluteWindowsTest.php" role="test" />
<file baseinstalldir="" name="RuleInclusionAbsoluteWindowsTest.xml" role="test" />
</dir>
<dir name="Sniffs">
<file baseinstalldir="" name="AbstractArraySniffTest.inc" role="test" />
<file baseinstalldir="" name="AbstractArraySniffTest.php" role="test" />
<file baseinstalldir="" name="AbstractArraySniffTestable.php" role="test" />
</dir>
<dir name="Tokenizer">
<file baseinstalldir="" name="AnonClassParenthesisOwnerTest.inc" role="test" />
<file baseinstalldir="" name="AnonClassParenthesisOwnerTest.php" role="test" />
Expand Down Expand Up @@ -1976,6 +1981,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" />
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" />
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" />
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTest.inc" name="tests/Core/Sniffs/AbstractArraySniffTest.inc" />
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTest.php" name="tests/Core/Sniffs/AbstractArraySniffTest.php" />
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTestable.php" name="tests/Core/Sniffs/AbstractArraySniffTestable.php" />
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" />
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" />
<install as="CodeSniffer/Core/Tokenizer/BackfillFnTokenTest.php" name="tests/Core/Tokenizer/BackfillFnTokenTest.php" />
Expand Down Expand Up @@ -2026,6 +2034,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" />
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" />
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" />
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTest.inc" name="tests/Core/Sniffs/AbstractArraySniffTest.inc" />
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTest.php" name="tests/Core/Sniffs/AbstractArraySniffTest.php" />
<install as="CodeSniffer/Core/Sniffs/AbstractArraySniffTestable.php" name="tests/Core/Sniffs/AbstractArraySniffTestable.php" />
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" />
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" />
<install as="CodeSniffer/Core/Tokenizer/BackfillFnTokenTest.php" name="tests/Core/Tokenizer/BackfillFnTokenTest.php" />
Expand Down
51 changes: 51 additions & 0 deletions tests/Core/Sniffs/AbstractArraySniffTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/* testSimpleValues */
$foo = [1,2,3];

/* testSimpleKeyValues */
$foo = ['1'=>1,'2'=>2,'3'=>3];

/* testMissingKeys */
$foo = ['1'=>1,2,'3'=>3];

/* testMultiTokenKeys */
$paths = array(
Init::ROOT_DIR.'/a' => 'a',
Init::ROOT_DIR.'/b' => 'b',
);

/* testMissingKeysCoalesceTernary */
return [
$a => static function () { return [1,2,3]; },
$b ?? $c,
$d ? [$e] : [$f],
];

/* testTernaryValues */
$foo = [
'1' => $row['status'] === 'rejected'
? self::REJECTED_CODE
: self::VERIFIED_CODE,
'2' => in_array($row['status'], array('notverified', 'unverified'), true)
? self::STATUS_PENDING
: self::STATUS_VERIFIED,
'3' => strtotime($row['date']),
];

/* testHeredocValues */
$foo = array(
<<<HERE
HERE
,
<<<HERE
HERE
,
);

/* testArrowFunctionValue */
$foo = array(
1 => '1',
2 => fn ($x) => yield 'a' => $x,
3 => '3',
);
290 changes: 290 additions & 0 deletions tests/Core/Sniffs/AbstractArraySniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
<?php
/**
* Tests for the \PHP_CodeSniffer\Sniffs\AbstractArraySniff.
*
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2020 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Sniffs;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

class AbstractArraySniffTest extends AbstractMethodUnitTest
{

/**
* The sniff objects we are testing.
*
* This extends the \PHP_CodeSniffer\Sniffs\AbstractArraySniff class to make the
* internal workings of the sniff observable.
*
* @var \PHP_CodeSniffer\Sniffs\AbstractArraySniffTestable
*/
protected static $sniff;


/**
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
*
* The test case file for a unit test class has to be in the same directory
* directory and use the same file name as the test class, using the .inc extension.
*
* @return void
*/
public static function setUpBeforeClass()
{
self::$sniff = new AbstractArraySniffTestable();
parent::setUpBeforeClass();

}//end setUpBeforeClass()


/**
* Test an array of simple values only.
*
* @return void
*/
public function testSimpleValues()
{
$token = $this->getTargetToken('/* testSimpleValues */', T_OPEN_SHORT_ARRAY);
self::$sniff->process(self::$phpcsFile, $token);

$expected = [
0 => ['value_start' => ($token + 1)],
1 => ['value_start' => ($token + 3)],
2 => ['value_start' => ($token + 5)],
];

$this->assertSame($expected, self::$sniff->indicies);

}//end testSimpleValues()


/**
* Test an array of simple keys and values.
*
* @return void
*/
public function testSimpleKeyValues()
{
$token = $this->getTargetToken('/* testSimpleKeyValues */', T_OPEN_SHORT_ARRAY);
self::$sniff->process(self::$phpcsFile, $token);

$expected = [
0 => [
'index_start' => ($token + 1),
'index_end' => ($token + 1),
'arrow' => ($token + 2),
'value_start' => ($token + 3),
],
1 => [
'index_start' => ($token + 5),
'index_end' => ($token + 5),
'arrow' => ($token + 6),
'value_start' => ($token + 7),
],
2 => [
'index_start' => ($token + 9),
'index_end' => ($token + 9),
'arrow' => ($token + 10),
'value_start' => ($token + 11),
],
];

$this->assertSame($expected, self::$sniff->indicies);

}//end testSimpleKeyValues()


/**
* Test an array of simple keys and values.
*
* @return void
*/
public function testMissingKeys()
{
$token = $this->getTargetToken('/* testMissingKeys */', T_OPEN_SHORT_ARRAY);
self::$sniff->process(self::$phpcsFile, $token);

$expected = [
0 => [
'index_start' => ($token + 1),
'index_end' => ($token + 1),
'arrow' => ($token + 2),
'value_start' => ($token + 3),
],
1 => [
'value_start' => ($token + 5),
],
2 => [
'index_start' => ($token + 7),
'index_end' => ($token + 7),
'arrow' => ($token + 8),
'value_start' => ($token + 9),
],
];

$this->assertSame($expected, self::$sniff->indicies);

}//end testMissingKeys()


/**
* Test an array with keys that span multiple tokens.
*
* @return void
*/
public function testMultiTokenKeys()
{
$token = $this->getTargetToken('/* testMultiTokenKeys */', T_ARRAY);
self::$sniff->process(self::$phpcsFile, $token);

$expected = [
0 => [
'index_start' => ($token + 4),
'index_end' => ($token + 8),
'arrow' => ($token + 10),
'value_start' => ($token + 12),
],
1 => [
'index_start' => ($token + 16),
'index_end' => ($token + 20),
'arrow' => ($token + 22),
'value_start' => ($token + 24),
],
];

$this->assertSame($expected, self::$sniff->indicies);

}//end testMultiTokenKeys()


/**
* Test an array of simple keys and values.
*
* @return void
*/
public function testMissingKeysCoalesceTernary()
{
$token = $this->getTargetToken('/* testMissingKeysCoalesceTernary */', T_OPEN_SHORT_ARRAY);
self::$sniff->process(self::$phpcsFile, $token);

$expected = [
0 => [
'index_start' => ($token + 3),
'index_end' => ($token + 3),
'arrow' => ($token + 5),
'value_start' => ($token + 7),
],
1 => [
'value_start' => ($token + 31),
],
2 => [
'value_start' => ($token + 39),
],
];

$this->assertSame($expected, self::$sniff->indicies);

}//end testMissingKeysCoalesceTernary()


/**
* Test an array of ternary values.
*
* @return void
*/
public function testTernaryValues()
{
$token = $this->getTargetToken('/* testTernaryValues */', T_ARRAY);
self::$sniff->process(self::$phpcsFile, $token);

$expected = [
0 => [
'index_start' => ($token + 3),
'index_end' => ($token + 3),
'arrow' => ($token + 5),
'value_start' => ($token + 7),
],
1 => [
'index_start' => ($token + 32),
'index_end' => ($token + 32),
'arrow' => ($token + 34),
'value_start' => ($token + 36),
],
2 => [
'index_start' => ($token + 72),
'index_end' => ($token + 72),
'arrow' => ($token + 74),
'value_start' => ($token + 76),
],
];

$this->assertSame($expected, self::$sniff->indicies);

}//end testTernaryValues()


/**
* Test an array of heredocs.
*
* @return void
*/
public function testHeredocValues()
{
$token = $this->getTargetToken('/* testHeredocValues */', T_ARRAY);
self::$sniff->process(self::$phpcsFile, $token);

$expected = [
0 => [
'value_start' => ($token + 4),
],
1 => [
'value_start' => ($token + 10),
],
];

$this->assertSame($expected, self::$sniff->indicies);

}//end testHeredocValues()


/**
* Test an array of with an arrow function as a value.
*
* @return void
*/
public function testArrowFunctionValue()
{
$token = $this->getTargetToken('/* testArrowFunctionValue */', T_ARRAY);
self::$sniff->process(self::$phpcsFile, $token);

$expected = [
0 => [
'index_start' => ($token + 4),
'index_end' => ($token + 4),
'arrow' => ($token + 6),
'value_start' => ($token + 8),
],
1 => [
'index_start' => ($token + 12),
'index_end' => ($token + 12),
'arrow' => ($token + 14),
'value_start' => ($token + 16),
],
2 => [
'index_start' => ($token + 34),
'index_end' => ($token + 34),
'arrow' => ($token + 36),
'value_start' => ($token + 38),
],
];

$this->assertSame($expected, self::$sniff->indicies);

}//end testArrowFunctionValue()


}//end class
Loading

0 comments on commit 6a3b033

Please sign in to comment.