ContainerCommandLoaderTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\CommandLoader;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
  14. use Symfony\Component\Console\Exception\CommandNotFoundException;
  15. use Symfony\Component\DependencyInjection\ServiceLocator;
  16. class ContainerCommandLoaderTest extends TestCase
  17. {
  18. public function testHas()
  19. {
  20. $loader = new ContainerCommandLoader(new ServiceLocator([
  21. 'foo-service' => function () { return new Command('foo'); },
  22. 'bar-service' => function () { return new Command('bar'); },
  23. ]), ['foo' => 'foo-service', 'bar' => 'bar-service']);
  24. $this->assertTrue($loader->has('foo'));
  25. $this->assertTrue($loader->has('bar'));
  26. $this->assertFalse($loader->has('baz'));
  27. }
  28. public function testGet()
  29. {
  30. $loader = new ContainerCommandLoader(new ServiceLocator([
  31. 'foo-service' => function () { return new Command('foo'); },
  32. 'bar-service' => function () { return new Command('bar'); },
  33. ]), ['foo' => 'foo-service', 'bar' => 'bar-service']);
  34. $this->assertInstanceOf(Command::class, $loader->get('foo'));
  35. $this->assertInstanceOf(Command::class, $loader->get('bar'));
  36. }
  37. public function testGetUnknownCommandThrows()
  38. {
  39. $this->expectException(CommandNotFoundException::class);
  40. (new ContainerCommandLoader(new ServiceLocator([]), []))->get('unknown');
  41. }
  42. public function testGetCommandNames()
  43. {
  44. $loader = new ContainerCommandLoader(new ServiceLocator([
  45. 'foo-service' => function () { return new Command('foo'); },
  46. 'bar-service' => function () { return new Command('bar'); },
  47. ]), ['foo' => 'foo-service', 'bar' => 'bar-service']);
  48. $this->assertSame(['foo', 'bar'], $loader->getNames());
  49. }
  50. }