‪TYPO3CMS  10.4
CommandRegistryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
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.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use org\bovigo\vfs\vfsStream;
21 use Prophecy\Prophecy\ObjectProphecy;
22 use Psr\Container\ContainerInterface;
23 use Symfony\Component\Console\Command\Command;
28 use TYPO3\CMS\Core\Package\PackageManager;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class CommandRegistryTest extends UnitTestCase
35 {
39  protected $rootDirectory;
40 
44  protected $packageManagerProphecy;
45 
49  protected $containerProphecy;
50 
54  protected function setUp(): void
55  {
56  parent::setUp();
57  $commandMockClass = $this->getMockClass(Command::class, ['dummy']);
58  $this->rootDirectory = vfsStream::setup('root', null, [
59  'package1' => [
60  'Configuration' => [
61  'Commands.php' => '<?php return ["first:command" => [ "class" => "' . $commandMockClass . '" ]];',
62  ],
63  ],
64  'package2' => [
65  'Configuration' => [
66  'Commands.php' => '<?php return ["second:command" => [ "class" => "' . $commandMockClass . '" ]];',
67  ],
68  ],
69  'package3' => [
70  'Configuration' => [
71  'Commands.php' => '<?php return ["third:command" => [ "class" => "' . $commandMockClass . '" ]];',
72  ],
73  ],
74  'package4' => [
75  'Configuration' => [
76  'Commands.php' => '<?php return ["third:command" => [ "class" => "' . $commandMockClass . '" ]];',
77  ],
78  ],
79  ]);
80 
82  $this->packageManagerProphecy = $this->prophesize(PackageManager::class);
83 
85  $this->containerProphecy = $this->prophesize(ContainerInterface::class);
86  }
87 
91  public function iteratesCommandsOfActivePackages()
92  {
94  $package1 = $this->prophesize(PackageInterface::class);
95  $package1->getPackagePath()->willReturn($this->rootDirectory->getChild('package1')->url() . '/');
97  $package2 = $this->prophesize(PackageInterface::class);
98  $package2->getPackagePath()->willReturn($this->rootDirectory->getChild('package2')->url() . '/');
99 
100  $this->packageManagerProphecy->getActivePackages()->willReturn([$package1->reveal(), $package2->reveal()]);
101 
102  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal(), $this->containerProphecy->reveal());
103  $commands = iterator_to_array($commandRegistry);
104 
105  self::assertCount(2, $commands);
106  self::assertContainsOnlyInstancesOf(Command::class, $commands);
107  }
108 
112  public function iteratesLegacyCommandsOfActivePackages()
113  {
115  $package1 = $this->prophesize(PackageInterface::class);
116  $package1->getPackagePath()->willReturn($this->rootDirectory->getChild('package1')->url() . '/');
118  $package2 = $this->prophesize(PackageInterface::class);
119  $package2->getPackagePath()->willReturn($this->rootDirectory->getChild('package2')->url() . '/');
120 
121  $this->packageManagerProphecy->getActivePackages()->willReturn([$package1->reveal(), $package2->reveal()]);
122 
123  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal(), $this->containerProphecy->reveal());
124  $commands = iterator_to_array($commandRegistry->getLegacyCommands());
125 
126  self::assertCount(2, $commands);
127  self::assertContainsOnlyInstancesOf(Command::class, $commands);
128  }
129 
133  public function throwsExceptionOnDuplicateCommand()
134  {
136  $package3 = $this->prophesize(PackageInterface::class);
137  $package3->getPackagePath()->willReturn($this->rootDirectory->getChild('package3')->url() . '/');
139  $package4 = $this->prophesize(PackageInterface::class);
140  $package4->getPackagePath()->willReturn($this->rootDirectory->getChild('package4')->url() . '/');
141  $package4->getPackageKey()->willReturn('package4');
142 
143  $this->packageManagerProphecy->getActivePackages()->willReturn([$package3->reveal(), $package4->reveal()]);
144 
145  $this->expectException(CommandNameAlreadyInUseException::class);
146  $this->expectExceptionCode(1484486383);
147 
148  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal(), $this->containerProphecy->reveal());
149  iterator_to_array($commandRegistry);
150  }
151 
155  public function getCommandByIdentifierReturnsRegisteredCommand()
156  {
158  $package = $this->prophesize(PackageInterface::class);
159  $package->getPackagePath()->willReturn($this->rootDirectory->getChild('package1')->url() . '/');
160  $package->getPackageKey()->willReturn('package1');
161 
162  $this->packageManagerProphecy->getActivePackages()->willReturn([$package->reveal()]);
163 
164  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal(), $this->containerProphecy->reveal());
165  $command = $commandRegistry->getCommandByIdentifier('first:command');
166 
167  self::assertInstanceOf(Command::class, $command);
168  }
169 
173  public function throwsUnknownCommandExceptionIfUnregisteredCommandIsRequested()
174  {
175  $this->packageManagerProphecy->getActivePackages()->willReturn([]);
176 
177  $this->expectException(UnknownCommandException::class);
178  $this->expectExceptionCode(1510906768);
179 
180  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal(), $this->containerProphecy->reveal());
181  $commandRegistry->getCommandByIdentifier('foo');
182  }
183 }
‪TYPO3\CMS\Core\Console\UnknownCommandException
Definition: UnknownCommandException.php:26
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:22
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:32
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Console
Definition: CommandRegistryTest.php:18
‪TYPO3\CMS\Core\Console\CommandNameAlreadyInUseException
Definition: CommandNameAlreadyInUseException.php:25