ApplicationFactoryTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 26
dl 0
loc 52
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A createCommandMock() 0 11 1
A allCommandsWhichAreServicesAreAdded() 0 17 1
A createServiceManager() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\CLI\Factory;
6
7
use Laminas\ServiceManager\ServiceManager;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Argument;
10
use Prophecy\PhpUnit\ProphecyTrait;
11
use Prophecy\Prophecy\ObjectProphecy;
12
use Shlinkio\Shlink\CLI\Factory\ApplicationFactory;
13
use Shlinkio\Shlink\Core\Options\AppOptions;
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Command\Command;
16
17
class ApplicationFactoryTest extends TestCase
18
{
19
    use ProphecyTrait;
20
21
    private ApplicationFactory $factory;
22
23
    public function setUp(): void
24
    {
25
        $this->factory = new ApplicationFactory();
26
    }
27
28
    /** @test */
29
    public function allCommandsWhichAreServicesAreAdded(): void
30
    {
31
        $sm = $this->createServiceManager([
32
            'commands' => [
33
                'foo' => 'foo',
34
                'bar' => 'bar',
35
                'baz' => 'baz',
36
            ],
37
        ]);
38
        $sm->setService('foo', $this->createCommandMock('foo')->reveal());
39
        $sm->setService('bar', $this->createCommandMock('bar')->reveal());
40
41
        $instance = ($this->factory)($sm);
42
43
        self::assertTrue($instance->has('foo'));
44
        self::assertTrue($instance->has('bar'));
45
        self::assertFalse($instance->has('baz'));
46
    }
47
48
    private function createServiceManager(array $config = []): ServiceManager
49
    {
50
        return new ServiceManager(['services' => [
51
            'config' => [
52
                'cli' => $config,
53
            ],
54
            AppOptions::class => new AppOptions(),
55
        ]]);
56
    }
57
58
    private function createCommandMock(string $name): ObjectProphecy
59
    {
60
        $command = $this->prophesize(Command::class);
61
        $command->getName()->willReturn($name);
62
        $command->getDefinition()->willReturn($name);
63
        $command->isEnabled()->willReturn(true);
64
        $command->getAliases()->willReturn([]);
65
        $command->setApplication(Argument::type(Application::class))->willReturn(function (): void {
66
        });
67
68
        return $command;
69
    }
70
}
71