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_FunctionTest extends TestCase
14{
15    /**
16     * @var PHP_Token_FUNCTION[]
17     */
18    private $functions;
19
20    protected function setUp()
21    {
22        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source.php');
23
24        foreach ($ts as $token) {
25            if ($token instanceof PHP_Token_FUNCTION) {
26                $this->functions[] = $token;
27            }
28        }
29    }
30
31    /**
32     * @covers PHP_Token_FUNCTION::getArguments
33     */
34    public function testGetArguments()
35    {
36        $this->assertEquals([], $this->functions[0]->getArguments());
37
38        $this->assertEquals(
39          ['$baz' => 'Baz'], $this->functions[1]->getArguments()
40        );
41
42        $this->assertEquals(
43          ['$foobar' => 'Foobar'], $this->functions[2]->getArguments()
44        );
45
46        $this->assertEquals(
47          ['$barfoo' => 'Barfoo'], $this->functions[3]->getArguments()
48        );
49
50        $this->assertEquals([], $this->functions[4]->getArguments());
51
52        $this->assertEquals(['$x' => null, '$y' => null], $this->functions[5]->getArguments());
53    }
54
55    /**
56     * @covers PHP_Token_FUNCTION::getName
57     */
58    public function testGetName()
59    {
60        $this->assertEquals('foo', $this->functions[0]->getName());
61        $this->assertEquals('bar', $this->functions[1]->getName());
62        $this->assertEquals('foobar', $this->functions[2]->getName());
63        $this->assertEquals('barfoo', $this->functions[3]->getName());
64        $this->assertEquals('baz', $this->functions[4]->getName());
65    }
66
67    /**
68     * @covers PHP_Token::getLine
69     */
70    public function testGetLine()
71    {
72        $this->assertEquals(5, $this->functions[0]->getLine());
73        $this->assertEquals(10, $this->functions[1]->getLine());
74        $this->assertEquals(17, $this->functions[2]->getLine());
75        $this->assertEquals(21, $this->functions[3]->getLine());
76        $this->assertEquals(29, $this->functions[4]->getLine());
77    }
78
79    /**
80     * @covers PHP_TokenWithScope::getEndLine
81     */
82    public function testGetEndLine()
83    {
84        $this->assertEquals(5, $this->functions[0]->getEndLine());
85        $this->assertEquals(12, $this->functions[1]->getEndLine());
86        $this->assertEquals(19, $this->functions[2]->getEndLine());
87        $this->assertEquals(23, $this->functions[3]->getEndLine());
88        $this->assertEquals(31, $this->functions[4]->getEndLine());
89    }
90
91    /**
92     * @covers PHP_Token_FUNCTION::getDocblock
93     */
94    public function testGetDocblock()
95    {
96        $this->assertNull($this->functions[0]->getDocblock());
97
98        $this->assertEquals(
99          "/**\n     * @param Baz \$baz\n     */",
100          $this->functions[1]->getDocblock()
101        );
102
103        $this->assertEquals(
104          "/**\n     * @param Foobar \$foobar\n     */",
105          $this->functions[2]->getDocblock()
106        );
107
108        $this->assertNull($this->functions[3]->getDocblock());
109        $this->assertNull($this->functions[4]->getDocblock());
110    }
111
112    public function testSignature()
113    {
114        $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source5.php');
115        $f  = $ts->getFunctions();
116        $c  = $ts->getClasses();
117        $i  = $ts->getInterfaces();
118
119        $this->assertEquals(
120          'foo($a, array $b, array $c = array())',
121          $f['foo']['signature']
122        );
123
124        $this->assertEquals(
125          'm($a, array $b, array $c = array())',
126          $c['c']['methods']['m']['signature']
127        );
128
129        $this->assertEquals(
130          'm($a, array $b, array $c = array())',
131          $c['a']['methods']['m']['signature']
132        );
133
134        $this->assertEquals(
135          'm($a, array $b, array $c = array())',
136          $i['i']['methods']['m']['signature']
137        );
138    }
139}
140