-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClassFileTest.php
More file actions
221 lines (187 loc) · 7.16 KB
/
Copy pathClassFileTest.php
File metadata and controls
221 lines (187 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
declare(strict_types=1);
namespace Component;
use Dot\Maker\Component\ClassFile;
use Dot\Maker\Component\Inject;
use Dot\Maker\Component\Method;
use Dot\Maker\Component\Parameter;
use Dot\Maker\Component\PromotedProperty;
use Dot\Maker\Component\Property;
use Dot\Maker\VisibilityEnum;
use PHPUnit\Framework\TestCase;
class ClassFileTest extends TestCase
{
private string $namespace = 'App\\Module\\Directory';
private string $className = 'ClassName';
public function testWillInstantiate(): void
{
$classFile = new ClassFile($this->namespace, $this->className);
$this->assertContainsOnlyInstancesOf(ClassFile::class, [$classFile]);
$this->assertSame($this->namespace, $classFile->namespace);
$this->assertSame($this->className, $classFile->className);
}
public function testAccessorsWillGetAndSetProperties(): void
{
$classFile = new ClassFile($this->namespace, $this->className);
$this->assertCount(0, $classFile->getInjects());
$classFile->addInject(new Inject('injectName'));
$this->assertCount(1, $classFile->getInjects());
$this->assertCount(0, $classFile->getInterfaces());
$classFile->addInterface('InterfaceName');
$this->assertCount(1, $classFile->getInterfaces());
$this->assertCount(0, $classFile->getMethods());
$classFile->addMethod(new Method('MethodName'));
$this->assertCount(1, $classFile->getMethods());
$this->assertTrue($classFile->hasMethod('MethodName'));
$this->assertCount(0, $classFile->getProperties());
$classFile->addProperty(new Property('PropertyName', 'string'));
$this->assertCount(1, $classFile->getProperties());
$this->assertCount(0, $classFile->getTraits());
$classFile->addTrait('TraitName');
$this->assertCount(1, $classFile->getTraits());
$this->assertSame([
'use TraitName;' => 'use TraitName;',
], $classFile->getTraits());
$this->assertCount(0, $classFile->getClassUses());
$classFile->useClass('App\\ModuleA\\ClassName');
$this->assertCount(1, $classFile->getClassUses());
$classFile->useClass('App\\ModuleB\\ClassName', 'OtherClassName');
$this->assertCount(2, $classFile->getClassUses());
$this->assertSame([
'use App\\ModuleA\\ClassName;' => 'use App\\ModuleA\\ClassName;',
'use App\\ModuleB\\ClassName as OtherClassName;' => 'use App\\ModuleB\\ClassName as OtherClassName;',
], $classFile->getClassUses());
$this->assertCount(0, $classFile->getFunctionUses());
$classFile->useFunction('some_function');
$this->assertCount(1, $classFile->getFunctionUses());
$this->assertSame([
'use function some_function;' => 'use function some_function;',
], $classFile->getFunctionUses());
$this->assertCount(0, $classFile->getConstantUses());
$classFile->useConstant('SOME_CONSTANT');
$this->assertCount(1, $classFile->getConstantUses());
$this->assertSame([
'use const SOME_CONSTANT;' => 'use const SOME_CONSTANT;',
], $classFile->getConstantUses());
$this->assertFalse($classFile->isAbstract());
$classFile->setAbstract(true);
$this->assertTrue($classFile->isAbstract());
$this->assertFalse($classFile->isReadonly());
$classFile->setReadonly(true);
$this->assertTrue($classFile->isReadonly());
$this->assertFalse($classFile->isFinal());
$classFile->setFinal(true);
$this->assertTrue($classFile->isFinal());
$this->assertTrue($classFile->isStrictTypes());
$classFile->setStrictTypes(false);
$this->assertFalse($classFile->isStrictTypes());
$this->assertNull($classFile->getExtends());
$classFile->setExtends('OtherClassName');
$this->assertSame('OtherClassName', $classFile->getExtends());
$this->assertSame('', $classFile->getComment());
$classFile->setComment('Comment');
$this->assertSame('Comment', $classFile->getComment());
}
public function testWillRenderClass(): void
{
$actual = (new ClassFile($this->namespace, $this->className))
->setExtends('OtherClassName')
->addInterface('InterfaceName')
->addInject(
(new Inject('CustomInjector'))->addArgument('true', 'param')
)
->setAbstract(true)
->setFinal(true)
->setReadonly(true)
->useClass('App\\Module\\OtherClassName')
->useClass('App\\Module\\InterfaceName')
->useFunction('sprintf')
->useConstant('PHP_EOL')
->addProperty(
new Property('propertyName', 'bool', false, 'true')
)
->addMethod(
(new Method\Constructor())
->addInject(
(new Inject())
->addArgument('SomeClass::class')
->addArgument('"config"')
)
->addPromotedProperty(
new PromotedProperty('instance', 'SomeClass')
)
->addPromotedProperty(
new PromotedProperty('config', 'array')
)
)
->addMethod(
(new Method('publicMethodName'))
->setComment(<<<COMM
/**
* @param string \$param
* @return bool
*/
COMM)
->setReturnType('bool')
->setNullable(true)
->addParameter(
new Parameter('param', 'string')
)
->appendBody('return true;')
)
->addMethod(
(new Method('protectedMethodName'))
->setVisibility(VisibilityEnum::Protected)
->appendBody('// add logic')
)
->addMethod(
(new Method('privateMethodName'))
->setVisibility(VisibilityEnum::Private)
->appendBody('// add logic')
);
$this->assertSame($this->dataProviderRenderedClass(), (string) $actual);
}
private function dataProviderRenderedClass(): string
{
return <<<BODY
<?php
declare(strict_types=1);
namespace App\Module\Directory;
use App\Module\InterfaceName;
use App\Module\OtherClassName;
use function sprintf;
use const PHP_EOL;
#[CustomInjector(param: true)]
final readonly abstract class ClassName extends OtherClassName implements InterfaceName
{
protected bool \$propertyName = true;
#[Inject(
SomeClass::class,
"config",
)]
public function __construct(
protected SomeClass \$instance,
protected array \$config,
) {
}
/**
* @param string \$param
* @return bool
*/
public function publicMethodName(
string \$param,
): ?bool {
return true;
}
protected function protectedMethodName(): void
{
// add logic
}
private function privateMethodName(): void
{
// add logic
}
}
BODY;
}
}