Skip to content

Commit ad51354

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

2 files changed

Lines changed: 114 additions & 32 deletions

File tree

test/MakerTest.php

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,48 @@
55
namespace DotTest\Maker;
66

77
use Dot\Maker\ColorEnum;
8+
use Dot\Maker\IO\Input;
89
use Dot\Maker\IO\Output;
910
use Dot\Maker\Maker;
1011
use org\bovigo\vfs\vfsStream;
1112
use PHPUnit\Framework\TestCase;
1213

1314
use function fclose;
1415
use function fopen;
16+
use function fwrite;
1517
use function rewind;
1618
use function stream_get_contents;
1719

1820
use const PHP_EOL;
1921

2022
class MakerTest extends TestCase
2123
{
24+
/** @var resource $outputStream */
25+
private $outputStream;
26+
/** @var resource $inputStream */
27+
private $inputStream;
28+
/** @var resource $errorStream */
29+
private $errorStream;
30+
31+
protected function setUp(): void
32+
{
33+
$this->errorStream = fopen('php://memory', 'w+');
34+
Output::setErrorStream($this->errorStream);
35+
36+
$this->inputStream = fopen('php://memory', 'w+');
37+
Input::setStream($this->inputStream);
38+
39+
$this->outputStream = fopen('php://memory', 'w+');
40+
Output::setOutputStream($this->outputStream);
41+
}
42+
43+
protected function tearDown(): void
44+
{
45+
fclose($this->outputStream);
46+
fclose($this->inputStream);
47+
fclose($this->errorStream);
48+
}
49+
2250
public function testWillInstantiate(): void
2351
{
2452
$maker = new Maker('');
@@ -27,23 +55,18 @@ public function testWillInstantiate(): void
2755

2856
public function testInvokeWillOutputErrorWhenNotInCliMode(): void
2957
{
30-
$errorStream = fopen('php://memory', 'w+');
31-
Output::setErrorStream($errorStream);
32-
3358
$maker = $this->getMockBuilder(Maker::class)
3459
->onlyMethods(['isCli'])
3560
->setConstructorArgs([''])
3661
->getMock();
3762
$maker->method('isCli')->willReturn(false);
3863
$maker([]);
3964

40-
rewind($errorStream);
65+
rewind($this->errorStream);
4166
$this->assertSame(
4267
ColorEnum::colorize('dot-maker must be run in CLI only', ColorEnum::ForegroundBrightRed) . PHP_EOL,
43-
stream_get_contents($errorStream)
68+
stream_get_contents($this->errorStream)
4469
);
45-
46-
fclose($errorStream);
4770
}
4871

4972
public function testInvokeWillOutputErrorWhenInvalidComponent(): void
@@ -59,29 +82,20 @@ public function testInvokeWillOutputErrorWhenInvalidComponent(): void
5982
}',
6083
]);
6184

62-
$errorStream = fopen('php://memory', 'w+');
63-
Output::setErrorStream($errorStream);
64-
65-
$outputStream = fopen('php://memory', 'w+');
66-
Output::setOutputStream($outputStream);
67-
6885
$maker = $this->getMockBuilder(Maker::class)
6986
->onlyMethods(['isCli'])
7087
->setConstructorArgs([$root->url()])
7188
->getMock();
7289
$maker->method('isCli')->willReturn(true);
7390
$maker(['', 'invalid-component']);
7491

75-
rewind($errorStream);
76-
rewind($outputStream);
92+
rewind($this->errorStream);
93+
rewind($this->outputStream);
7794
$this->assertSame(
7895
ColorEnum::colorize('Unknown component: "invalid-component"', ColorEnum::ForegroundBrightRed) . PHP_EOL,
79-
stream_get_contents($errorStream)
96+
stream_get_contents($this->errorStream)
8097
);
81-
$this->assertEmpty(stream_get_contents($outputStream));
82-
83-
fclose($errorStream);
84-
fclose($outputStream);
98+
$this->assertEmpty(stream_get_contents($this->outputStream));
8599
}
86100

87101
public function testInvokeWithoutArgsWillOutputHelpText(): void
@@ -97,28 +111,54 @@ public function testInvokeWithoutArgsWillOutputHelpText(): void
97111
}',
98112
]);
99113

100-
$errorStream = fopen('php://memory', 'w+');
101-
Output::setErrorStream($errorStream);
102-
103-
$outputStream = fopen('php://memory', 'w+');
104-
Output::setOutputStream($outputStream);
105-
106114
$maker = $this->getMockBuilder(Maker::class)
107115
->onlyMethods(['isCli'])
108116
->setConstructorArgs([$root->url()])
109117
->getMock();
110118
$maker->method('isCli')->willReturn(true);
111119
$maker(['', '']);
112120

113-
rewind($errorStream);
114-
rewind($outputStream);
121+
rewind($this->errorStream);
122+
rewind($this->outputStream);
115123
$this->assertSame(
116124
Helper::getHelpText(),
117-
stream_get_contents($outputStream)
125+
stream_get_contents($this->outputStream)
118126
);
119-
$this->assertEmpty(stream_get_contents($errorStream));
127+
$this->assertEmpty(stream_get_contents($this->errorStream));
128+
}
129+
130+
public function testCallingInvokeWithArgsModuleWillOutputDebugInfo(): void
131+
{
132+
$root = vfsStream::setup('root', 0644, [
133+
'composer.json' => '{
134+
"autoload": {
135+
"psr-4": {
136+
"Api\\\\App\\\\": "src/App/src/"
137+
}
138+
}
139+
}',
140+
]);
141+
142+
fwrite($this->inputStream, PHP_EOL);
143+
rewind($this->inputStream);
120144

121-
fclose($errorStream);
122-
fclose($outputStream);
145+
$maker = $this->getMockBuilder(Maker::class)
146+
->onlyMethods(['isCli'])
147+
->setConstructorArgs([$root->url()])
148+
->getMock();
149+
$maker->method('isCli')->willReturn(true);
150+
$maker(['', 'module']);
151+
152+
rewind($this->outputStream);
153+
$this->assertSame(
154+
<<<BODY
155+
\033[94mDetected project type: Api\033[0m
156+
\033[94mCore architecture: No\033[0m
157+
158+
New module name:
159+
BODY,
160+
stream_get_contents($this->outputStream)
161+
);
162+
$this->assertEmpty(stream_get_contents($this->errorStream));
123163
}
124164
}

test/Type/TypeEnumTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Maker\Type;
6+
7+
use Dot\Maker\Type\Collection;
8+
use Dot\Maker\Type\Command;
9+
use Dot\Maker\Type\Entity;
10+
use Dot\Maker\Type\Form;
11+
use Dot\Maker\Type\Handler;
12+
use Dot\Maker\Type\Help;
13+
use Dot\Maker\Type\Input;
14+
use Dot\Maker\Type\InputFilter;
15+
use Dot\Maker\Type\Middleware;
16+
use Dot\Maker\Type\Module;
17+
use Dot\Maker\Type\Repository;
18+
use Dot\Maker\Type\Service;
19+
use Dot\Maker\Type\ServiceInterface;
20+
use Dot\Maker\Type\TypeEnum;
21+
use PHPUnit\Framework\TestCase;
22+
23+
class TypeEnumTest extends TestCase
24+
{
25+
public function testWillMatchCorrectly(): void
26+
{
27+
$this->assertSame(Help::class, TypeEnum::getClass(''));
28+
$this->assertSame(Collection::class, TypeEnum::getClass('collection'));
29+
$this->assertSame(Command::class, TypeEnum::getClass('command'));
30+
$this->assertSame(Entity::class, TypeEnum::getClass('entity'));
31+
$this->assertSame(Form::class, TypeEnum::getClass('form'));
32+
$this->assertSame(Handler::class, TypeEnum::getClass('handler'));
33+
$this->assertSame(Input::class, TypeEnum::getClass('input'));
34+
$this->assertSame(InputFilter::class, TypeEnum::getClass('input-filter'));
35+
$this->assertSame(Middleware::class, TypeEnum::getClass('middleware'));
36+
$this->assertSame(Module::class, TypeEnum::getClass('module'));
37+
$this->assertSame(Repository::class, TypeEnum::getClass('repository'));
38+
$this->assertSame(Service::class, TypeEnum::getClass('service'));
39+
$this->assertSame(ServiceInterface::class, TypeEnum::getClass('service-interface'));
40+
$this->assertNull(TypeEnum::getClass('test'));
41+
}
42+
}

0 commit comments

Comments
 (0)