ContainerCommandLoaderTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. public function testGetUnknownCommandThrows()
  37. {
  38. $this->expectException('Symfony\Component\Console\Exception\CommandNotFoundException');
  39. (new ContainerCommandLoader(new ServiceLocator([]), []))->get('unknown');
  40. }
  41. public function testGetCommandNames()
  42. {
  43. $loader = new ContainerCommandLoader(new ServiceLocator([
  44. 'foo-service' => function () { return new Command('foo'); },
  45. 'bar-service' => function () { return new Command('bar'); },
  46. ]), ['foo' => 'foo-service', 'bar' => 'bar-service']);
  47. $this->assertSame(['foo', 'bar'], $loader->getNames());
  48. }
  49. }