Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .roave-backward-compatibility-check.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/roave/backward-compatibility-check/resources/schema.xsd">
<baseline>
<ignored-regex>#\[BC\] SKIPPED: An enum expression Dot\Maker\\VisibilityEnum\:\:Public is not supported in class Dot\\Maker\\Component\\Method in file /src/Component/Method.php#</ignored-regex>
<ignored-regex>#\[BC\] SKIPPED: An enum expression Dot\Maker\\VisibilityEnum\:\:Protected is not supported in class Dot\\Maker\\Component\\Property in file /src/Component/Property.php#</ignored-regex>
<ignored-regex>#\[BC\] SKIPPED: An enum expression Dot\\Maker\\VisibilityEnum::Public is not supported in class Dot\\Maker\\Component\\Method in file \/src\/Component\/Method\.php \(line 20\)#</ignored-regex>
<ignored-regex>#\[BC\] SKIPPED: An enum expression Dot\\Maker\\VisibilityEnum::Protected is not supported in class Dot\\Maker\\Component\\Property in file \/src\/Component\/Property\.php \(line 15\)#</ignored-regex>
</baseline>
</roave-bc-check>
2 changes: 1 addition & 1 deletion src/Component/InterfaceFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function render(): string
}

if ($this->comment !== '') {
$interface .= PHP_EOL . $this->comment . PHP_EOL;
$interface .= PHP_EOL . $this->comment;
}

$interface .= PHP_EOL;
Expand Down
38 changes: 5 additions & 33 deletions src/Type/InputFilter/DeleteResourceInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,11 @@ public function create(string $name): File
throw DuplicateFileException::create($inputFilter);
}

if ($this->context->isApi()) {
$content = $this->renderApi(
$name,
$inputFilter->getComponent(),
);
} else {
$content = $this->render(
$name,
$inputFilter->getComponent(),
$this->fileSystem->confirmDeleteInput($name)->getComponent(),
);
}
$content = $this->render(
$name,
$inputFilter->getComponent(),
$this->fileSystem->confirmDeleteInput($name)->getComponent(),
);

$inputFilter->create($content);

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

return $class->render();
}

public function renderApi(string $name, Component $inputFilter): string
{
$class = (new ClassFile($inputFilter->getNamespace(), $inputFilter->getClassName()))
->setExtends('AbstractInputFilter')
->useClass($this->import->getAbstractInputFilterFqcn())
->setComment(<<<COMM
/**
* @phpstan-type Delete{$name}DataType array{}
* @extends AbstractInputFilter<Delete{$name}DataType>
*/
COMM);

$init = (new Constructor())
->setBody(<<<BODY
// chain inputs here
BODY);
$class->addMethod($init);

return $class->render();
}
}
65 changes: 65 additions & 0 deletions test/ColorEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace DotTest\Maker;

use Dot\Maker\ColorEnum;
use Dot\Maker\IO\Output;
use PHPUnit\Framework\TestCase;

use function fclose;
use function fopen;
use function rewind;
use function stream_get_contents;

class ColorEnumTest extends TestCase
{
/** @var resource $outputStream */
private $outputStream;

protected function setUp(): void
{
$this->outputStream = fopen('php://memory', 'w+');
Output::setOutputStream($this->outputStream);
}

protected function tearDown(): void
{
fclose($this->outputStream);
}

public function testWillRenderMessageWithBackgroundColor(): void
{
Output::write(
ColorEnum::colorize('Test Message', ColorEnum::BackgroundBlack)
);

rewind($this->outputStream);
$this->assertSame("\033[40mTest Message\033[0m", stream_get_contents($this->outputStream));
}

public function testWillRenderMessageWithForegroundColor(): void
{
Output::write(
ColorEnum::colorize('Test Message', ColorEnum::ForegroundBrightGreen)
);

rewind($this->outputStream);
$this->assertSame("\033[92mTest Message\033[0m", stream_get_contents($this->outputStream));
}

public function testWillRenderMessageWithBackgroundAndForegroundColor(): void
{
Output::write(
ColorEnum::colorize(
'Test Message',
ColorEnum::ForegroundBrightGreen,
ColorEnum::BackgroundBlack
)
);

rewind($this->outputStream);
$this->assertSame("\033[92;40mTest Message\033[0m", stream_get_contents($this->outputStream));
}
}
7 changes: 5 additions & 2 deletions test/Component/ClassFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public function testWillRenderClass(): void
->addInject(
(new Inject('CustomInjector'))->addArgument('true', 'param')
)
->setAbstract(true)
->setFinal(true)
->setReadonly(true)
->useClass('App\\Module\\OtherClassName')
->useClass('App\\Module\\InterfaceName')
->useFunction('sprintf')
Expand Down Expand Up @@ -158,7 +161,7 @@ public function testWillRenderClass(): void
->appendBody('// add logic')
);

$this->assertSame($this->dataProviderRenderedClass(), $actual->render());
$this->assertSame($this->dataProviderRenderedClass(), (string) $actual);
}

private function dataProviderRenderedClass(): string
Expand All @@ -178,7 +181,7 @@ private function dataProviderRenderedClass(): string
use const PHP_EOL;

#[CustomInjector(param: true)]
class ClassName extends OtherClassName implements InterfaceName
final readonly abstract class ClassName extends OtherClassName implements InterfaceName
{
protected bool \$propertyName = true;

Expand Down
37 changes: 36 additions & 1 deletion test/Component/DeclarationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,41 @@ public function testWillSetReturnType(): void
$this->assertSame('self', $declaration->getReturnType());
}

public function testWillRenderParameters(): void
{
$declaration = new Declaration($this->declarationName);
$this->assertSame('', $declaration->renderParameters());

$declaration->addParameter(
new Parameter('param', 'string')
);
$this->assertSame(' string $param,', $declaration->renderParameters());

$declaration->addParameter(
new Parameter('other', 'string')
);
$this->assertSame(<<<BODY
string \$param,
string \$other,
BODY, $declaration->renderParameters());
}

public function testWillRenderSignature(): void
{
$declaration = new Declaration($this->declarationName);
$this->assertSame(': void', $declaration->renderSignature());

$declaration->setReturnType('');
$this->assertSame('', $declaration->renderSignature());

$declaration->setReturnType('self');
$this->assertSame(': self', $declaration->renderSignature());

$declaration->setReturnType('string');
$declaration->setNullable(true);
$this->assertSame(': ?string', $declaration->renderSignature());
}

public function testWillRender(): void
{
$comment = <<<COMM
Expand All @@ -74,7 +109,7 @@ public function testWillRender(): void
->addParameter(
new Parameter('param', 'string')
);
$this->assertSame($this->dataProviderRenderedDeclaration(), $declaration->render());
$this->assertSame($this->dataProviderRenderedDeclaration(), (string) $declaration);
}

private function dataProviderRenderedDeclaration(): string
Expand Down
10 changes: 9 additions & 1 deletion test/Component/InterfaceFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,14 @@ public function testWillRenderInterface(): void
->useClass('App\\Module\\OtherInterface')
->useFunction('sprintf')
->useConstant('PHP_EOL')
->setComment(<<<COMM
/**
* Comment
*/
COMM)
->addDeclaration((new Declaration('getResource'))->setReturnType('Resource'));

$this->assertSame($this->dataProviderRenderedClass(), $actual->render());
$this->assertSame($this->dataProviderRenderedClass(), (string) $actual);
}

private function dataProviderRenderedClass(): string
Expand All @@ -93,6 +98,9 @@ private function dataProviderRenderedClass(): string

use const PHP_EOL;

/**
* Comment
*/
interface ClassName extends OtherInterface
{
public function getResource(): Resource;
Expand Down
21 changes: 19 additions & 2 deletions test/Component/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@ public function testWillSetVisibility(): void
$this->assertSame(VisibilityEnum::Private, $method->getVisibility());
}

public function testWillRender(): void
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)
Expand All @@ -121,7 +129,16 @@ public function testWillRender(): void
COMM)
->addParameter(new Parameter('param', 'string', true, 'null'))
->setBody(' return $this;');
$this->assertSame($this->dataProviderRenderedMethod(), $method->render());
$this->assertSame($this->dataProviderRenderedMethod(), (string) $method);
}

private function dataProviderRenderedMethodWithoutBody(): string
{
return <<<BODY
private function someMethod(): void
{
}
BODY;
}

private function dataProviderRenderedMethod(): string
Expand Down
2 changes: 1 addition & 1 deletion test/Component/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ public function testWillSetNullable(): void
public function testWillRender(): void
{
$parameter = new Parameter('param', 'string', true, '"test"');
$this->assertSame('?string $param = "test"', $parameter->render());
$this->assertSame('?string $param = "test"', (string) $parameter);
}
}
11 changes: 11 additions & 0 deletions test/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,15 @@ public function testWillRenderInputServiceInterface(): void
$this->assertSame('BOOK_STORE_SERVICE_INTERFACE', $component->toUpperCase(false));
$this->assertSame('BOOK_STORE_SERVICE', $component->toUpperCase());
}

public function testWillPluralize(): void
{
$this->assertSame('buses', Component::pluralize('bus'));
$this->assertSame('boxes', Component::pluralize('box'));
$this->assertSame('jazzes', Component::pluralize('jazz'));
$this->assertSame('fishes', Component::pluralize('fish'));
$this->assertSame('watches', Component::pluralize('watch'));
$this->assertSame('candies', Component::pluralize('candy'));
$this->assertSame('books', Component::pluralize('book'));
}
}
12 changes: 12 additions & 0 deletions test/IO/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,16 @@ public function testConfirmWillOutputMessageAndReadNegativeAnswer(string $input)
fclose($inputStream);
fclose($outputStream);
}

public function testWillAlwaysGetInputStream(): void
{
Input::setStream(fopen('php://memory', 'w+'));

$stream = Input::getStream();
$this->assertIsResource($stream);
fclose($stream);

$stream = Input::getStream();
$this->assertIsResource($stream);
}
}
24 changes: 24 additions & 0 deletions test/IO/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,28 @@ public function testWillWriteLine(): void

fclose($stream);
}

public function testWillAlwaysGetErrorStream(): void
{
Output::setErrorStream(fopen('php://memory', 'w+'));

$stream = Output::getErrorStream();
$this->assertIsResource($stream);
fclose($stream);

$stream = Output::getErrorStream();
$this->assertIsResource($stream);
}

public function testWillAlwaysGetOutputStream(): void
{
Output::setOutputStream(fopen('php://memory', 'w+'));

$stream = Output::getOutputStream();
$this->assertIsResource($stream);
fclose($stream);

$stream = Output::getOutputStream();
$this->assertIsResource($stream);
}
}
Loading