‪TYPO3CMS  9.5
CommandRegistryTest.php
Go to the documentation of this file.
1 <?php
2 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 
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;
27 
31 class CommandRegistryTest extends UnitTestCase
32 {
36  protected $rootDirectory;
37 
41  protected $packageManagerProphecy;
42 
46  protected function setUp()
47  {
48  $commandMockClass = $this->getMockClass(Command::class, ['dummy']);
49  $this->rootDirectory = vfsStream::setup('root', null, [
50  'package1' => [
51  'Configuration' => [
52  'Commands.php' => '<?php return ["first:command" => [ "class" => "' . $commandMockClass . '" ]];',
53  ],
54  ],
55  'package2' => [
56  'Configuration' => [
57  'Commands.php' => '<?php return ["second:command" => [ "class" => "' . $commandMockClass . '" ]];',
58  ],
59  ],
60  'package3' => [
61  'Configuration' => [
62  'Commands.php' => '<?php return ["third:command" => [ "class" => "' . $commandMockClass . '" ]];',
63  ],
64  ],
65  'package4' => [
66  'Configuration' => [
67  'Commands.php' => '<?php return ["third:command" => [ "class" => "' . $commandMockClass . '" ]];',
68  ],
69  ],
70  ]);
71 
73  $this->packageManagerProphecy = $this->prophesize(PackageManager::class);
74  }
75 
79  public function iteratesCommandsOfActivePackages()
80  {
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() . '/');
87 
88  $this->packageManagerProphecy->getActivePackages()->willReturn([$package1->reveal(), $package2->reveal()]);
89 
90  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal());
91  $commands = iterator_to_array($commandRegistry);
92 
93  $this->assertCount(2, $commands);
94  $this->assertContainsOnlyInstancesOf(Command::class, $commands);
95  }
96 
100  public function throwsExceptionOnDuplicateCommand()
101  {
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');
109 
110  $this->packageManagerProphecy->getActivePackages()->willReturn([$package3->reveal(), $package4->reveal()]);
111 
112  $this->expectException(CommandNameAlreadyInUseException::class);
113  $this->expectExceptionCode(1484486383);
114 
115  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal());
116  iterator_to_array($commandRegistry);
117  }
118 
122  public function getCommandByIdentifierReturnsRegisteredCommand()
123  {
125  $package = $this->prophesize(PackageInterface::class);
126  $package->getPackagePath()->willReturn($this->rootDirectory->getChild('package1')->url() . '/');
127  $package->getPackageKey()->willReturn('package1');
128 
129  $this->packageManagerProphecy->getActivePackages()->willReturn([$package->reveal()]);
130 
131  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal());
132  $command = $commandRegistry->getCommandByIdentifier('first:command');
133 
134  $this->assertInstanceOf(Command::class, $command);
135  }
136 
140  public function throwsUnknowCommandExceptionIfUnregisteredCommandIsRequested()
141  {
142  $this->packageManagerProphecy->getActivePackages()->willReturn([]);
143 
144  $this->expectException(UnknownCommandException::class);
145  $this->expectExceptionCode(1510906768);
146 
147  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal());
148  $commandRegistry->getCommandByIdentifier('foo');
149  }
150 }
‪TYPO3\CMS\Core\Console\UnknownCommandException
Definition: UnknownCommandException.php:24
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:21
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:27
‪TYPO3\CMS\Core\Console\CommandNameAlreadyInUseException
Definition: CommandNameAlreadyInUseException.php:24
‪TYPO3\CMS\Core\Tests\Unit\Console
Definition: CommandRegistryTest.php:3