Browse Source

AbstractTransformerTestCase - always execute counting assertion

Dariusz Ruminski 7 years ago
parent
commit
eb85477d24

+ 32 - 19
tests/Test/AbstractTransformerTestCase.php

@@ -23,29 +23,23 @@ use PHPUnit\Framework\TestCase;
  */
 abstract class AbstractTransformerTestCase extends TestCase
 {
-    protected function doTest($source, array $expectedTokens = array(), array $observedKinds = array())
+    protected function doTest($source, array $expectedTokens = array(), array $observedKindsOrPrototypes = array())
     {
         $tokens = Tokens::fromCode($source);
 
-        if (count($observedKinds)) {
-            $observedKinds = array_unique(array_merge(
-                $observedKinds,
-                array_filter($expectedTokens, function ($item) {
-                    return !is_string($item);
-                })
-            ));
-
-            $this->assertSame(
-                count($expectedTokens),
-                array_sum(array_map(
-                    function ($item) {
-                        return count($item);
+        $this->assertSame(
+            count($expectedTokens),
+            $this->countTokenPrototypes(
+                $tokens,
+                array_map(
+                    function ($kindOrPrototype) {
+                        return is_int($kindOrPrototype) ? array($kindOrPrototype) : $kindOrPrototype;
                     },
-                    $tokens->findGivenKind($observedKinds)
-                )),
-                'Number of expected tokens does not match actual token count.'
-            );
-        }
+                    array_unique(array_merge($observedKindsOrPrototypes, $expectedTokens))
+                )
+            ),
+            'Number of expected tokens does not match actual token count.'
+        );
 
         foreach ($expectedTokens as $index => $tokenIdOrContent) {
             if (is_string($tokenIdOrContent)) {
@@ -67,4 +61,23 @@ abstract class AbstractTransformerTestCase extends TestCase
             );
         }
     }
+
+    /**
+     * @param Tokens $tokens
+     * @param array  $prototypes
+     *
+     * @return int
+     */
+    private function countTokenPrototypes(Tokens $tokens, array $prototypes)
+    {
+        $count = 0;
+
+        foreach ($tokens as $token) {
+            if ($token->equalsAny($prototypes)) {
+                ++$count;
+            }
+        }
+
+        return $count;
+    }
 }

+ 68 - 0
tests/Tokenizer/Transformer/BraceClassInstantiationTransformerTest.php

@@ -66,36 +66,88 @@ final class BraceClassInstantiationTransformerTest extends AbstractTransformerTe
             array(
                 '<?php return foo()->bar(new Foo())->bar();',
                 array(
+                    4 => '(',
+                    5 => ')',
                     8 => '(',
+                    12 => '(',
+                    13 => ')',
                     14 => ')',
+                    17 => '(',
+                    18 => ')',
+                ),
+                array(
+                    '(',
+                    ')',
+                    CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
+                    CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
                 ),
             ),
             array(
                 '<?php $foo[0](new Foo())->bar();',
                 array(
                     5 => '(',
+                    9 => '(',
+                    10 => ')',
                     11 => ')',
+                    14 => '(',
+                    15 => ')',
+                ),
+                array(
+                    '(',
+                    ')',
+                    CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
+                    CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
                 ),
             ),
             array(
                 '<?php $foo{0}(new Foo())->bar();',
                 array(
                     5 => '(',
+                    9 => '(',
+                    10 => ')',
                     11 => ')',
+                    14 => '(',
+                    15 => ')',
+                ),
+                array(
+                    '(',
+                    ')',
+                    CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
+                    CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
                 ),
             ),
             array(
                 '<?php $foo(new Foo())->bar();',
                 array(
                     2 => '(',
+                    6 => '(',
+                    7 => ')',
                     8 => ')',
+                    11 => '(',
+                    12 => ')',
+                ),
+                array(
+                    '(',
+                    ')',
+                    CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
+                    CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
                 ),
             ),
             array(
                 '<?php $$foo(new Foo())->bar();',
                 array(
                     3 => '(',
+                    7 => '(',
+                    8 => ')',
                     9 => ')',
+                    12 => '(',
+                    13 => ')',
+                ),
+                array(
+                    '(',
+                    ')',
+                    CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
+                    CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
                 ),
             ),
             array(
@@ -124,8 +176,16 @@ final class BraceClassInstantiationTransformerTest extends AbstractTransformerTe
                 '<?php $foo = array(new Foo());',
                 array(
                     6 => '(',
+                    10 => '(',
+                    11 => ')',
                     12 => ')',
                 ),
+                array(
+                    '(',
+                    ')',
+                    CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
+                    CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
+                ),
             ),
         );
     }
@@ -151,8 +211,16 @@ final class BraceClassInstantiationTransformerTest extends AbstractTransformerTe
                 '<?php $foo = new class(new \stdClass()) {};',
                 array(
                     8 => '(',
+                    13 => '(',
+                    14 => ')',
                     15 => ')',
                 ),
+                array(
+                    '(',
+                    ')',
+                    CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
+                    CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
+                ),
             ),
             array(
                 '<?php $foo = (new class(new \stdClass()) {});',