2 declare(strict_types
=1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Console
;
6 * This file is part of the TYPO3 CMS project.
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 use org\bovigo\vfs\vfsStream
;
19 use Prophecy\Prophecy\ObjectProphecy
;
20 use Symfony\Component\Console\Command\Command
;
21 use TYPO3\CMS\Core\Console\CommandNameAlreadyInUseException
;
22 use TYPO3\CMS\Core\Console\CommandRegistry
;
23 use TYPO3\CMS\Core\Console\UnknownCommandException
;
24 use TYPO3\CMS\Core\Package\PackageInterface
;
25 use TYPO3\CMS\Core\Package\PackageManager
;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase
;
29 * Testcase for CommandRegistry
31 class CommandRegistryTest
extends UnitTestCase
34 * @var \org\bovigo\vfs\vfsStreamDirectory
36 protected $rootDirectory;
39 * @var PackageManager|\Prophecy\Prophecy\ObjectProphecy
41 protected $packageManagerProphecy;
44 * Set up this testcase
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 . '" ]];',
72 /** @var PackageManager */
73 $this->packageManagerProphecy
= $this->prophesize(PackageManager
::class);
79 public function iteratesCommandsOfActivePackages()
81 /** @var PackageInterface */
82 $package1 = $this->prophesize(PackageInterface
::class);
83 $package1->getPackagePath()->willReturn($this->rootDirectory
->getChild('package1')->url() . '/');
84 /** @var PackageInterface */
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()
102 /** @var PackageInterface */
103 $package3 = $this->prophesize(PackageInterface
::class);
104 $package3->getPackagePath()->willReturn($this->rootDirectory
->getChild('package3')->url() . '/');
105 /** @var PackageInterface */
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()
124 /** @var PackageInterface|ObjectProphecy $package */
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');