2 declare(strict_types = 1);
18 use org\bovigo\vfs\vfsStream;
19 use Prophecy\Prophecy\ObjectProphecy;
20 use Symfony\Component\Console\Command\Command;
25 use TYPO3\CMS\Core\Package\PackageManager;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 class CommandRegistryTest
extends UnitTestCase
36 protected $rootDirectory;
41 protected $packageManagerProphecy;
46 protected function setUp()
48 $commandMockClass = $this->getMockClass(Command::class, [
'dummy']);
49 $this->rootDirectory = vfsStream::setup(
'root',
null, [
52 'Commands.php' =>
'<?php return ["first:command" => [ "class" => "' . $commandMockClass .
'" ]];',
57 'Commands.php' =>
'<?php return ["second:command" => [ "class" => "' . $commandMockClass .
'" ]];',
62 'Commands.php' =>
'<?php return ["third:command" => [ "class" => "' . $commandMockClass .
'" ]];',
67 'Commands.php' =>
'<?php return ["third:command" => [ "class" => "' . $commandMockClass .
'" ]];',
73 $this->packageManagerProphecy = $this->prophesize(PackageManager::class);
79 public function iteratesCommandsOfActivePackages()
82 $package1 = $this->prophesize(PackageInterface::class);
83 $package1->getPackagePath()->willReturn($this->rootDirectory->getChild(
'package1')->url() .
'/');
85 $package2 = $this->prophesize(PackageInterface::class);
86 $package2->getPackagePath()->willReturn($this->rootDirectory->getChild(
'package2')->url() .
'/');
88 $this->packageManagerProphecy->getActivePackages()->willReturn([$package1->reveal(), $package2->reveal()]);
90 $commandRegistry =
new CommandRegistry($this->packageManagerProphecy->reveal());
91 $commands = iterator_to_array($commandRegistry);
93 $this->assertCount(2, $commands);
94 $this->assertContainsOnlyInstancesOf(Command::class, $commands);
100 public function throwsExceptionOnDuplicateCommand()
103 $package3 = $this->prophesize(PackageInterface::class);
104 $package3->getPackagePath()->willReturn($this->rootDirectory->getChild(
'package3')->url() .
'/');
106 $package4 = $this->prophesize(PackageInterface::class);
107 $package4->getPackagePath()->willReturn($this->rootDirectory->getChild(
'package4')->url() .
'/');
108 $package4->getPackageKey()->willReturn(
'package4');
110 $this->packageManagerProphecy->getActivePackages()->willReturn([$package3->reveal(), $package4->reveal()]);
112 $this->expectException(CommandNameAlreadyInUseException::class);
113 $this->expectExceptionCode(1484486383);
115 $commandRegistry =
new CommandRegistry($this->packageManagerProphecy->reveal());
116 iterator_to_array($commandRegistry);
122 public function getCommandByIdentifierReturnsRegisteredCommand()
125 $package = $this->prophesize(PackageInterface::class);
126 $package->getPackagePath()->willReturn($this->rootDirectory->getChild(
'package1')->url() .
'/');
127 $package->getPackageKey()->willReturn(
'package1');
129 $this->packageManagerProphecy->getActivePackages()->willReturn([$package->reveal()]);
131 $commandRegistry =
new CommandRegistry($this->packageManagerProphecy->reveal());
132 $command = $commandRegistry->getCommandByIdentifier(
'first:command');
134 $this->assertInstanceOf(Command::class, $command);
140 public function throwsUnknowCommandExceptionIfUnregisteredCommandIsRequested()
142 $this->packageManagerProphecy->getActivePackages()->willReturn([]);
144 $this->expectException(UnknownCommandException::class);
145 $this->expectExceptionCode(1510906768);
147 $commandRegistry =
new CommandRegistry($this->packageManagerProphecy->reveal());
148 $commandRegistry->getCommandByIdentifier(
'foo');