|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2021 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Framework\App\Test\Unit\Console\CommandLoader; |
| 9 | + |
| 10 | +use Magento\Framework\Console\CommandLoader\Aggregate; |
| 11 | +use PHPUnit\Framework\MockObject\MockObject; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; |
| 15 | +use Symfony\Component\Console\Exception\CommandNotFoundException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests the "aggregate" command loader |
| 19 | + * @see Aggregate |
| 20 | + */ |
| 21 | +class AggregateTest extends TestCase |
| 22 | +{ |
| 23 | + /** @var CommandLoaderInterface|MockObject */ |
| 24 | + private MockObject|CommandLoaderInterface $firstMockCommandLoader; |
| 25 | + |
| 26 | + /** @var CommandLoaderInterface|MockObject */ |
| 27 | + private MockObject|CommandLoaderInterface $secondMockCommandLoader; |
| 28 | + |
| 29 | + /** @var Aggregate */ |
| 30 | + private Aggregate $aggregateCommandLoader; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + $this->firstMockCommandLoader = $this->getMockBuilder(CommandLoaderInterface::class)->getMock(); |
| 35 | + $this->secondMockCommandLoader = $this->getMockBuilder(CommandLoaderInterface::class)->getMock(); |
| 36 | + $this->aggregateCommandLoader = new Aggregate([$this->firstMockCommandLoader, $this->secondMockCommandLoader]); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Test the various cases of `has` for the aggregate command loader: |
| 41 | + * - When at least one "internal" command loader has a command, the aggregate does as well |
| 42 | + * - When none of the "internal" command loaders has a command, neither does the aggregate |
| 43 | + * |
| 44 | + * @dataProvider provideTestCasesForHas |
| 45 | + */ |
| 46 | + public function testHas(bool $firstResult, bool $secondResult, bool $overallResult): void |
| 47 | + { |
| 48 | + $this->firstMockCommandLoader->method('has')->with('foo')->willReturn($firstResult); |
| 49 | + $this->secondMockCommandLoader->method('has')->with('foo')->willReturn($secondResult); |
| 50 | + |
| 51 | + $this->assertEquals($overallResult, $this->aggregateCommandLoader->has('foo')); |
| 52 | + } |
| 53 | + |
| 54 | + public function provideTestCasesForHas(): array |
| 55 | + { |
| 56 | + return [ |
| 57 | + [true, false, true], |
| 58 | + [false, true, true], |
| 59 | + [false, false, false] |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test the various cases of `get` for the aggregate command loader. Similar to `has`, |
| 65 | + * the return value of `Aggregate::get` mirrors its internal command loaders. |
| 66 | + * |
| 67 | + * For simplicity, this test does not cover the "no results" case. @see testGetThrow |
| 68 | + * |
| 69 | + * @dataProvider provideTestCasesForGet |
| 70 | + */ |
| 71 | + public function testGet(?Command $firstCmd, ?Command $secondCmd): void |
| 72 | + { |
| 73 | + $firstHas = (bool)$firstCmd; |
| 74 | + $secondHas = (bool)$secondCmd; |
| 75 | + |
| 76 | + $this->firstMockCommandLoader->method('has')->with('foo')->willReturn($firstHas); |
| 77 | + if ($firstHas) { |
| 78 | + $this->firstMockCommandLoader->method('get')->with('foo')->willReturn($firstCmd); |
| 79 | + } |
| 80 | + |
| 81 | + $this->secondMockCommandLoader->method('has')->with('foo')->willReturn($secondHas); |
| 82 | + if ($secondHas) { |
| 83 | + $this->secondMockCommandLoader->method('get')->with('foo')->willReturn($secondCmd); |
| 84 | + } |
| 85 | + |
| 86 | + $this->assertInstanceOf(Command::class, $this->aggregateCommandLoader->get('foo')); |
| 87 | + } |
| 88 | + |
| 89 | + public function provideTestCasesForGet(): array |
| 90 | + { |
| 91 | + return [ |
| 92 | + [ |
| 93 | + new Command(), |
| 94 | + null |
| 95 | + ], |
| 96 | + [ |
| 97 | + null, |
| 98 | + new Command() |
| 99 | + ] |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * When none of the internal command loaders have matching commands, the aggregate command loader |
| 105 | + * will throw an exception. @see CommandNotFoundException |
| 106 | + */ |
| 107 | + public function testGetThrow(): void |
| 108 | + { |
| 109 | + $this->firstMockCommandLoader->method('has')->with('foo')->willReturn(false); |
| 110 | + $this->secondMockCommandLoader->method('has')->with('foo')->willReturn(false); |
| 111 | + |
| 112 | + $this->expectException(CommandNotFoundException::class); |
| 113 | + $this->aggregateCommandLoader->get('foo'); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * An aggregate command loader's `getNames` method returns the merged array of the `getNames` |
| 118 | + * return values of all its internal command loaders |
| 119 | + */ |
| 120 | + public function testGetNames(): void |
| 121 | + { |
| 122 | + $this->firstMockCommandLoader->method('getNames')->willReturn(['foo', 'bar']); |
| 123 | + $this->secondMockCommandLoader->method('getNames')->willReturn(['baz', 'qux']); |
| 124 | + |
| 125 | + $this->assertEquals(['foo', 'bar', 'baz', 'qux'], $this->aggregateCommandLoader->getNames()); |
| 126 | + } |
| 127 | +} |
0 commit comments