|
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 Spiral\Reactor\ClassDeclaration; |
|
16
|
|
|
use Spiral\Reactor\FileDeclaration; |
|
17
|
|
|
use Spiral\Reactor\NamespaceDeclaration; |
|
18
|
|
|
use Spiral\Reactor\Partial; |
|
19
|
|
|
|
|
20
|
|
|
class ReplaceTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testReplace(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$declaration = new ClassDeclaration('MyClass'); |
|
25
|
|
|
$declaration->setExtends('Record'); |
|
26
|
|
|
|
|
27
|
|
|
$declaration->property('names') |
|
28
|
|
|
->setAccess(Partial\Property::ACCESS_PRIVATE) |
|
29
|
|
|
->setComment(['This is foxes', '', '@var array']) |
|
30
|
|
|
->setDefaultValue(['name' => 11, 'value' => 'hi', 'test' => []]); |
|
31
|
|
|
|
|
32
|
|
|
$method = $declaration->method('sample'); |
|
33
|
|
|
$method->parameter('input')->setType('int'); |
|
34
|
|
|
$method->parameter('output')->setType('int')->setDefaultValue(null)->setPBR(true); |
|
35
|
|
|
$method->setAccess(Partial\Method::ACCESS_PUBLIC)->setStatic(true); |
|
36
|
|
|
$method->setComment('Get some foxes'); |
|
37
|
|
|
|
|
38
|
|
|
$method->setSource([ |
|
39
|
|
|
'$output = $input;', |
|
40
|
|
|
'return true;' |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
$declaration->addTrait('Spiral\Debug\Traits\LoggerTrait'); |
|
44
|
|
|
$this->assertTrue($declaration->hasTrait('Spiral\Debug\Traits\LoggerTrait')); |
|
45
|
|
|
|
|
46
|
|
|
$namespace = new NamespaceDeclaration('Namespace'); |
|
47
|
|
|
$namespace->setComment('All about foxes'); |
|
48
|
|
|
|
|
49
|
|
|
$namespace->addElement($declaration); |
|
50
|
|
|
|
|
51
|
|
|
$file = new FileDeclaration(); |
|
52
|
|
|
$file->getComment()->addLine('Full file of foxes'); |
|
53
|
|
|
$file->addElement($namespace); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertSame( |
|
56
|
|
|
preg_replace('/\s+/', '', '<?php |
|
57
|
|
|
/** |
|
58
|
|
|
* Full file of foxes |
|
59
|
|
|
*/ |
|
60
|
|
|
/** |
|
61
|
|
|
* All about foxes |
|
62
|
|
|
*/ |
|
63
|
|
|
namespace Namespace { |
|
64
|
|
|
class MyClass extends Record |
|
65
|
|
|
{ |
|
66
|
|
|
use Spiral\Debug\Traits\LoggerTrait; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* This is foxes |
|
70
|
|
|
* |
|
71
|
|
|
* @var array |
|
72
|
|
|
*/ |
|
73
|
|
|
private $names = [ |
|
74
|
|
|
\'name\' => 11, |
|
75
|
|
|
\'value\' => \'hi\', |
|
76
|
|
|
\'test\' => [] |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get some foxes |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function sample(int $input, int &$output = null) |
|
83
|
|
|
{ |
|
84
|
|
|
$output = $input; |
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
}'), |
|
89
|
|
|
preg_replace('/\s+/', '', $file->render()) |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
$file->replace('foxes', 'dogs'); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertSame( |
|
95
|
|
|
preg_replace('/\s+/', '', '<?php |
|
96
|
|
|
/** |
|
97
|
|
|
* Full file of dogs |
|
98
|
|
|
*/ |
|
99
|
|
|
/** |
|
100
|
|
|
* All about dogs |
|
101
|
|
|
*/ |
|
102
|
|
|
namespace Namespace { |
|
103
|
|
|
class MyClass extends Record |
|
104
|
|
|
{ |
|
105
|
|
|
use Spiral\Debug\Traits\LoggerTrait; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* This is dogs |
|
109
|
|
|
* |
|
110
|
|
|
* @var array |
|
111
|
|
|
*/ |
|
112
|
|
|
private $names = [ |
|
113
|
|
|
\'name\' => 11, |
|
114
|
|
|
\'value\' => \'hi\', |
|
115
|
|
|
\'test\' => [] |
|
116
|
|
|
]; |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get some dogs |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function sample(int $input, int &$output = null) |
|
122
|
|
|
{ |
|
123
|
|
|
$output = $input; |
|
124
|
|
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
}'), |
|
128
|
|
|
preg_replace( |
|
129
|
|
|
'/\s+/', |
|
130
|
|
|
'', |
|
131
|
|
|
$file->render() |
|
132
|
|
|
) |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|