Issues (224)

src/Test/CommandTestCase.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Test;
16
17
use Fidry\Console\Bridge\Command\SymfonyCommand;
18
use Fidry\Console\Command\Command;
19
use Fidry\Console\Test\CommandTester;
20
use Fidry\Console\Test\OutputAssertions;
21
use Symfony\Component\Console\Application;
22
23
/**
24
 * @private
25
 */
26
abstract class CommandTestCase extends FileSystemTestCase
27
{
28
    protected CommandTester $commandTester;
29
    protected Command $command;
30
31
    protected function setUp(): void
32
    {
33
        parent::setUp();
34
35
        $this->command = $this->getCommand();
36
37
        $command = new SymfonyCommand($this->command);
38
39
        $application = new Application();
40
        $application->add($command);
41
42
        $this->commandTester = new CommandTester(
43
            $application->get(
44
                $command->getName(),
0 ignored issues
show
It seems like $command->getName() can also be of type null; however, parameter $name of Symfony\Component\Console\Application::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
                /** @scrutinizer ignore-type */ $command->getName(),
Loading history...
45
            ),
46
        );
47
    }
48
49
    protected function tearDown(): void
50
    {
51
        unset($this->command, $this->commandTester);
52
53
        parent::tearDown();
54
    }
55
56
    /**
57
     * Returns the command to be tested.
58
     *
59
     * @return Command the command
60
     */
61
    abstract protected function getCommand(): Command;
62
63
    /**
64
     * @param callable(string):string $extraNormalizers
65
     */
66
    public function assertSameOutput(
67
        string $expectedOutput,
68
        int $expectedStatusCode,
69
        callable ...$extraNormalizers,
70
    ): void {
71
        OutputAssertions::assertSameOutput(
72
            $expectedOutput,
73
            $expectedStatusCode,
74
            $this->commandTester,
75
            ...$extraNormalizers,
76
        );
77
    }
78
}
79