1<?php
2/*
3 * This file is part of php-token-stream.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11use PHPUnit\Framework\TestCase;
12
13class PHP_Token_ClassTest extends TestCase
14{
15    /**
16     * @var PHP_Token_CLASS
17     */
18    private $class;
19
20    /**
21     * @var PHP_Token_FUNCTION
22     */
23    private $function;
24
25    protected function setUp()
26    {
27        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source2.php');
28
29        foreach ($ts as $token) {
30            if ($token instanceof PHP_Token_CLASS) {
31                $this->class = $token;
32            }
33
34            if ($token instanceof PHP_Token_FUNCTION) {
35                $this->function = $token;
36                break;
37            }
38        }
39    }
40
41    /**
42     * @covers PHP_Token_CLASS::getKeywords
43     */
44    public function testGetClassKeywords()
45    {
46        $this->assertEquals('abstract', $this->class->getKeywords());
47    }
48
49    /**
50     * @covers PHP_Token_FUNCTION::getKeywords
51     */
52    public function testGetFunctionKeywords()
53    {
54        $this->assertEquals('abstract,static', $this->function->getKeywords());
55    }
56
57    /**
58     * @covers PHP_Token_FUNCTION::getVisibility
59     */
60    public function testGetFunctionVisibility()
61    {
62        $this->assertEquals('public', $this->function->getVisibility());
63    }
64
65    public function testIssue19()
66    {
67        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue19.php');
68
69        foreach ($ts as $token) {
70            if ($token instanceof PHP_Token_CLASS) {
71                $this->assertFalse($token->hasInterfaces());
72            }
73        }
74    }
75
76    public function testIssue30()
77    {
78        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue30.php');
79        $this->assertCount(1, $ts->getClasses());
80    }
81
82    public function testAnonymousClassesAreHandledCorrectly()
83    {
84        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class.php');
85
86        $classes = $ts->getClasses();
87
88        $this->assertEquals(
89            [
90                'class_with_method_that_declares_anonymous_class',
91                'AnonymousClass:9#31',
92                'AnonymousClass:10#55',
93                'AnonymousClass:11#75',
94                'AnonymousClass:12#91',
95                'AnonymousClass:13#107'
96            ],
97            array_keys($classes)
98        );
99    }
100
101    /**
102     * @ticket https://github.com/sebastianbergmann/php-token-stream/issues/52
103     */
104    public function testAnonymousClassesAreHandledCorrectly2()
105    {
106        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class2.php');
107
108        $classes = $ts->getClasses();
109
110        $this->assertEquals(['Test', 'AnonymousClass:4#23'], array_keys($classes));
111        $this->assertEquals(['methodOne', 'methodTwo'], array_keys($classes['Test']['methods']));
112
113        $this->assertEmpty($ts->getFunctions());
114    }
115
116    public function testImportedFunctionsAreHandledCorrectly()
117    {
118        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'classUsesNamespacedFunction.php');
119
120        $this->assertEmpty($ts->getFunctions());
121        $this->assertCount(1, $ts->getClasses());
122    }
123
124    /**
125     * @ticket https://github.com/sebastianbergmann/php-code-coverage/issues/543
126     */
127    public function testClassWithMultipleAnonymousClassesAndFunctionsIsHandledCorrectly()
128    {
129        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_multiple_anonymous_classes_and_functions.php');
130
131        $classes = $ts->getClasses();
132
133        $this->assertArrayHasKey('class_with_multiple_anonymous_classes_and_functions', $classes);
134        $this->assertArrayHasKey('AnonymousClass:6#23', $classes);
135        $this->assertArrayHasKey('AnonymousClass:12#53', $classes);
136        $this->assertArrayHasKey('m', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']);
137        $this->assertArrayHasKey('anonymousFunction:18#81', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']);
138        $this->assertArrayHasKey('anonymousFunction:22#108', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']);
139    }
140
141    /**
142     * @ticket https://github.com/sebastianbergmann/php-token-stream/issues/68
143     */
144    public function testClassWithMethodNamedEmptyIsHandledCorrectly()
145    {
146        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_named_empty.php');
147
148        $classes = $ts->getClasses();
149
150        $this->assertArrayHasKey('class_with_method_named_empty', $classes);
151        $this->assertArrayHasKey('empty', $classes['class_with_method_named_empty']['methods']);
152    }
153
154    /**
155     * @ticket https://github.com/sebastianbergmann/php-code-coverage/issues/424
156     */
157    public function testSomething()
158    {
159        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'php-code-coverage-issue-424.php');
160
161        $classes = $ts->getClasses();
162
163        $this->assertSame(5, $classes['Example']['methods']['even']['startLine']);
164        $this->assertSame(12, $classes['Example']['methods']['even']['endLine']);
165
166        $this->assertSame(7, $classes['Example']['methods']['anonymousFunction:7#28']['startLine']);
167        $this->assertSame(9, $classes['Example']['methods']['anonymousFunction:7#28']['endLine']);
168    }
169}
170