Passed
Push — master ( fdae84...25d981 )
by Aleksei
08:01
created

InvocationsTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 85
c 0
b 0
f 0
dl 0
loc 180
rs 10
1
<?php
2
3
/**
4
 * Spiral, Core Components
5
 *
6
 * @author Wolfy-J
7
 */
8
9
namespace Spiral\Tests\Tokenizer;
10
11
use PHPUnit\Framework\TestCase;
12
use Spiral\Tokenizer\Config\TokenizerConfig;
13
use Spiral\Tokenizer\Reflection\ReflectionArgument;
14
use Spiral\Tokenizer\Reflection\ReflectionInvocation;
15
use Spiral\Tokenizer\Tokenizer;
16
17
class InvocationsTest extends TestCase
18
{
19
    protected function someFunction()
20
    {
21
        $result = $this->sampleMethod('hello world');
22
        print_r(self::sampleMethod($result . 'plus'));
23
    }
24
25
    public function testInstance()
26
    {
27
        $invocations = $this->getInvocations();
28
        $this->assertCount(2, $invocations);
29
30
        $invocation1 = $invocations[0];
31
        $invocation2 = $invocations[1];
32
33
        $this->assertInstanceOf(ReflectionInvocation::class, $invocation1);
34
        $this->assertInstanceOf(ReflectionInvocation::class, $invocation2);
35
    }
36
37
    public function testClass()
38
    {
39
        $invocations = $this->getInvocations();
40
        $this->assertCount(2, $invocations);
41
42
        $invocation1 = $invocations[0];
43
        $invocation2 = $invocations[1];
44
45
        $this->assertSame(self::class, $invocation1->getClass());
46
        $this->assertSame(self::class, $invocation2->getClass());
47
    }
48
49
    public function testName()
50
    {
51
        $invocations = $this->getInvocations();
52
        $this->assertCount(2, $invocations);
53
54
        $invocation1 = $invocations[0];
55
        $invocation2 = $invocations[1];
56
57
        $this->assertSame('sampleMethod', $invocation1->getName());
58
        $this->assertSame('sampleMethod', $invocation2->getName());
59
    }
60
61
    public function testFilename()
62
    {
63
        $invocations = $this->getInvocations();
64
        $this->assertCount(2, $invocations);
65
66
        $invocation1 = $invocations[0];
67
        $invocation2 = $invocations[1];
68
69
        $this->assertSame(str_replace('\\', '/', __FILE__), $invocation1->getFilename());
70
        $this->assertSame(str_replace('\\', '/', __FILE__), $invocation2->getFilename());
71
    }
72
73
    public function testLine()
74
    {
75
        $invocations = $this->getInvocations();
76
        $this->assertCount(2, $invocations);
77
78
        $invocation1 = $invocations[0];
79
        $invocation2 = $invocations[1];
80
81
        $this->assertSame(21, $invocation1->getLine());
82
        $this->assertSame(22, $invocation2->getLine());
83
    }
84
85
    public function testLevel()
86
    {
87
        $invocations = $this->getInvocations();
88
        $this->assertCount(2, $invocations);
89
90
        $invocation1 = $invocations[0];
91
        $invocation2 = $invocations[1];
92
93
        $this->assertSame(0, $invocation1->getLevel());
94
        $this->assertSame(1, $invocation2->getLevel());
95
    }
96
97
    public function testOperator()
98
    {
99
        $invocations = $this->getInvocations();
100
        $this->assertCount(2, $invocations);
101
102
        $invocation1 = $invocations[0];
103
        $invocation2 = $invocations[1];
104
105
        $this->assertSame('->', $invocation1->getOperator());
106
        $this->assertSame('::', $invocation2->getOperator());
107
    }
108
109
    public function testIsMethod()
110
    {
111
        $invocations = $this->getInvocations();
112
        $this->assertCount(2, $invocations);
113
114
        $invocation1 = $invocations[0];
115
        $invocation2 = $invocations[1];
116
117
        $this->assertTrue($invocation1->isMethod());
118
        $this->assertTrue($invocation2->isMethod());
119
    }
120
121
    public function testCountArguments()
122
    {
123
        $invocations = $this->getInvocations();
124
        $this->assertCount(2, $invocations);
125
126
        $invocation1 = $invocations[0];
127
        $invocation2 = $invocations[1];
128
129
        $this->assertSame(1, $invocation1->countArguments());
130
        $this->assertSame(1, $invocation2->countArguments());
131
    }
132
133
    public function testSimpleArgument()
134
    {
135
        $invocations = $this->getInvocations();
136
        $this->assertCount(2, $invocations);
137
138
        $invocation1 = $invocations[0];
139
140
        $argument = $invocation1->getArgument(0);
141
142
        $this->assertInstanceOf(ReflectionArgument::class, $argument);
143
144
        $this->assertSame(ReflectionArgument::STRING, $argument->getType());
145
        $this->assertSame("'hello world'", $argument->getValue());
146
        $this->assertSame("hello world", $argument->stringValue());
147
    }
148
149
    public function testVariableArgument()
150
    {
151
        $invocations = $this->getInvocations();
152
        $this->assertCount(2, $invocations);
153
154
        $invocation2 = $invocations[1];
155
156
        $argument = $invocation2->getArgument(0);
157
158
        $this->assertInstanceOf(ReflectionArgument::class, $argument);
159
160
        $this->assertSame(ReflectionArgument::EXPRESSION, $argument->getType());
161
        $this->assertSame('$result.\'plus\'', $argument->getValue());
162
    }
163
164
    public function testSource()
165
    {
166
        $invocations = $this->getInvocations();
167
        $this->assertCount(2, $invocations);
168
169
        $invocation1 = $invocations[0];
170
        $invocation2 = $invocations[1];
171
172
        $this->assertSame('$this->sampleMethod(\'hello world\')', $invocation1->getSource());
173
        $this->assertSame('self::sampleMethod($result . \'plus\')', $invocation2->getSource());
174
    }
175
176
    protected static function sampleMethod(string $string)
177
    {
178
    }
179
180
    /**
181
     * @return ReflectionInvocation[]
182
     * @throws \ReflectionException
183
     */
184
    protected function getInvocations(): array
185
    {
186
        $tokenizer = new Tokenizer(new TokenizerConfig([
187
            'directories' => [__DIR__],
188
            'exclude'     => []
189
        ]));
190
191
        $locator = $tokenizer->invocationLocator();
192
193
        $method = new \ReflectionMethod($this, 'sampleMethod');
194
        $method->setAccessible(true);
195
196
        return $locator->getInvocations($method);
197
    }
198
}
199