e348d305ee621b06028b0d69d724371e940e80f6
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Console / CommandRegistryTest.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Console;
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 Symfony\Component\Console\Command\Command;
20 use TYPO3\CMS\Core\Console\CommandNameAlreadyInUseException;
21 use TYPO3\CMS\Core\Console\CommandRegistry;
22 use TYPO3\CMS\Core\Package\PackageInterface;
23 use TYPO3\CMS\Core\Package\PackageManager;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25
26 /**
27 * Testcase for CommandRegistry
28 */
29 class CommandRegistryTest extends UnitTestCase
30 {
31 /**
32 * @var \org\bovigo\vfs\vfsStreamDirectory
33 */
34 protected $rootDirectory;
35
36 /**
37 * @var PackageManager|\Prophecy\Prophecy\ObjectProphecy
38 */
39 protected $packageManagerProphecy;
40
41 /**
42 * Set up this testcase
43 */
44 protected function setUp()
45 {
46 $commandMockClass = $this->getMockClass(Command::class, ['dummy']);
47 $this->rootDirectory = vfsStream::setup('root', null, [
48 'package1' => [
49 'Configuration' => [
50 'Commands.php' => '<?php return ["first:command" => [ "class" => "' . $commandMockClass . '" ]];',
51 ],
52 ],
53 'package2' => [
54 'Configuration' => [
55 'Commands.php' => '<?php return ["second:command" => [ "class" => "' . $commandMockClass . '" ]];',
56 ],
57 ],
58 'package3' => [
59 'Configuration' => [
60 'Commands.php' => '<?php return ["third:command" => [ "class" => "' . $commandMockClass . '" ]];',
61 ],
62 ],
63 'package4' => [
64 'Configuration' => [
65 'Commands.php' => '<?php return ["third:command" => [ "class" => "' . $commandMockClass . '" ]];',
66 ],
67 ],
68 ]);
69
70 /** @var PackageManager */
71 $this->packageManagerProphecy = $this->prophesize(PackageManager::class);
72 }
73
74 /**
75 * @test
76 */
77 public function iteratesCommandsOfActivePackages()
78 {
79 /** @var PackageInterface */
80 $package1 = $this->prophesize(PackageInterface::class);
81 $package1->getPackagePath()->willReturn($this->rootDirectory->getChild('package1')->url() . '/');
82 /** @var PackageInterface */
83 $package2 = $this->prophesize(PackageInterface::class);
84 $package2->getPackagePath()->willReturn($this->rootDirectory->getChild('package2')->url() . '/');
85
86 $this->packageManagerProphecy->getActivePackages()->willReturn([$package1->reveal(), $package2->reveal()]);
87
88 $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal());
89 $commands = iterator_to_array($commandRegistry);
90
91 $this->assertCount(2, $commands);
92 $this->assertContainsOnlyInstancesOf(Command::class, $commands);
93 }
94
95 /**
96 * @test
97 */
98 public function throwsExceptionOnDuplicateCommand()
99 {
100 /** @var PackageInterface */
101 $package3 = $this->prophesize(PackageInterface::class);
102 $package3->getPackagePath()->willReturn($this->rootDirectory->getChild('package3')->url() . '/');
103 /** @var PackageInterface */
104 $package4 = $this->prophesize(PackageInterface::class);
105 $package4->getPackagePath()->willReturn($this->rootDirectory->getChild('package4')->url() . '/');
106 $package4->getPackageKey()->willReturn('package4');
107
108 $this->packageManagerProphecy->getActivePackages()->willReturn([$package3->reveal(), $package4->reveal()]);
109
110 $this->expectException(CommandNameAlreadyInUseException::class);
111 $this->expectExceptionCode(1484486383);
112
113 $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal());
114 iterator_to_array($commandRegistry);
115 }
116 }