1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\CLI\Factory; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Prophecy\Argument; |
8
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
9
|
|
|
use Shlinkio\Shlink\CLI\Factory\ApplicationFactory; |
10
|
|
|
use Shlinkio\Shlink\Core\Options\AppOptions; |
11
|
|
|
use Symfony\Component\Console\Application; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Zend\ServiceManager\ServiceManager; |
14
|
|
|
|
15
|
|
|
use function array_merge; |
16
|
|
|
|
17
|
|
|
class ApplicationFactoryTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** @var ApplicationFactory */ |
20
|
|
|
private $factory; |
21
|
|
|
|
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
$this->factory = new ApplicationFactory(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** @test */ |
28
|
|
|
public function allCommandsWhichAreServicesAreAdded(): void |
29
|
|
|
{ |
30
|
|
|
$sm = $this->createServiceManager([ |
31
|
|
|
'commands' => [ |
32
|
|
|
'foo' => 'foo', |
33
|
|
|
'bar' => 'bar', |
34
|
|
|
'baz' => 'baz', |
35
|
|
|
], |
36
|
|
|
]); |
37
|
|
|
$sm->setService('foo', $this->createCommandMock('foo')->reveal()); |
38
|
|
|
$sm->setService('bar', $this->createCommandMock('bar')->reveal()); |
39
|
|
|
|
40
|
|
|
/** @var Application $instance */ |
41
|
|
|
$instance = ($this->factory)($sm); |
42
|
|
|
|
43
|
|
|
$this->assertTrue($instance->has('foo')); |
44
|
|
|
$this->assertTrue($instance->has('bar')); |
45
|
|
|
$this->assertFalse($instance->has('baz')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function createServiceManager(array $config = []): ServiceManager |
49
|
|
|
{ |
50
|
|
|
return new ServiceManager(['services' => [ |
51
|
|
|
'config' => [ |
52
|
|
|
'cli' => array_merge($config, ['locale' => 'en']), |
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 () { |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
return $command; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|