CommandFactoryTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @license https://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Console;
  15. use Cake\Console\CommandFactory;
  16. use Cake\Console\CommandInterface;
  17. use Cake\Core\Container;
  18. use Cake\TestSuite\TestCase;
  19. use InvalidArgumentException;
  20. use stdClass;
  21. use TestApp\Command\DemoCommand;
  22. use TestApp\Command\DependencyCommand;
  23. use TestApp\Shell\SampleShell;
  24. class CommandFactoryTest extends TestCase
  25. {
  26. public function testCreateCommand(): void
  27. {
  28. $factory = new CommandFactory();
  29. $command = $factory->create(DemoCommand::class);
  30. $this->assertInstanceOf(DemoCommand::class, $command);
  31. $this->assertInstanceOf(CommandInterface::class, $command);
  32. }
  33. public function testCreateCommandDependencies(): void
  34. {
  35. $container = new Container();
  36. $container->add(stdClass::class, json_decode('{"key":"value"}'));
  37. $container->add(DependencyCommand::class)
  38. ->addArgument(stdClass::class);
  39. $factory = new CommandFactory($container);
  40. $command = $factory->create(DependencyCommand::class);
  41. $this->assertInstanceOf(DependencyCommand::class, $command);
  42. $this->assertInstanceOf(stdClass::class, $command->inject);
  43. }
  44. public function testCreateShell(): void
  45. {
  46. $factory = new CommandFactory();
  47. $shell = $factory->create(SampleShell::class);
  48. $this->assertInstanceOf(SampleShell::class, $shell);
  49. }
  50. public function testInvalid(): void
  51. {
  52. $factory = new CommandFactory();
  53. $this->expectException(InvalidArgumentException::class);
  54. $this->expectExceptionMessage(
  55. 'Class `Cake\Test\TestCase\Console\CommandFactoryTest` must be an instance of ' .
  56. '`Cake\Console\Shell` or `Cake\Console\CommandInterface`.'
  57. );
  58. $factory->create(static::class);
  59. }
  60. }