ApplicationFactoryTest.php 2.0 KB

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