Skip to content

Commit 07442f3

Browse files
authored
Merge pull request #16 from dotkernel/issue-13
Issue #13: Codecov percentage optimizations
2 parents cc566fa + 7695181 commit 07442f3

14 files changed

Lines changed: 376 additions & 76 deletions

src/Component/InterfaceFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function render(): string
153153
}
154154

155155
if ($this->comment !== '') {
156-
$interface .= PHP_EOL . $this->comment . PHP_EOL;
156+
$interface .= PHP_EOL . $this->comment;
157157
}
158158

159159
$interface .= PHP_EOL;

src/Type/InputFilter/DeleteResourceInputFilter.php

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,11 @@ public function create(string $name): File
3131
throw DuplicateFileException::create($inputFilter);
3232
}
3333

34-
if ($this->context->isApi()) {
35-
$content = $this->renderApi(
36-
$name,
37-
$inputFilter->getComponent(),
38-
);
39-
} else {
40-
$content = $this->render(
41-
$name,
42-
$inputFilter->getComponent(),
43-
$this->fileSystem->confirmDeleteInput($name)->getComponent(),
44-
);
45-
}
34+
$content = $this->render(
35+
$name,
36+
$inputFilter->getComponent(),
37+
$this->fileSystem->confirmDeleteInput($name)->getComponent(),
38+
);
4639

4740
$inputFilter->create($content);
4841

@@ -77,25 +70,4 @@ public function render(string $name, Component $inputFilter, Component $input):
7770

7871
return $class->render();
7972
}
80-
81-
public function renderApi(string $name, Component $inputFilter): string
82-
{
83-
$class = (new ClassFile($inputFilter->getNamespace(), $inputFilter->getClassName()))
84-
->setExtends('AbstractInputFilter')
85-
->useClass($this->import->getAbstractInputFilterFqcn())
86-
->setComment(<<<COMM
87-
/**
88-
* @phpstan-type Delete{$name}DataType array{}
89-
* @extends AbstractInputFilter<Delete{$name}DataType>
90-
*/
91-
COMM);
92-
93-
$init = (new Constructor())
94-
->setBody(<<<BODY
95-
// chain inputs here
96-
BODY);
97-
$class->addMethod($init);
98-
99-
return $class->render();
100-
}
10173
}

test/ColorEnumTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Maker;
6+
7+
use Dot\Maker\ColorEnum;
8+
use Dot\Maker\IO\Output;
9+
use PHPUnit\Framework\TestCase;
10+
11+
use function fclose;
12+
use function fopen;
13+
use function rewind;
14+
use function stream_get_contents;
15+
16+
class ColorEnumTest extends TestCase
17+
{
18+
/** @var resource $outputStream */
19+
private $outputStream;
20+
21+
protected function setUp(): void
22+
{
23+
$this->outputStream = fopen('php://memory', 'w+');
24+
Output::setOutputStream($this->outputStream);
25+
}
26+
27+
protected function tearDown(): void
28+
{
29+
fclose($this->outputStream);
30+
}
31+
32+
public function testWillRenderMessageWithBackgroundColor(): void
33+
{
34+
Output::write(
35+
ColorEnum::colorize('Test Message', ColorEnum::BackgroundBlack)
36+
);
37+
38+
rewind($this->outputStream);
39+
$this->assertSame("\033[40mTest Message\033[0m", stream_get_contents($this->outputStream));
40+
}
41+
42+
public function testWillRenderMessageWithForegroundColor(): void
43+
{
44+
Output::write(
45+
ColorEnum::colorize('Test Message', ColorEnum::ForegroundBrightGreen)
46+
);
47+
48+
rewind($this->outputStream);
49+
$this->assertSame("\033[92mTest Message\033[0m", stream_get_contents($this->outputStream));
50+
}
51+
52+
public function testWillRenderMessageWithBackgroundAndForegroundColor(): void
53+
{
54+
Output::write(
55+
ColorEnum::colorize(
56+
'Test Message',
57+
ColorEnum::ForegroundBrightGreen,
58+
ColorEnum::BackgroundBlack
59+
)
60+
);
61+
62+
rewind($this->outputStream);
63+
$this->assertSame("\033[92;40mTest Message\033[0m", stream_get_contents($this->outputStream));
64+
}
65+
}

test/Component/ClassFileTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ public function testWillRenderClass(): void
111111
->addInject(
112112
(new Inject('CustomInjector'))->addArgument('true', 'param')
113113
)
114+
->setAbstract(true)
115+
->setFinal(true)
116+
->setReadonly(true)
114117
->useClass('App\\Module\\OtherClassName')
115118
->useClass('App\\Module\\InterfaceName')
116119
->useFunction('sprintf')
@@ -158,7 +161,7 @@ public function testWillRenderClass(): void
158161
->appendBody('// add logic')
159162
);
160163

161-
$this->assertSame($this->dataProviderRenderedClass(), $actual->render());
164+
$this->assertSame($this->dataProviderRenderedClass(), (string) $actual);
162165
}
163166

164167
private function dataProviderRenderedClass(): string
@@ -178,7 +181,7 @@ private function dataProviderRenderedClass(): string
178181
use const PHP_EOL;
179182
180183
#[CustomInjector(param: true)]
181-
class ClassName extends OtherClassName implements InterfaceName
184+
final readonly abstract class ClassName extends OtherClassName implements InterfaceName
182185
{
183186
protected bool \$propertyName = true;
184187

test/Component/DeclarationTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,41 @@ public function testWillSetReturnType(): void
5959
$this->assertSame('self', $declaration->getReturnType());
6060
}
6161

62+
public function testWillRenderParameters(): void
63+
{
64+
$declaration = new Declaration($this->declarationName);
65+
$this->assertSame('', $declaration->renderParameters());
66+
67+
$declaration->addParameter(
68+
new Parameter('param', 'string')
69+
);
70+
$this->assertSame(' string $param,', $declaration->renderParameters());
71+
72+
$declaration->addParameter(
73+
new Parameter('other', 'string')
74+
);
75+
$this->assertSame(<<<BODY
76+
string \$param,
77+
string \$other,
78+
BODY, $declaration->renderParameters());
79+
}
80+
81+
public function testWillRenderSignature(): void
82+
{
83+
$declaration = new Declaration($this->declarationName);
84+
$this->assertSame(': void', $declaration->renderSignature());
85+
86+
$declaration->setReturnType('');
87+
$this->assertSame('', $declaration->renderSignature());
88+
89+
$declaration->setReturnType('self');
90+
$this->assertSame(': self', $declaration->renderSignature());
91+
92+
$declaration->setReturnType('string');
93+
$declaration->setNullable(true);
94+
$this->assertSame(': ?string', $declaration->renderSignature());
95+
}
96+
6297
public function testWillRender(): void
6398
{
6499
$comment = <<<COMM
@@ -74,7 +109,7 @@ public function testWillRender(): void
74109
->addParameter(
75110
new Parameter('param', 'string')
76111
);
77-
$this->assertSame($this->dataProviderRenderedDeclaration(), $declaration->render());
112+
$this->assertSame($this->dataProviderRenderedDeclaration(), (string) $declaration);
78113
}
79114

80115
private function dataProviderRenderedDeclaration(): string

test/Component/InterfaceFileTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,14 @@ public function testWillRenderInterface(): void
7373
->useClass('App\\Module\\OtherInterface')
7474
->useFunction('sprintf')
7575
->useConstant('PHP_EOL')
76+
->setComment(<<<COMM
77+
/**
78+
* Comment
79+
*/
80+
COMM)
7681
->addDeclaration((new Declaration('getResource'))->setReturnType('Resource'));
7782

78-
$this->assertSame($this->dataProviderRenderedClass(), $actual->render());
83+
$this->assertSame($this->dataProviderRenderedClass(), (string) $actual);
7984
}
8085

8186
private function dataProviderRenderedClass(): string
@@ -93,6 +98,9 @@ private function dataProviderRenderedClass(): string
9398
9499
use const PHP_EOL;
95100
101+
/**
102+
* Comment
103+
*/
96104
interface ClassName extends OtherInterface
97105
{
98106
public function getResource(): Resource;

test/Component/MethodTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ public function testWillSetVisibility(): void
108108
$this->assertSame(VisibilityEnum::Private, $method->getVisibility());
109109
}
110110

111-
public function testWillRender(): void
111+
public function testWillRenderMethodWithoutBody(): void
112+
{
113+
$method = (new Method($this->methodName))
114+
->setVisibility(VisibilityEnum::Private)
115+
->setNullable(false);
116+
$this->assertSame($this->dataProviderRenderedMethodWithoutBody(), (string) $method);
117+
}
118+
119+
public function testWillRenderMethod(): void
112120
{
113121
$method = (new Method($this->methodName))
114122
->setVisibility(VisibilityEnum::Private)
@@ -121,7 +129,16 @@ public function testWillRender(): void
121129
COMM)
122130
->addParameter(new Parameter('param', 'string', true, 'null'))
123131
->setBody(' return $this;');
124-
$this->assertSame($this->dataProviderRenderedMethod(), $method->render());
132+
$this->assertSame($this->dataProviderRenderedMethod(), (string) $method);
133+
}
134+
135+
private function dataProviderRenderedMethodWithoutBody(): string
136+
{
137+
return <<<BODY
138+
private function someMethod(): void
139+
{
140+
}
141+
BODY;
125142
}
126143

127144
private function dataProviderRenderedMethod(): string

test/Component/ParameterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ public function testWillSetNullable(): void
6969
public function testWillRender(): void
7070
{
7171
$parameter = new Parameter('param', 'string', true, '"test"');
72-
$this->assertSame('?string $param = "test"', $parameter->render());
72+
$this->assertSame('?string $param = "test"', (string) $parameter);
7373
}
7474
}

test/ComponentTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,15 @@ public function testWillRenderInputServiceInterface(): void
269269
$this->assertSame('BOOK_STORE_SERVICE_INTERFACE', $component->toUpperCase(false));
270270
$this->assertSame('BOOK_STORE_SERVICE', $component->toUpperCase());
271271
}
272+
273+
public function testWillPluralize(): void
274+
{
275+
$this->assertSame('buses', Component::pluralize('bus'));
276+
$this->assertSame('boxes', Component::pluralize('box'));
277+
$this->assertSame('jazzes', Component::pluralize('jazz'));
278+
$this->assertSame('fishes', Component::pluralize('fish'));
279+
$this->assertSame('watches', Component::pluralize('watch'));
280+
$this->assertSame('candies', Component::pluralize('candy'));
281+
$this->assertSame('books', Component::pluralize('book'));
282+
}
272283
}

test/IO/InputTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,16 @@ public function testConfirmWillOutputMessageAndReadNegativeAnswer(string $input)
111111
fclose($inputStream);
112112
fclose($outputStream);
113113
}
114+
115+
public function testWillAlwaysGetInputStream(): void
116+
{
117+
Input::setStream(fopen('php://memory', 'w+'));
118+
119+
$stream = Input::getStream();
120+
$this->assertIsResource($stream);
121+
fclose($stream);
122+
123+
$stream = Input::getStream();
124+
$this->assertIsResource($stream);
125+
}
114126
}

0 commit comments

Comments
 (0)