-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMethodTest.php
More file actions
157 lines (136 loc) · 4.46 KB
/
Copy pathMethodTest.php
File metadata and controls
157 lines (136 loc) · 4.46 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
<?php
declare(strict_types=1);
namespace Component;
use Dot\Maker\Component\Inject;
use Dot\Maker\Component\Method;
use Dot\Maker\Component\Parameter;
use Dot\Maker\VisibilityEnum;
use PHPUnit\Framework\TestCase;
use const PHP_EOL;
class MethodTest extends TestCase
{
private string $methodName = 'someMethod';
public function testWillInstantiate(): void
{
$method = new Method($this->methodName);
$this->assertContainsOnlyInstancesOf(Method::class, [$method]);
$this->assertSame($this->methodName, $method->getName());
$this->assertSame($this->methodName, $method->name);
}
public function testWillSetBody(): void
{
$method = new Method($this->methodName);
$method->setBody(' // do stuff');
$this->assertSame(PHP_EOL . ' // do stuff', $method->getBody());
$method->prependBody('// prepend comment');
$this->assertSame(<<<BODY
// prepend comment
// do stuff
BODY, $method->getBody());
$method->appendBody('// append comment');
$this->assertSame(<<<BODY
// prepend comment
// do stuff
// append comment
BODY, $method->getBody());
}
public function testWillAddInject(): void
{
$method = new Method($this->methodName);
$this->assertCount(0, $method->getInjects());
$method->addInject(new Inject());
$this->assertCount(1, $method->getInjects());
}
public function testWillAddParameter(): void
{
$method = new Method($this->methodName);
$this->assertCount(0, $method->getParameters());
$method->addParameter(new Parameter('param', 'string'));
$this->assertCount(1, $method->getParameters());
}
public function testWillCommentBody(): void
{
$method = new Method($this->methodName);
$method->setBody('return true;');
$method->commentBody();
$this->assertSame(PHP_EOL . '// return true;', $method->getBody());
}
public function testWillAddComment(): void
{
$comment = <<<COMM
/**
* Some comment
*/
COMM;
$method = new Method($this->methodName);
$this->assertSame('', $method->getComment());
$method->setComment($comment);
$this->assertSame($comment, $method->getComment());
}
public function testWillMakeNullable(): void
{
$method = new Method($this->methodName);
$this->assertFalse($method->isNullable());
$method->setNullable(true);
$this->assertTrue($method->isNullable());
}
public function testWillSetReturnType(): void
{
$method = new Method($this->methodName);
$this->assertSame('void', $method->getReturnType());
$method->setReturnType('bool');
$this->assertSame('bool', $method->getReturnType());
}
public function testWillSetVisibility(): void
{
$method = new Method($this->methodName);
$this->assertSame(VisibilityEnum::Public, $method->getVisibility());
$method->setVisibility(VisibilityEnum::Protected);
$this->assertSame(VisibilityEnum::Protected, $method->getVisibility());
$method->setVisibility(VisibilityEnum::Private);
$this->assertSame(VisibilityEnum::Private, $method->getVisibility());
}
public function testWillRenderMethodWithoutBody(): void
{
$method = (new Method($this->methodName))
->setVisibility(VisibilityEnum::Private)
->setNullable(false);
$this->assertSame($this->dataProviderRenderedMethodWithoutBody(), (string) $method);
}
public function testWillRenderMethod(): void
{
$method = (new Method($this->methodName))
->setVisibility(VisibilityEnum::Private)
->setReturnType('self')
->setNullable(false)
->setComment(<<<COMM
/**
* @param string \$param
*/
COMM)
->addParameter(new Parameter('param', 'string', true, 'null'))
->setBody(' return $this;');
$this->assertSame($this->dataProviderRenderedMethod(), (string) $method);
}
private function dataProviderRenderedMethodWithoutBody(): string
{
return <<<BODY
private function someMethod(): void
{
}
BODY;
}
private function dataProviderRenderedMethod(): string
{
return <<<BODY
/**
* @param string \$param
*/
private function someMethod(
?string \$param = null,
): self {
return \$this;
}
BODY;
}
}