‪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 Psr\Container\ContainerInterface;
22 use Symfony\Component\Console\Command\Command;
23 use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
26 use TYPO3\CMS\Core\Package\PackageManager;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 class CommandRegistryTest extends UnitTestCase
33 {
37  protected $rootDirectory;
38 
42  protected $packageManagerProphecy;
43 
47  protected $containerProphecy;
48 
52  protected function setUp(): void
53  {
54  parent::setUp();
55 
57  $this->packageManagerProphecy = $this->prophesize(PackageManager::class);
58  $this->packageManagerProphecy->getActivePackages()->willReturn([]);
59 
61  $this->containerProphecy = $this->prophesize(ContainerInterface::class);
62  }
63 
67  public function implementsCommandLoaderInterface()
68  {
69  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal(), $this->containerProphecy->reveal());
70  self::assertInstanceof(CommandLoaderInterface::class, $commandRegistry);
71  }
72 
76  public function iteratesLazyCommandsOfActivePackages()
77  {
78  $command1MockClass = $this->getMockClass(Command::class, ['dummy']);
79  $command2MockClass = $this->getMockClass(Command::class, ['dummy']);
80 
81  $this->containerProphecy->get('command1')->willReturn(new $command1MockClass());
82  $this->containerProphecy->get('command2')->willReturn(new $command2MockClass());
83 
84  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal(), $this->containerProphecy->reveal());
85  $commandRegistry->addLazyCommand('test:command', 'command1');
86  $commandRegistry->addLazyCommand('test:command2', 'command2');
87 
88  $commandNames = $commandRegistry->getNames();
89 
90  self::assertCount(2, $commandNames);
91  self::assertInstanceOf($command1MockClass, $commandRegistry->get('test:command'));
92  self::assertInstanceOf($command1MockClass, $commandRegistry->get('test:command2'));
93  }
94 
98  public function iteratesLegacyCommandsOfActivePackages()
99  {
100  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal(), $this->containerProphecy->reveal());
101  $commands = iterator_to_array($commandRegistry->getLegacyCommands());
102 
103  self::assertCount(0, $commands);
104  self::assertContainsOnlyInstancesOf(Command::class, $commands);
105  }
106 
110  public function lazyCommandOverridesLegacyCommandsWithoutDeprecationError()
111  {
112  $commandMockClass = $this->getMockClass(Command::class, ['dummy']);
113  $this->rootDirectory = vfsStream::setup('root', null, [
114  'package1' => [
115  'Configuration' => [
116  'Commands.php' => '<?php return ["first:command" => [ "class" => "' . $commandMockClass . '" ]];',
117  ],
118  ],
119  ]);
121  $package = $this->prophesize(PackageInterface::class);
122  $package->getPackagePath()->willReturn($this->rootDirectory->getChild('package1')->url() . '/');
123 
124  $this->packageManagerProphecy->getActivePackages()->willReturn([$package->reveal()]);
125 
126  $this->containerProphecy->get($commandMockClass)->willReturn(new $commandMockClass());
127 
128  $commandRegistry = new CommandRegistry($this->packageManagerProphecy->reveal(), $this->containerProphecy->reveal());
129  $commandRegistry->addLazyCommand('first:command', $commandMockClass);
130 
131  $nonLazyCommands = iterator_to_array($commandRegistry->getLegacyCommands());
132  $lazyCommands = $commandRegistry->getNames();
133 
134  self::assertCount(0, $nonLazyCommands);
135  self::assertCount(1, $lazyCommands);
136  }
137 }
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:22
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:32
‪TYPO3\CMS\Core\Tests\Unit\Console
Definition: CommandRegistryTest.php:18