|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Unish; |
| 4 | + |
| 5 | +use Custom\Library\AutowireTest; |
| 6 | +use Custom\Library\AutowireTestService; |
| 7 | +use Custom\Library\AutowireTestServiceInterface; |
| 8 | +use Custom\Library\CreateExpectsDrushContainer; |
| 9 | +use Drupal\Core\DependencyInjection\ContainerBuilder; |
| 10 | +use Drush\Commands\AutowireTrait; |
| 11 | +use Drush\Log\Logger; |
| 12 | +use League\Container\Container; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use Symfony\Component\Console\Output\NullOutput; |
| 15 | +use Symfony\Component\DependencyInjection\Reference; |
| 16 | + |
| 17 | +/** |
| 18 | + * @covers \Drush\Commands\AutowireTrait::create |
| 19 | + * @group base |
| 20 | + */ |
| 21 | +class AutowireArgumentsTest extends TestCase |
| 22 | +{ |
| 23 | + public function testDrupalContainer(): void |
| 24 | + { |
| 25 | + $drupalContainer = new ContainerBuilder(); |
| 26 | + $drupalContainer->register('autowire_test', AutowireTestService::class); |
| 27 | + $drupalContainer->setAlias(AutowireTestServiceInterface::class, new Reference('autowire_test')); |
| 28 | + $drupalContainer->setParameter('foo', 'bar'); |
| 29 | + $drupalContainer->compile(); |
| 30 | + $drushContainer = new Container(); |
| 31 | + $drushContainer->delegate($drupalContainer); |
| 32 | + |
| 33 | + $instance = AutowireTest::create($drupalContainer, $drushContainer); |
| 34 | + $this->assertInstanceOf(AutowireTest::class, $instance); |
| 35 | + |
| 36 | + $this->assertSame('a string as it is', $instance->argListStringValue); |
| 37 | + $this->assertSame('Hello World!', $instance->argListContainerService->greeting()); |
| 38 | + $this->assertSame('bar', $instance->argListContainerParam); |
| 39 | + $this->assertSame('a string as it is', $instance->namedArgStringValue); |
| 40 | + $this->assertSame('Hello World!', $instance->namedArgContainerService->greeting()); |
| 41 | + $this->assertSame('bar', $instance->namedArgContainerParam); |
| 42 | + $this->assertSame('Hello World!', $instance->noAutowireAttributeContainerService->greeting()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testDrushContainer(): void |
| 46 | + { |
| 47 | + $drushContainer = new Container(); |
| 48 | + $drushContainer->add('autowire_test', AutowireTestService::class); |
| 49 | + $drushContainer->add(AutowireTestServiceInterface::class, AutowireTestService::class); |
| 50 | + |
| 51 | + $instance = AutowireTest::create($drushContainer); |
| 52 | + $this->assertInstanceOf(AutowireTest::class, $instance); |
| 53 | + |
| 54 | + $this->assertSame('a string as it is', $instance->argListStringValue); |
| 55 | + $this->assertSame('Hello World!', $instance->argListContainerService->greeting()); |
| 56 | + // Drush container has no Drupal container as delegate. It can't resolve the container param. |
| 57 | + $this->assertSame('%foo%', $instance->argListContainerParam); |
| 58 | + $this->assertSame('a string as it is', $instance->namedArgStringValue); |
| 59 | + $this->assertSame('Hello World!', $instance->namedArgContainerService->greeting()); |
| 60 | + // Drush container has no Drupal container as delegate. It can't resolve the container param. |
| 61 | + $this->assertSame('%foo%', $instance->namedArgContainerParam); |
| 62 | + $this->assertSame('Hello World!', $instance->noAutowireAttributeContainerService->greeting()); |
| 63 | + } |
| 64 | + |
| 65 | + public function testClassWithStaticCreateFactory(): void |
| 66 | + { |
| 67 | + $drushContainer = new Container(); |
| 68 | + $drushContainer->add('logger', Logger::class)->addArgument(new NullOutput()); |
| 69 | + |
| 70 | + $instance = CreateExpectsDrushContainer::create($drushContainer); |
| 71 | + $this->assertInstanceOf(CreateExpectsDrushContainer::class, $instance); |
| 72 | + |
| 73 | + $this->assertSame('a string as it is', $instance->string); |
| 74 | + // If logger injection failed, this line will fail the test. |
| 75 | + $instance->log->debug('debug string'); |
| 76 | + } |
| 77 | +} |
0 commit comments