ContainerCommandLoaderTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\DependencyInjection\ServiceLocator;
  15. class ContainerCommandLoaderTest extends TestCase
  16. {
  17. public function testHas()
  18. {
  19. $loader = new ContainerCommandLoader(new ServiceLocator([
  20. 'foo-service' => function () { return new Command('foo'); },
  21. 'bar-service' => function () { return new Command('bar'); },
  22. ]), ['foo' => 'foo-service', 'bar' => 'bar-service']);
  23. $this->assertTrue($loader->has('foo'));
  24. $this->assertTrue($loader->has('bar'));
  25. $this->assertFalse($loader->has('baz'));
  26. }
  27. public function testGet()
  28. {
  29. $loader = new ContainerCommandLoader(new ServiceLocator([
  30. 'foo-service' => function () { return new Command('foo'); },
  31. 'bar-service' => function () { return new Command('bar'); },
  32. ]), ['foo' => 'foo-service', 'bar' => 'bar-service']);
  33. $this->assertInstanceOf(Command::class, $loader->get('foo'));
  34. $this->assertInstanceOf(Command::class, $loader->get('bar'));
  35. }
  36. /**
  37. * @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
  38. */
  39. public function testGetUnknownCommandThrows()
  40. {
  41. (new ContainerCommandLoader(new ServiceLocator([]), []))->get('unknown');
  42. }
  43. public function testGetCommandNames()
  44. {
  45. $loader = new ContainerCommandLoader(new ServiceLocator([
  46. 'foo-service' => function () { return new Command('foo'); },
  47. 'bar-service' => function () { return new Command('bar'); },
  48. ]), ['foo' => 'foo-service', 'bar' => 'bar-service']);
  49. $this->assertSame(['foo', 'bar'], $loader->getNames());
  50. }
  51. }