‪TYPO3CMS  ‪main
ConsoleCommandPassTest.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 PHPUnit\Framework\Attributes\Test;
21 use Psr\Container\ContainerInterface;
22 use Symfony\Component\Console\Command\Command;
25 use TYPO3\CMS\Core\DependencyInjection\ContainerBuilder;
27 use TYPO3\CMS\Core\Package\PackageManager;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
32 final class ‪ConsoleCommandPassTest extends UnitTestCase
33 {
34  private function ‪buildContainer(string $uniqid, array $packages = []): ContainerInterface
35  {
36  $packageManagerMock = $this->createMock(PackageManager::class);
37  $activePackages = [];
38  foreach ($packages as $packageKey => $config) {
39  $packageMock = $this->createMock(Package::class);
40  $packageMock->method('getPackageKey')->willReturn($packageKey);
41  $packageMock->method('getPackagePath')->willReturn($config['path']);
42  $packageMock->method('isPartOfMinimalUsableSystem')->willReturn(false);
43  $packageMock->method('getServiceProvider')->willReturn($config['serviceProvider'] ?? NullServiceProvider::class);
44  $activePackages[$packageKey] = $packageMock;
45  }
46 
47  $packageManagerMock->method('getCacheIdentifier')->willReturn('PackageManager.' . $uniqid);
48  $packageManagerMock->method('getActivePackages')->willReturn($activePackages);
49 
50  $cacheMock = $this->createMock(PhpFrontend::class);
51  $cacheMock->method('requireOnce')->with(self::isType('string'))->willReturn(false);
52  $cacheMock->method('set')->willReturnCallback(function (string $entryIdentifier, string $sourceCode): void {
53  eval($sourceCode);
54  });
55 
56  return (new ContainerBuilder([]))->createDependencyInjectionContainer($packageManagerMock, $cacheMock);
57  }
58 
59  #[Test]
60  public function ‪commandRegistrationViaTags(): void
61  {
62  $container = $this->‪buildContainer(__METHOD__, [
63  'command-registry-package' => [
64  'path' => __DIR__ . '/Fixtures/CommandRegistryPackage/',
65  'serviceProvider' => CommandRegistryServiceProvider::class,
66  ],
67  'package1' => [
68  'path' => __DIR__ . '/Fixtures/Package1/',
69  ],
70  ]);
71 
72  $commandRegistry = $container->get(CommandRegistry::class);
73 
74  self::assertTrue($commandRegistry->has('test:cmd'));
75  self::assertEquals(['test:cmd'], $commandRegistry->getNames());
76  self::assertEquals('Dummy description including new as word', $commandRegistry->filter()['test:cmd']['description'] ?? '');
77  self::assertInstanceOf(Command::class, $commandRegistry->getCommandByIdentifier('test:cmd'));
78  }
79 
80  #[Test]
81  public function ‪withoutConfiguration(): void
82  {
83  $container = $this->‪buildContainer(__METHOD__, [
84  'command-registry-package' => [
85  'path' => __DIR__ . '/Fixtures/CommandRegistryPackage/',
86  'serviceProvider' => CommandRegistryServiceProvider::class,
87  ],
88  ]);
89 
90  $commandRegistry = $container->get(CommandRegistry::class);
91 
92  self::assertEquals([], $commandRegistry->getNames());
93  self::assertFalse($commandRegistry->has('unknown:command'));
94  }
95 }
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ConsoleCommandPassTest\buildContainer
‪buildContainer(string $uniqid, array $packages=[])
Definition: ConsoleCommandPassTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ConsoleCommandPassTest\withoutConfiguration
‪withoutConfiguration()
Definition: ConsoleCommandPassTest.php:81
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ConsoleCommandPassTest\commandRegistrationViaTags
‪commandRegistrationViaTags()
Definition: ConsoleCommandPassTest.php:60
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ConsoleCommandPassTest
Definition: ConsoleCommandPassTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\Fixtures\NullServiceProvider
Definition: NullServiceProvider.php:23
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:31
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection
Definition: ConsoleCommandPassTest.php:18
‪TYPO3\CMS\Core\Package\Package
Definition: Package.php:30
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\Fixtures\CommandRegistryPackage\CommandRegistryServiceProvider
Definition: CommandRegistryServiceProvider.php:25