1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Reactor; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use ReflectionException; |
16
|
|
|
use Spiral\Reactor\ClassDeclaration; |
17
|
|
|
use Spiral\Reactor\Partial\Method; |
18
|
|
|
use Spiral\Reactor\Partial\Parameter; |
19
|
|
|
use Spiral\Reactor\Partial\Property; |
20
|
|
|
|
21
|
|
|
class PartialsTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @throws ReflectionException |
25
|
|
|
*/ |
26
|
|
|
public function testParameter(): void |
27
|
|
|
{ |
28
|
|
|
$p = new Parameter('name'); |
29
|
|
|
$this->assertSame('name', $p->getName()); |
30
|
|
|
$this->assertFalse($p->isOptional()); |
31
|
|
|
$this->assertNull($p->getDefaultValue()); |
32
|
|
|
$this->assertFalse($p->isPBR()); |
33
|
|
|
|
34
|
|
|
$p->setDefaultValue('default'); |
35
|
|
|
$this->assertTrue($p->isOptional()); |
36
|
|
|
$this->assertSame('default', $p->getDefaultValue()); |
37
|
|
|
|
38
|
|
|
$p->setPBR(true); |
39
|
|
|
$this->assertTrue($p->isPBR()); |
40
|
|
|
|
41
|
|
|
$this->assertSame("&\$name = 'default'", $p->render()); |
42
|
|
|
|
43
|
|
|
$this->assertSame('', $p->getType()); |
44
|
|
|
$p->setType('int'); |
45
|
|
|
$this->assertSame('int', $p->getType()); |
46
|
|
|
$this->assertSame("int &\$name = 'default'", $p->render()); |
47
|
|
|
|
48
|
|
|
$p->removeDefaultValue(); |
49
|
|
|
$this->assertSame('int &$name', $p->render()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @throws ReflectionException |
54
|
|
|
*/ |
55
|
|
|
public function testProperty(): void |
56
|
|
|
{ |
57
|
|
|
$p = new Property('name'); |
58
|
|
|
$this->assertSame('name', $p->getName()); |
59
|
|
|
$this->assertFalse($p->hasDefaultValue()); |
60
|
|
|
$this->assertNull($p->getDefaultValue()); |
61
|
|
|
|
62
|
|
|
$this->assertSame('private $name;', $p->render()); |
63
|
|
|
|
64
|
|
|
$p = new Property('name', 10); |
65
|
|
|
$this->assertSame('name', $p->getName()); |
66
|
|
|
$this->assertTrue($p->hasDefaultValue()); |
67
|
|
|
$this->assertSame(10, $p->getDefaultValue()); |
68
|
|
|
$this->assertSame('private $name = 10;', $p->render()); |
69
|
|
|
|
70
|
|
|
$p->removeDefaultValue(); |
71
|
|
|
|
72
|
|
|
$this->assertFalse($p->hasDefaultValue()); |
73
|
|
|
$this->assertNull($p->getDefaultValue()); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testMethod(): void |
77
|
|
|
{ |
78
|
|
|
$m = new Method('method'); |
79
|
|
|
$this->assertSame('method', $m->getName()); |
80
|
|
|
$this->assertFalse($m->isStatic()); |
81
|
|
|
|
82
|
|
|
$m->setStatic(true); |
83
|
|
|
$this->assertTrue($m->isStatic()); |
84
|
|
|
|
85
|
|
|
$m->parameter('name')->setDefaultValue('value'); |
86
|
|
|
$this->assertCount(1, $m->getParameters()); |
87
|
|
|
|
88
|
|
|
$m->setSource('return $name;'); |
89
|
|
|
|
90
|
|
|
$this->assertSame(preg_replace('/\s+/', '', "private static function method(\$name = 'value') |
91
|
|
|
{ |
92
|
|
|
return \$name; |
93
|
|
|
}"), preg_replace('/\s+/', '', $m->render())); |
94
|
|
|
|
95
|
|
|
$m2 = new Method('method', ['return true;'], 'some method'); |
96
|
|
|
$m2->setPublic(); |
97
|
|
|
|
98
|
|
|
$this->assertSame(preg_replace('/\s+/', '', '/** |
99
|
|
|
* some method |
100
|
|
|
*/ |
101
|
|
|
public function method() |
102
|
|
|
{ |
103
|
|
|
return true; |
104
|
|
|
}'), preg_replace('/\s+/', '', $m2->render())); |
105
|
|
|
|
106
|
|
|
$m3 = new Method('method', 'return true;', ['some method']); |
107
|
|
|
$m3->setProtected(); |
108
|
|
|
|
109
|
|
|
$this->assertSame(preg_replace('/\s+/', '', '/** |
110
|
|
|
* some method |
111
|
|
|
*/ |
112
|
|
|
protected function method() |
113
|
|
|
{ |
114
|
|
|
return true; |
115
|
|
|
}'), preg_replace('/\s+/', '', $m3->render())); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testDefaultsInClass(): void |
119
|
|
|
{ |
120
|
|
|
$c = new ClassDeclaration('TestClass'); |
121
|
|
|
$c->constant('SCHEMA')->setValue([ |
122
|
|
|
'key' => 'value' |
123
|
|
|
])->setProtected(); |
124
|
|
|
|
125
|
|
|
$c->property('schema')->setDefaultValue([ |
126
|
|
|
'key' => 'value' |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
$this->assertSame(preg_replace('/\s+/', '', "class TestClass |
130
|
|
|
{ |
131
|
|
|
protected const SCHEMA = [ |
132
|
|
|
'key' => 'value' |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
private \$schema = [ |
136
|
|
|
'key' => 'value' |
137
|
|
|
]; |
138
|
|
|
}"), preg_replace('/\s+/', '', $c->render())); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.