Skip to content

Commit cdbb844

Browse files
committed
Issue #13: Codecov percentage optimizations
Signed-off-by: alexmerlin <alex.merlin.1985@gmail.com>
1 parent ab41082 commit cdbb844

10 files changed

Lines changed: 204 additions & 8 deletions

File tree

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;

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/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
}

test/IO/OutputTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,28 @@ public function testWillWriteLine(): void
169169

170170
fclose($stream);
171171
}
172+
173+
public function testWillAlwaysGetErrorStream(): void
174+
{
175+
Output::setErrorStream(fopen('php://memory', 'w+'));
176+
177+
$stream = Output::getErrorStream();
178+
$this->assertIsResource($stream);
179+
fclose($stream);
180+
181+
$stream = Output::getErrorStream();
182+
$this->assertIsResource($stream);
183+
}
184+
185+
public function testWillAlwaysGetOutputStream(): void
186+
{
187+
Output::setOutputStream(fopen('php://memory', 'w+'));
188+
189+
$stream = Output::getOutputStream();
190+
$this->assertIsResource($stream);
191+
fclose($stream);
192+
193+
$stream = Output::getOutputStream();
194+
$this->assertIsResource($stream);
195+
}
172196
}

test/Type/ModuleTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ protected function tearDown(): void
5757
fclose($this->outputStream);
5858
}
5959

60+
public function testWillCallAccessors(): void
61+
{
62+
$root = vfsStream::setup('root', 0644, [
63+
'composer.json' => '{
64+
"autoload": {
65+
"psr-4": {
66+
"Api\\\\App\\\\": "src/App/src/"
67+
}
68+
}
69+
}',
70+
]);
71+
72+
$config = new Config($root->url());
73+
$context = new Context($root->url());
74+
$fileSystem = (new FileSystem($context))->setModuleName($this->moduleName);
75+
76+
fwrite($this->inputStream, PHP_EOL);
77+
rewind($this->inputStream);
78+
79+
$module = new Module($fileSystem, $context, $config);
80+
$this->assertContainsOnlyInstancesOf(Config::class, [$module->getConfig()]);
81+
$this->assertContainsOnlyInstancesOf(Context::class, [$module->getContext()]);
82+
$this->assertContainsOnlyInstancesOf(FileSystem::class, [$module->getFileSystem()]);
83+
84+
$this->assertTrue($module->isModule());
85+
$this->assertNull($module->getModule());
86+
$this->assertFalse($module->hasModule());
87+
$module->setModule($module);
88+
$this->assertContainsOnlyInstancesOf(Module::class, [$module->getModule()]);
89+
$this->assertTrue($module->hasModule());
90+
}
91+
6092
public function testCallToInvokeWillEarlyReturnOnEmptyInput(): void
6193
{
6294
$root = vfsStream::setup('root', 0644, [

0 commit comments

Comments
 (0)