shlink/module/CLI/test/Factory/ApplicationFactoryTest.php

84 lines
2.3 KiB
PHP
Raw Normal View History

2016-07-19 11:28:21 -05:00
<?php
2017-10-12 03:13:20 -05:00
declare(strict_types=1);
2016-07-19 11:28:21 -05:00
namespace ShlinkioTest\Shlink\CLI\Factory;
2017-03-24 14:34:18 -05:00
use PHPUnit\Framework\TestCase;
2018-11-17 11:40:47 -06:00
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
2016-07-19 11:28:21 -05:00
use Shlinkio\Shlink\CLI\Factory\ApplicationFactory;
2016-08-15 02:52:44 -05:00
use Shlinkio\Shlink\Core\Options\AppOptions;
2016-07-19 11:28:21 -05:00
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Zend\ServiceManager\ServiceManager;
use function array_merge;
2016-07-19 11:28:21 -05:00
class ApplicationFactoryTest extends TestCase
{
/**
* @var ApplicationFactory
*/
protected $factory;
public function setUp()
{
$this->factory = new ApplicationFactory();
}
/**
* @test
*/
public function serviceIsCreated()
{
2018-11-17 11:40:47 -06:00
$instance = ($this->factory)($this->createServiceManager(), '');
2016-07-19 11:28:21 -05:00
$this->assertInstanceOf(Application::class, $instance);
}
/**
* @test
*/
public function allCommandsWhichAreServicesAreAdded()
{
$sm = $this->createServiceManager([
'commands' => [
2018-11-17 11:40:47 -06:00
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
2016-07-19 11:28:21 -05:00
],
]);
2018-11-17 11:40:47 -06:00
$sm->setService('foo', $this->createCommandMock('foo')->reveal());
$sm->setService('bar', $this->createCommandMock('bar')->reveal());
2016-07-19 11:28:21 -05:00
/** @var Application $instance */
2018-11-17 11:40:47 -06:00
$instance = ($this->factory)($sm, '');
2016-07-19 11:28:21 -05:00
$this->assertInstanceOf(Application::class, $instance);
2018-11-17 11:40:47 -06:00
$this->assertTrue($instance->has('foo'));
$this->assertTrue($instance->has('bar'));
$this->assertFalse($instance->has('baz'));
2016-07-19 11:28:21 -05:00
}
2018-11-17 11:40:47 -06:00
private function createServiceManager(array $config = []): ServiceManager
2016-07-19 11:28:21 -05:00
{
return new ServiceManager(['services' => [
'config' => [
2016-08-15 02:52:44 -05:00
'cli' => array_merge($config, ['locale' => 'en']),
2016-07-19 11:28:21 -05:00
],
2016-08-15 02:52:44 -05:00
AppOptions::class => new AppOptions(),
2016-07-19 11:28:21 -05:00
]]);
}
2018-11-17 11:40:47 -06:00
private function createCommandMock(string $name): ObjectProphecy
{
$command = $this->prophesize(Command::class);
$command->getName()->willReturn($name);
$command->getDefinition()->willReturn($name);
$command->isEnabled()->willReturn(true);
$command->getAliases()->willReturn([]);
$command->setApplication(Argument::type(Application::class))->willReturn(function () {
});
return $command;
}
2016-07-19 11:28:21 -05:00
}