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\I18n\Translator\Translator; |
14
|
|
|
use Zend\ServiceManager\ServiceManager; |
15
|
|
|
use function array_merge; |
16
|
|
|
|
17
|
|
|
class ApplicationFactoryTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ApplicationFactory |
21
|
|
|
*/ |
22
|
|
|
protected $factory; |
23
|
|
|
|
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
$this->factory = new ApplicationFactory(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
*/ |
32
|
|
|
public function serviceIsCreated() |
33
|
|
|
{ |
34
|
|
|
$instance = ($this->factory)($this->createServiceManager(), ''); |
35
|
|
|
$this->assertInstanceOf(Application::class, $instance); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @test |
40
|
|
|
*/ |
41
|
|
|
public function allCommandsWhichAreServicesAreAdded() |
42
|
|
|
{ |
43
|
|
|
$sm = $this->createServiceManager([ |
44
|
|
|
'commands' => [ |
45
|
|
|
'foo' => 'foo', |
46
|
|
|
'bar' => 'bar', |
47
|
|
|
'baz' => 'baz', |
48
|
|
|
], |
49
|
|
|
]); |
50
|
|
|
$sm->setService('foo', $this->createCommandMock('foo')->reveal()); |
51
|
|
|
$sm->setService('bar', $this->createCommandMock('bar')->reveal()); |
52
|
|
|
|
53
|
|
|
/** @var Application $instance */ |
54
|
|
|
$instance = ($this->factory)($sm, ''); |
55
|
|
|
$this->assertInstanceOf(Application::class, $instance); |
56
|
|
|
|
57
|
|
|
$this->assertTrue($instance->has('foo')); |
58
|
|
|
$this->assertTrue($instance->has('bar')); |
59
|
|
|
$this->assertFalse($instance->has('baz')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function createServiceManager(array $config = []): ServiceManager |
63
|
|
|
{ |
64
|
|
|
return new ServiceManager(['services' => [ |
65
|
|
|
'config' => [ |
66
|
|
|
'cli' => array_merge($config, ['locale' => 'en']), |
67
|
|
|
], |
68
|
|
|
AppOptions::class => new AppOptions(), |
69
|
|
|
Translator::class => Translator::factory([]), |
70
|
|
|
]]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function createCommandMock(string $name): ObjectProphecy |
74
|
|
|
{ |
75
|
|
|
$command = $this->prophesize(Command::class); |
76
|
|
|
$command->getName()->willReturn($name); |
77
|
|
|
$command->getDefinition()->willReturn($name); |
78
|
|
|
$command->isEnabled()->willReturn(true); |
79
|
|
|
$command->getAliases()->willReturn([]); |
80
|
|
|
$command->setApplication(Argument::type(Application::class))->willReturn(function () { |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
return $command; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|