Passed
Pull Request — master (#253)
by Wilmer
02:02
created

HelloCest::testExecute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 0
loc 25
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Tests\Unit;
6
7
use App\Tests\Support\UnitTester;
8
use Psr\Container\ContainerInterface;
9
use Symfony\Component\Console\Application;
10
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
11
use Symfony\Component\Console\Tester\CommandTester;
12
use Yiisoft\Config\Config;
13
use Yiisoft\Config\ConfigPaths;
14
use Yiisoft\Di\Container;
15
use Yiisoft\Di\ContainerConfig;
16
use Yiisoft\Yii\Console\ExitCode;
17
use Yiisoft\Yii\Runner\ConfigFactory;
18
19
use function dirname;
20
21
final class HelloCest
22
{
23
    private ContainerInterface $container;
24
25
    public function _before(UnitTester $I): void
0 ignored issues
show
Unused Code introduced by
The parameter $I is not used and could be removed. ( Ignorable by Annotation )

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

25
    public function _before(/** @scrutinizer ignore-unused */ UnitTester $I): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    {
27
        $config = $this->getConfig();
28
29
        $containerConfig = ContainerConfig::create()
30
            ->withDefinitions($config->get('console'))
31
            ->withProviders($config->get('providers'));
32
        $this->container = new Container($containerConfig);
33
    }
34
35
    public function testExecute(UnitTester $I): void
36
    {
37
        $app = new Application();
38
        $params = $this
39
            ->getConfig()
40
            ->get('params');
41
42
        $loader = new ContainerCommandLoader(
43
            $this->container,
44
            $params['yiisoft/yii-console']['commands']
45
        );
46
47
        $app->setCommandLoader($loader);
48
49
        $command = $app->find('hello');
50
51
        $commandCreate = new CommandTester($command);
52
53
        $commandCreate->setInputs(['yes']);
54
55
        $I->assertSame(ExitCode::OK, $commandCreate->execute([]));
56
57
        $output = $commandCreate->getDisplay(true);
58
59
        $I->assertStringContainsString('Hello!', $output);
60
    }
61
62
    private function getConfig(): Config
63
    {
64
        return ConfigFactory::create(new ConfigPaths(dirname(__DIR__, 2), 'config'), null);
65
    }
66
}
67