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

MakeFileCommandTest::tearDownAfterClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 MakeFileCommandTest extends TestCase
19
{
20
    private const CACHE_DIR = '.' . DIRECTORY_SEPARATOR . 'src' . 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_file_command_description(): void
36
    {
37
        $bootstrap = new ConsoleBootstrap();
38
        $command = $bootstrap->find('make:file');
39
40
        $description = $command->getDescription();
41
42
        // Test that the description contains 'Generate a ' followed by the expected filenames
43
        self::assertStringContainsString('Generate a ', $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)
50
        self::assertStringStartsWith('Generate a ', $description);
51
    }
52
53
    #[DataProvider('createFilesProvider')]
54
    public function test_make_file(string $action, string $fileName, bool $shortName): void
55
    {
56
        $shortNameFlag = $shortName ? '--short-name' : '';
57
        $input = new StringInput(sprintf('make:file Psr4CodeGenerator/TestModule %s %s', $action, $shortNameFlag));
58
        $output = new BufferedOutput();
59
60
        $bootstrap = new ConsoleBootstrap();
61
        $bootstrap->setAutoExit(false);
62
        $bootstrap->run($input, $output);
63
64
        self::assertSame(sprintf("> Path 'src/TestModule/%s.php' created successfully", $fileName), trim($output->fetch()));
65
        self::assertFileExists(sprintf('./src/TestModule/%s.php', $fileName));
66
    }
67
68
    public static function createFilesProvider(): iterable
69
    {
70
        yield 'facade' => ['facade', 'TestModuleFacade', false];
71
        yield 'factory' => ['factory', 'TestModuleFactory', false];
72
        yield 'config' => ['config', 'TestModuleConfig', false];
73
        yield 'dependency provider' => ['dependency-provider', 'TestModuleProvider', false];
74
75
        // Short name flag
76
        yield 'facade -s' => ['facade', 'Facade', true];
77
        yield 'factory -s' => ['factory', 'Factory', true];
78
        yield 'config -s' => ['config', 'Config', true];
79
        yield 'dependency provider -s' => ['dependency-provider', 'Provider', true];
80
    }
81
}
82