ApplicationFactoryTest.php 1.9 KB

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