From 74fe243dc29192f0e12f4fd37568acd4ab131069 Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Mon, 4 Dec 2017 20:02:37 +0100 Subject: [PATCH] Add tests --- src/Fixer/Import/NoUnusedImportsFixer.php | 8 +- .../Analyzer/Analysis/ArgumentAnalysis.php | 26 +- .../Analyzer/Analysis/NamespaceAnalysis.php | 5 +- .../Analysis/NamespaceUseAnalysis.php | 7 +- .../Analysis/StartEndTokenAwareAnalysis.php | 10 +- src/Tokenizer/Analyzer/FunctionsAnalyzer.php | 2 +- .../Analyzer/NamespaceUsesAnalyzer.php | 2 +- src/Tokenizer/Analyzer/NamespacesAnalyzer.php | 1 + .../FullyQualifiedStrictTypesFixerTest.php | 236 +++++++----------- .../Analysis/ArgumentAnalysisTest.php | 65 +++++ .../FunctionReturnTypeAnalysisTest.php | 51 ++++ .../Analysis/NamespaceAnalysisTest.php | 57 +++++ .../Analysis/NamespaceUseAnalysisTest.php | 66 +++++ .../Analyzer/ArgumentsAnalyzerTest.php | 9 +- .../Analyzer/FunctionsAnalyzerTest.php | 4 +- .../Analyzer/NamespaceUsesAnalyzerTest.php | 4 +- .../Analyzer/NamespacesAnalyzerTest.php | 2 +- 17 files changed, 381 insertions(+), 174 deletions(-) create mode 100644 tests/Tokenizer/Analyzer/Analysis/ArgumentAnalysisTest.php create mode 100644 tests/Tokenizer/Analyzer/Analysis/FunctionReturnTypeAnalysisTest.php create mode 100644 tests/Tokenizer/Analyzer/Analysis/NamespaceAnalysisTest.php create mode 100644 tests/Tokenizer/Analyzer/Analysis/NamespaceUseAnalysisTest.php diff --git a/src/Fixer/Import/NoUnusedImportsFixer.php b/src/Fixer/Import/NoUnusedImportsFixer.php index e845ac564dc..658e1a7f02c 100644 --- a/src/Fixer/Import/NoUnusedImportsFixer.php +++ b/src/Fixer/Import/NoUnusedImportsFixer.php @@ -96,7 +96,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) } /** - * @param string $content + * @param string $content * @param NamespaceUseAnalysis[] $useDeclarations * * @return array @@ -113,7 +113,7 @@ private function detectUseUsages($content, array $useDeclarations) } /** - * @param Tokens $tokens + * @param Tokens $tokens * @param StartEndTokenAwareAnalysis[] $partials * * @return string @@ -217,9 +217,9 @@ private function removeUseDeclaration(Tokens $tokens, NamespaceUseAnalysis $useD } /** - * @param Tokens $tokens + * @param Tokens $tokens * @param NamespaceUseAnalysis[] $useDeclarations - * @param NamespaceAnalysis[] $namespaceDeclarations + * @param NamespaceAnalysis[] $namespaceDeclarations */ private function removeUsesInSameNamespace(Tokens $tokens, array $useDeclarations, array $namespaceDeclarations) { diff --git a/src/Tokenizer/Analyzer/Analysis/ArgumentAnalysis.php b/src/Tokenizer/Analyzer/Analysis/ArgumentAnalysis.php index 94d17942fdb..b727b3280ad 100644 --- a/src/Tokenizer/Analyzer/Analysis/ArgumentAnalysis.php +++ b/src/Tokenizer/Analyzer/Analysis/ArgumentAnalysis.php @@ -17,11 +17,10 @@ */ final class ArgumentAnalysis { - /** * The default value of the argument. * - * @var string|null + * @var null|string */ private $default; @@ -42,28 +41,29 @@ final class ArgumentAnalysis /** * The type of the argument. * - * @var string|null + * @var null|string */ private $type; /** - * @var int|null + * @var null|int */ private $typeIndexStart; /** - * @var int|null + * @var null|int */ private $typeIndexEnd; /** * ArgumentAnalysis constructor. - * @param string $name - * @param int $nameIndex - * @param string|null $default - * @param string|null $type - * @param int|null $typeIndexStart - * @param int|null $typeIndexEnd + * + * @param string $name + * @param int $nameIndex + * @param null|string $default + * @param null|string $type + * @param null|int $typeIndexStart + * @param null|int $typeIndexEnd */ public function __construct($name, $nameIndex, $default, $type, $typeIndexStart, $typeIndexEnd) { @@ -124,7 +124,7 @@ public function hasType() } /** - * @return int|null + * @return null|int */ public function getTypeIndexStart() { @@ -132,7 +132,7 @@ public function getTypeIndexStart() } /** - * @return int|null + * @return null|int */ public function getTypeIndexEnd() { diff --git a/src/Tokenizer/Analyzer/Analysis/NamespaceAnalysis.php b/src/Tokenizer/Analyzer/Analysis/NamespaceAnalysis.php index 7b88b1e5626..f5d7facdac5 100644 --- a/src/Tokenizer/Analyzer/Analysis/NamespaceAnalysis.php +++ b/src/Tokenizer/Analyzer/Analysis/NamespaceAnalysis.php @@ -47,10 +47,11 @@ final class NamespaceAnalysis implements StartEndTokenAwareAnalysis /** * NamespaceAnalysis constructor. + * * @param string $fullName * @param string $shortName - * @param int $startIndex - * @param int $endIndex + * @param int $startIndex + * @param int $endIndex */ public function __construct($fullName, $shortName, $startIndex, $endIndex) { diff --git a/src/Tokenizer/Analyzer/Analysis/NamespaceUseAnalysis.php b/src/Tokenizer/Analyzer/Analysis/NamespaceUseAnalysis.php index 6ec04f045a0..f8e4d38c14f 100644 --- a/src/Tokenizer/Analyzer/Analysis/NamespaceUseAnalysis.php +++ b/src/Tokenizer/Analyzer/Analysis/NamespaceUseAnalysis.php @@ -54,11 +54,12 @@ final class NamespaceUseAnalysis implements StartEndTokenAwareAnalysis /** * NamespaceAnalysis constructor. + * * @param string $fullName * @param string $shortName - * @param bool $aliased - * @param int $startIndex - * @param int $endIndex + * @param bool $aliased + * @param int $startIndex + * @param int $endIndex */ public function __construct($fullName, $shortName, $aliased, $startIndex, $endIndex) { diff --git a/src/Tokenizer/Analyzer/Analysis/StartEndTokenAwareAnalysis.php b/src/Tokenizer/Analyzer/Analysis/StartEndTokenAwareAnalysis.php index e25b9f159ea..1cadfff488e 100644 --- a/src/Tokenizer/Analyzer/Analysis/StartEndTokenAwareAnalysis.php +++ b/src/Tokenizer/Analyzer/Analysis/StartEndTokenAwareAnalysis.php @@ -1,6 +1,14 @@ + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ namespace PhpCsFixer\Tokenizer\Analyzer\Analysis; diff --git a/src/Tokenizer/Analyzer/FunctionsAnalyzer.php b/src/Tokenizer/Analyzer/FunctionsAnalyzer.php index c3069004f77..3777b79a2ec 100644 --- a/src/Tokenizer/Analyzer/FunctionsAnalyzer.php +++ b/src/Tokenizer/Analyzer/FunctionsAnalyzer.php @@ -46,7 +46,7 @@ public function getFunctionArguments(Tokens $tokens, $methodIndex) * @param Tokens $tokens * @param int $methodIndex * - * @return FunctionReturnTypeAnalysis|null + * @return null|FunctionReturnTypeAnalysis */ public function getFunctionReturnType(Tokens $tokens, $methodIndex) { diff --git a/src/Tokenizer/Analyzer/NamespaceUsesAnalyzer.php b/src/Tokenizer/Analyzer/NamespaceUsesAnalyzer.php index fda8f6f7065..1e06f59cc11 100644 --- a/src/Tokenizer/Analyzer/NamespaceUsesAnalyzer.php +++ b/src/Tokenizer/Analyzer/NamespaceUsesAnalyzer.php @@ -19,7 +19,7 @@ /** * @internal */ -class NamespaceUsesAnalyzer +final class NamespaceUsesAnalyzer { /** * @param Tokens $tokens diff --git a/src/Tokenizer/Analyzer/NamespacesAnalyzer.php b/src/Tokenizer/Analyzer/NamespacesAnalyzer.php index 3526e2dea1c..3e9a12332fd 100644 --- a/src/Tokenizer/Analyzer/NamespacesAnalyzer.php +++ b/src/Tokenizer/Analyzer/NamespacesAnalyzer.php @@ -22,6 +22,7 @@ final class NamespacesAnalyzer { /** * @param Tokens $tokens + * * @return NamespaceAnalysis[] */ public function getDeclarations(Tokens $tokens) diff --git a/tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php b/tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php index 213cae93ac6..a87c6eeb761 100644 --- a/tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php +++ b/tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php @@ -23,44 +23,36 @@ */ final class FullyQualifiedStrictTypesFixerTest extends AbstractFixerTestCase { - public function testImportedStrictTypesFixWithoutReturn() - { - $expected = <<<'EOF' -doTest($expected, $input); } -} -EOF; - $input = <<<'EOF' -doTest($expected, $input); } - /** - * @requires PHP 7.0 - */ - public function testImportedStrictTypesFixWithReturn() + public function provideCodeWithReturnTypesCases() { - $expected = <<<'EOF' -doTest($expected, $input); - } - - public function testNamespaceFixesWithoutReturn() - { - $expected = <<<'EOF' -doTest($expected, $input); - } - - /** - * @requires PHP 7.0 - */ - public function testNamespaceFixesWithReturn() - { - $expected = <<<'EOF' -doTest($expected, $input); - } - - public function testMultiNamespaceFixesWithoutReturn() - { - $expected = <<<'EOF' -doTest($expected, null); +class SomeClass +{ + public function doSomething(Bar $foo) + { } +}', + 'doTest($expected, $input); - } - - public function testPartialNamespaces() - { - $expected = <<<'EOF' -doTest($expected, $input); +}', + ], + ]; } } diff --git a/tests/Tokenizer/Analyzer/Analysis/ArgumentAnalysisTest.php b/tests/Tokenizer/Analyzer/Analysis/ArgumentAnalysisTest.php new file mode 100644 index 00000000000..576608e6056 --- /dev/null +++ b/tests/Tokenizer/Analyzer/Analysis/ArgumentAnalysisTest.php @@ -0,0 +1,65 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Tests\Tokenizer\Analyzer\Analysis; + +use PhpCsFixer\Tokenizer\Analyzer\Analysis\ArgumentAnalysis; +use PHPUnit\Framework\TestCase; + +/** + * @author VeeWee + * + * @internal + * + * @covers \PhpCsFixer\Tokenizer\Analyzer\Analysis\ArgumentAnalysis + */ +final class ArgumentAnalysisTest extends TestCase +{ + public function testName() + { + $analysis = new ArgumentAnalysis('$name', 1, null, null, null, null); + $this->assertSame('$name', $analysis->getName()); + $this->assertSame(1, $analysis->getNameIndex()); + } + + public function testDefault() + { + $analysis = new ArgumentAnalysis('$name', 1, 'default', null, null, null); + $this->assertTrue($analysis->hasDefault()); + $this->assertSame('default', $analysis->getDefault()); + } + + public function testNoDefaultFound() + { + $analysis = new ArgumentAnalysis('$name', 1, null, null, null, null); + $this->assertFalse($analysis->hasDefault()); + $this->assertNull($analysis->getDefault()); + } + + public function testType() + { + $analysis = new ArgumentAnalysis('$name', 1, null, 'string', 1, 4); + $this->assertTrue($analysis->hasType()); + $this->assertSame('string', $analysis->getType()); + $this->assertSame(1, $analysis->getTypeIndexStart()); + $this->assertSame(4, $analysis->getTypeIndexEnd()); + } + + public function testNoTypeFound() + { + $analysis = new ArgumentAnalysis('$name', 1, null, null, null, null); + $this->assertFalse($analysis->hasDefault()); + $this->assertNull($analysis->getDefault()); + $this->assertNull($analysis->getTypeIndexStart()); + $this->assertNull($analysis->getTypeIndexEnd()); + } +} diff --git a/tests/Tokenizer/Analyzer/Analysis/FunctionReturnTypeAnalysisTest.php b/tests/Tokenizer/Analyzer/Analysis/FunctionReturnTypeAnalysisTest.php new file mode 100644 index 00000000000..2996ead87fb --- /dev/null +++ b/tests/Tokenizer/Analyzer/Analysis/FunctionReturnTypeAnalysisTest.php @@ -0,0 +1,51 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Tests\Tokenizer\Analyzer\Analysis; + +use PhpCsFixer\Tokenizer\Analyzer\Analysis\FunctionReturnTypeAnalysis; +use PhpCsFixer\Tokenizer\Analyzer\Analysis\StartEndTokenAwareAnalysis; +use PHPUnit\Framework\TestCase; + +/** + * @author VeeWee + * + * @internal + * + * @covers \PhpCsFixer\Tokenizer\Analyzer\Analysis\FunctionReturnTypeAnalysis + */ +final class FunctionReturnTypeAnalysisTest extends TestCase +{ + public function testStartEndTokenAwareAnalysis() + { + $analysis = new FunctionReturnTypeAnalysis('string', 1, 2); + $this->assertInstanceOf(StartEndTokenAwareAnalysis::class, $analysis); + } + + public function testType() + { + $analysis = new FunctionReturnTypeAnalysis('string', 1, 2); + $this->assertSame('string', $analysis->getType()); + } + + public function testStartIndex() + { + $analysis = new FunctionReturnTypeAnalysis('string', 1, 2); + $this->assertSame(1, $analysis->getStartIndex()); + } + + public function testEndIndex() + { + $analysis = new FunctionReturnTypeAnalysis('string', 1, 2); + $this->assertSame(2, $analysis->getEndIndex()); + } +} diff --git a/tests/Tokenizer/Analyzer/Analysis/NamespaceAnalysisTest.php b/tests/Tokenizer/Analyzer/Analysis/NamespaceAnalysisTest.php new file mode 100644 index 00000000000..3a147b1b524 --- /dev/null +++ b/tests/Tokenizer/Analyzer/Analysis/NamespaceAnalysisTest.php @@ -0,0 +1,57 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Tests\Tokenizer\Analyzer\Analysis; + +use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis; +use PhpCsFixer\Tokenizer\Analyzer\Analysis\StartEndTokenAwareAnalysis; +use PHPUnit\Framework\TestCase; + +/** + * @author VeeWee + * + * @internal + * + * @covers \PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis + */ +final class NamespaceAnalysisTest extends TestCase +{ + public function testStartEndTokenAwareAnalysis() + { + $analysis = new NamespaceAnalysis('Full\NamespaceName', 'NamespaceName', 1, 2); + $this->assertInstanceOf(StartEndTokenAwareAnalysis::class, $analysis); + } + + public function testFullName() + { + $analysis = new NamespaceAnalysis('Full\NamespaceName', 'NamespaceName', 1, 2); + $this->assertSame('Full\NamespaceName', $analysis->getFullName()); + } + + public function testShortName() + { + $analysis = new NamespaceAnalysis('Full\NamespaceName', 'NamespaceName', 1, 2); + $this->assertSame('NamespaceName', $analysis->getShortName()); + } + + public function testStartIndex() + { + $analysis = new NamespaceAnalysis('Full\NamespaceName', 'NamespaceName', 1, 2); + $this->assertSame(1, $analysis->getStartIndex()); + } + + public function testEndIndex() + { + $analysis = new NamespaceAnalysis('Full\NamespaceName', 'NamespaceName', 1, 2); + $this->assertSame(2, $analysis->getEndIndex()); + } +} diff --git a/tests/Tokenizer/Analyzer/Analysis/NamespaceUseAnalysisTest.php b/tests/Tokenizer/Analyzer/Analysis/NamespaceUseAnalysisTest.php new file mode 100644 index 00000000000..93bc3298c65 --- /dev/null +++ b/tests/Tokenizer/Analyzer/Analysis/NamespaceUseAnalysisTest.php @@ -0,0 +1,66 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Tests\Tokenizer\Analyzer\Analysis; + +use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis; +use PhpCsFixer\Tokenizer\Analyzer\Analysis\StartEndTokenAwareAnalysis; +use PHPUnit\Framework\TestCase; + +/** + * @author VeeWee + * + * @internal + * + * @covers \PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis + */ +final class NamespaceUseAnalysisTest extends TestCase +{ + public function testStartEndTokenAwareAnalysis() + { + $analysis = new NamespaceUseAnalysis('Full\NamespaceName', 'NamespaceName', false, 1, 2); + $this->assertInstanceOf(StartEndTokenAwareAnalysis::class, $analysis); + } + + public function testFullName() + { + $analysis = new NamespaceUseAnalysis('Full\NamespaceName', 'NamespaceName', false, 1, 2); + $this->assertSame('Full\NamespaceName', $analysis->getFullName()); + } + + public function testAliased() + { + $analysis = new NamespaceUseAnalysis('Full\NamespaceName', 'NamespaceName', false, 1, 2); + $this->assertFalse($analysis->isAliased()); + + $analysis = new NamespaceUseAnalysis('Full\NamespaceName', 'NamespaceName', true, 1, 2); + $this->assertTrue($analysis->isAliased()); + } + + public function testShortName() + { + $analysis = new NamespaceUseAnalysis('Full\NamespaceName', 'NamespaceName', false, 1, 2); + $this->assertSame('NamespaceName', $analysis->getShortName()); + } + + public function testStartIndex() + { + $analysis = new NamespaceUseAnalysis('Full\NamespaceName', 'NamespaceName', false, 1, 2); + $this->assertSame(1, $analysis->getStartIndex()); + } + + public function testEndIndex() + { + $analysis = new NamespaceUseAnalysis('Full\NamespaceName', 'NamespaceName', false, 1, 2); + $this->assertSame(2, $analysis->getEndIndex()); + } +} diff --git a/tests/Tokenizer/Analyzer/ArgumentsAnalyzerTest.php b/tests/Tokenizer/Analyzer/ArgumentsAnalyzerTest.php index 3e3e3ab8f40..e4e815bf3cc 100644 --- a/tests/Tokenizer/Analyzer/ArgumentsAnalyzerTest.php +++ b/tests/Tokenizer/Analyzer/ArgumentsAnalyzerTest.php @@ -39,8 +39,8 @@ public function testArguments($code, $openIndex, $closeIndex, array $arguments) $tokens = Tokens::fromCode($code); $analyzer = new ArgumentsAnalyzer(); - $this->assertEquals(count($arguments), $analyzer->countArguments($tokens, $openIndex, $closeIndex)); - $this->assertEquals($arguments, $analyzer->getArguments($tokens, $openIndex, $closeIndex)); + $this->assertSame(count($arguments), $analyzer->countArguments($tokens, $openIndex, $closeIndex)); + $this->assertSame($arguments, $analyzer->getArguments($tokens, $openIndex, $closeIndex)); } /** @@ -56,7 +56,10 @@ public function testArgumentInfo($code, $openIndex, $closeIndex, $expected) $tokens = Tokens::fromCode($code); $analyzer = new ArgumentsAnalyzer(); - $this->assertEquals($expected, $analyzer->getArgumentInfo($tokens, $openIndex, $closeIndex)); + $this->assertSame( + serialize($expected), + serialize($analyzer->getArgumentInfo($tokens, $openIndex, $closeIndex)) + ); } public function provideArgumentsCases() diff --git a/tests/Tokenizer/Analyzer/FunctionsAnalyzerTest.php b/tests/Tokenizer/Analyzer/FunctionsAnalyzerTest.php index c503c6d4389..9503c6056d0 100644 --- a/tests/Tokenizer/Analyzer/FunctionsAnalyzerTest.php +++ b/tests/Tokenizer/Analyzer/FunctionsAnalyzerTest.php @@ -39,7 +39,7 @@ public function testFunctionArgumentInfo($code, $methodIndex, $expected) $tokens = Tokens::fromCode($code); $analyzer = new FunctionsAnalyzer(); - $this->assertEquals($expected, $analyzer->getFunctionArguments($tokens, $methodIndex)); + $this->assertSame(serialize($expected), serialize($analyzer->getFunctionArguments($tokens, $methodIndex))); } /** @@ -54,7 +54,7 @@ public function testFunctionReturnTypeInfo($code, $methodIndex, $expected) $tokens = Tokens::fromCode($code); $analyzer = new FunctionsAnalyzer(); - $this->assertEquals($expected, $analyzer->getFunctionReturnType($tokens, $methodIndex)); + $this->assertSame(serialize($expected), serialize($analyzer->getFunctionReturnType($tokens, $methodIndex))); } public function provideFunctionsWithArgumentsCases() diff --git a/tests/Tokenizer/Analyzer/NamespaceUsesAnalyzerTest.php b/tests/Tokenizer/Analyzer/NamespaceUsesAnalyzerTest.php index 56a27e30084..35ed955f968 100644 --- a/tests/Tokenizer/Analyzer/NamespaceUsesAnalyzerTest.php +++ b/tests/Tokenizer/Analyzer/NamespaceUsesAnalyzerTest.php @@ -37,7 +37,7 @@ public function testUsesFromTokens($code, $expected) $tokens = Tokens::fromCode($code); $analyzer = new NamespaceUsesAnalyzer(); - $this->assertEquals($expected, $analyzer->getDeclarationsFromTokens($tokens)); + $this->assertSame(serialize($expected), serialize($analyzer->getDeclarationsFromTokens($tokens))); } /** @@ -52,7 +52,7 @@ public function testUsesFromIndexes($code, $expected, $useIndexes) $tokens = Tokens::fromCode($code); $analyzer = new NamespaceUsesAnalyzer(); - $this->assertEquals($expected, $analyzer->getDeclarations($tokens, $useIndexes)); + $this->assertSame(serialize($expected), serialize($analyzer->getDeclarations($tokens, $useIndexes))); } public function provideNamespaceUsesCases() diff --git a/tests/Tokenizer/Analyzer/NamespacesAnalyzerTest.php b/tests/Tokenizer/Analyzer/NamespacesAnalyzerTest.php index 90e10e09760..9487869d406 100644 --- a/tests/Tokenizer/Analyzer/NamespacesAnalyzerTest.php +++ b/tests/Tokenizer/Analyzer/NamespacesAnalyzerTest.php @@ -37,7 +37,7 @@ public function testNamespaces($code, $expected) $tokens = Tokens::fromCode($code); $analyzer = new NamespacesAnalyzer(); - $this->assertEquals($expected, $analyzer->getDeclarations($tokens)); + $this->assertSame(serialize($expected), serialize(($analyzer->getDeclarations($tokens)))); } public function provideNamespacesCases()