Passed
Push — main ( 8161e3...71fa0f )
by Chema
04:11
created

test_make_module_command_description()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 16
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Feature\Console\CodeGenerator;
6
7
use Gacela\Console\Infrastructure\ConsoleBootstrap;
8
use Gacela\Framework\Bootstrap\GacelaConfig;
9
use Gacela\Framework\Gacela;
10
use GacelaTest\Feature\Util\DirectoryUtil;
11
use PHPUnit\Framework\Attributes\DataProvider;
12
use PHPUnit\Framework\TestCase;
13
use Symfony\Component\Console\Input\StringInput;
14
use Symfony\Component\Console\Output\BufferedOutput;
15
16
use function sprintf;
17
18
final class MakeModuleCommandTest extends TestCase
19
{
20
    private const CACHE_DIR = '.' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'TestModule';
21
22
    public static function tearDownAfterClass(): void
23
    {
24
        DirectoryUtil::removeDir(self::CACHE_DIR);
25
    }
26
27
    protected function setUp(): void
28
    {
29
        Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void {
30
            $config->resetInMemoryCache();
31
        });
32
        DirectoryUtil::removeDir(self::CACHE_DIR);
33
    }
34
35
    public function test_make_module_command_description(): void
36
    {
37
        $bootstrap = new ConsoleBootstrap();
38
        $command = $bootstrap->find('make:module');
39
40
        $description = $command->getDescription();
41
42
        // Test that the description contains 'Generate a basic module with an empty ' followed by the expected filenames
43
        self::assertStringContainsString('Generate a basic module with an empty ', $description);
44
        self::assertStringContainsString('Facade', $description);
45
        self::assertStringContainsString('Factory', $description);
46
        self::assertStringContainsString('Config', $description);
47
        self::assertStringContainsString('Provider', $description);
48
49
        // Ensure it's in the correct order (not reversed or partial)
50
        self::assertStringStartsWith('Generate a basic module with an empty ', $description);
51
    }
52
53
    #[DataProvider('createModulesProvider')]
54
    public function test_make_module(string $fileName, bool $shortName): void
55
    {
56
        $shortNameFlag = $shortName ? '--short-name' : '';
57
        $input = new StringInput('make:module Psr4CodeGeneratorData/TestModule ' . $shortNameFlag);
58
        $output = new BufferedOutput();
59
60
        $bootstrap = new ConsoleBootstrap();
61
        $bootstrap->setAutoExit(false);
62
        $bootstrap->run($input, $output);
63
64
        $expectedOutput = <<<OUT
65
> Path 'data/TestModule/{$fileName}Facade.php' created successfully
66
> Path 'data/TestModule/{$fileName}Factory.php' created successfully
67
> Path 'data/TestModule/{$fileName}Config.php' created successfully
68
> Path 'data/TestModule/{$fileName}Provider.php' created successfully
69
Module 'TestModule' created successfully
70
OUT;
71
72
        if (strcasecmp(substr(PHP_OS, 0, 3), 'WIN') == 0) {
73
            $expectedOutput = str_replace("\n", PHP_EOL, $expectedOutput);
74
        }
75
76
        self::assertSame($expectedOutput, trim($output->fetch()));
77
78
        self::assertFileExists(sprintf('./data/TestModule/%sFacade.php', $fileName));
79
        self::assertFileExists(sprintf('./data/TestModule/%sFactory.php', $fileName));
80
        self::assertFileExists(sprintf('./data/TestModule/%sConfig.php', $fileName));
81
        self::assertFileExists(sprintf('./data/TestModule/%sProvider.php', $fileName));
82
    }
83
84
    public static function createModulesProvider(): iterable
85
    {
86
        yield 'module' => ['TestModule', false];
87
        yield 'module -s' => ['', true];
88
    }
89
}
90