FactoryCommandLoaderTest.php 1.8 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\FactoryCommandLoader;
  14. use Symfony\Component\Console\Exception\CommandNotFoundException;
  15. class FactoryCommandLoaderTest extends TestCase
  16. {
  17. public function testHas()
  18. {
  19. $loader = new FactoryCommandLoader([
  20. 'foo' => function () { return new Command('foo'); },
  21. 'bar' => function () { return new Command('bar'); },
  22. ]);
  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 FactoryCommandLoader([
  30. 'foo' => function () { return new Command('foo'); },
  31. 'bar' => function () { return new Command('bar'); },
  32. ]);
  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(CommandNotFoundException::class);
  39. (new FactoryCommandLoader([]))->get('unknown');
  40. }
  41. public function testGetCommandNames()
  42. {
  43. $loader = new FactoryCommandLoader([
  44. 'foo' => function () { return new Command('foo'); },
  45. 'bar' => function () { return new Command('bar'); },
  46. ]);
  47. $this->assertSame(['foo', 'bar'], $loader->getNames());
  48. }
  49. }