[FEATURE] Introduce scheduler task to execute console commands
[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 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;
27
28 /**
29 * Testcase for CommandRegistry
30 */
31 class CommandRegistryTest extends UnitTestCase
32 {
33 /**
34 * @var \org\bovigo\vfs\vfsStreamDirectory
35 */
36 protected $rootDirectory;
37
38 /**
39 * @var PackageManager|\Prophecy\Prophecy\ObjectProphecy
40 */
41 protected $packageManagerProphecy;
42
43 /**
44 * Set up this testcase
45 */
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
72 /** @var PackageManager */
73 $this->packageManagerProphecy = $this->prophesize(PackageManager::class);
74 }
75
76 /**
77 * @test
78 */
79 public function iteratesCommandsOfActivePackages()
80 {
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() . '/');
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
97 /**
98 * @test
99 */
100 public function throwsExceptionOnDuplicateCommand()
101 {
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');
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
119 /**
120 * @test
121 */
122 public function getCommandByIdentifierReturnsRegisteredCommand()
123 {
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');
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
137 /**
138 * @test
139 */
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 }