-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dcccbd
commit 811901b
Showing
6 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Custom\Library; | ||
|
||
use Drush\Commands\AutowireTrait; | ||
use Drush\Commands\DrushCommands; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
|
||
final class AutowireTest extends DrushCommands | ||
{ | ||
use AutowireTrait; | ||
|
||
public function __construct( | ||
#[Autowire('a string as it is')] | ||
public readonly string $argListStringValue, | ||
#[Autowire(null, 'autowire_test')] | ||
public readonly AutowireTestService $argListContainerService, | ||
#[Autowire(null, null, null, null, 'foo')] | ||
public readonly string $argListContainerParam, | ||
#[Autowire(value: 'a string as it is')] | ||
public readonly string $namedArgStringValue, | ||
#[Autowire(service: 'autowire_test')] | ||
public readonly AutowireTestService $namedArgContainerService, | ||
#[Autowire(param: 'foo')] | ||
public readonly string $namedArgContainerParam, | ||
public readonly AutowireTestServiceInterface $noAutowireAttributeContainerService, | ||
) { | ||
parent::__construct(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Custom\Library; | ||
|
||
final class AutowireTestService implements AutowireTestServiceInterface | ||
{ | ||
public function greeting(): string | ||
{ | ||
return 'Hello World!'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Custom\Library; | ||
|
||
interface AutowireTestServiceInterface | ||
{ | ||
public function greeting(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Custom\Library; | ||
|
||
use Drupal\Component\DependencyInjection\ContainerInterface; | ||
use Drupal\Core\Routing\RedirectDestinationInterface; | ||
use Drush\Commands\DrushCommands; | ||
use Psr\Log\LoggerInterface; | ||
|
||
final class CreateFactoryExpectsDrupalContainer extends DrushCommands | ||
{ | ||
public function __construct( | ||
public readonly string $string, | ||
public readonly RedirectDestinationInterface $redirectDestination, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public static function create(ContainerInterface $container): self | ||
{ | ||
return new self('a string as it is', $container->get('redirect.destination')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Custom\Library; | ||
|
||
use Drush\Commands\DrushCommands; | ||
use League\Container\DefinitionContainerInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
final class CreateExpectsDrushContainer extends DrushCommands | ||
{ | ||
public function __construct( | ||
public readonly string $string, | ||
public readonly LoggerInterface $log, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public static function create(DefinitionContainerInterface $container): self | ||
{ | ||
return new self('a string as it is', $container->get('logger')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Unish; | ||
|
||
use Custom\Library\AutowireTest; | ||
use Custom\Library\AutowireTestService; | ||
use Custom\Library\AutowireTestServiceInterface; | ||
use Custom\Library\CreateExpectsDrushContainer; | ||
use Drupal\Core\DependencyInjection\ContainerBuilder; | ||
use Drush\Commands\AutowireTrait; | ||
use Drush\Log\Logger; | ||
use League\Container\Container; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Output\NullOutput; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @covers \Drush\Commands\AutowireTrait::create | ||
* @group base | ||
*/ | ||
class AutowireArgumentsTest extends TestCase | ||
{ | ||
public function testDrupalContainer(): void | ||
{ | ||
$drupalContainer = new ContainerBuilder(); | ||
$drupalContainer->register('autowire_test', AutowireTestService::class); | ||
$drupalContainer->setAlias(AutowireTestServiceInterface::class, new Reference('autowire_test')); | ||
$drupalContainer->setParameter('foo', 'bar'); | ||
$drupalContainer->compile(); | ||
$drushContainer = new Container(); | ||
$drushContainer->delegate($drupalContainer); | ||
|
||
$instance = AutowireTest::create($drupalContainer, $drushContainer); | ||
$this->assertInstanceOf(AutowireTest::class, $instance); | ||
|
||
$this->assertSame('a string as it is', $instance->argListStringValue); | ||
$this->assertSame('Hello World!', $instance->argListContainerService->greeting()); | ||
$this->assertSame('bar', $instance->argListContainerParam); | ||
$this->assertSame('a string as it is', $instance->namedArgStringValue); | ||
$this->assertSame('Hello World!', $instance->namedArgContainerService->greeting()); | ||
$this->assertSame('bar', $instance->namedArgContainerParam); | ||
$this->assertSame('Hello World!', $instance->noAutowireAttributeContainerService->greeting()); | ||
} | ||
|
||
public function testDrushContainer(): void | ||
{ | ||
$drushContainer = new Container(); | ||
$drushContainer->add('autowire_test', AutowireTestService::class); | ||
$drushContainer->add(AutowireTestServiceInterface::class, AutowireTestService::class); | ||
|
||
$instance = AutowireTest::create($drushContainer); | ||
$this->assertInstanceOf(AutowireTest::class, $instance); | ||
|
||
$this->assertSame('a string as it is', $instance->argListStringValue); | ||
$this->assertSame('Hello World!', $instance->argListContainerService->greeting()); | ||
// Drush container has no Drupal container as delegate. It can't resolve the container param. | ||
$this->assertSame('%foo%', $instance->argListContainerParam); | ||
$this->assertSame('a string as it is', $instance->namedArgStringValue); | ||
$this->assertSame('Hello World!', $instance->namedArgContainerService->greeting()); | ||
// Drush container has no Drupal container as delegate. It can't resolve the container param. | ||
$this->assertSame('%foo%', $instance->namedArgContainerParam); | ||
$this->assertSame('Hello World!', $instance->noAutowireAttributeContainerService->greeting()); | ||
} | ||
|
||
public function testClassWithStaticCreateFactory(): void | ||
{ | ||
$drushContainer = new Container(); | ||
$drushContainer->add('logger', Logger::class)->addArgument(new NullOutput()); | ||
|
||
$instance = CreateExpectsDrushContainer::create($drushContainer); | ||
$this->assertInstanceOf(CreateExpectsDrushContainer::class, $instance); | ||
|
||
$this->assertSame('a string as it is', $instance->string); | ||
// If logger injection failed, this line will fail the test. | ||
$instance->log->debug('debug string'); | ||
} | ||
} |