-
-
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 02471ee
Showing
7 changed files
with
211 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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\woot; | ||
|
||
final class AutowireTestService implements AutowireTestServiceInterface | ||
{ | ||
public function __toString(): 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,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\woot; | ||
|
||
interface AutowireTestServiceInterface extends \Stringable {} |
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
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')); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
tests/fixtures/lib/Drush/Commands/AutowireTestCommands.php
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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Custom\Library\Drush\Commands; | ||
|
||
use Drupal\woot\AutowireTestService; | ||
use Drupal\woot\AutowireTestServiceInterface; | ||
use Drush\Attributes as CLI; | ||
use Drush\Boot\DrupalBootLevels; | ||
use Drush\Commands\AutowireTrait; | ||
use Drush\Commands\DrushCommands; | ||
use League\Container\ContainerAwareInterface; | ||
use League\Container\ContainerAwareTrait; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
|
||
final class AutowireTestCommands extends DrushCommands | ||
{ | ||
use AutowireTrait; | ||
|
||
public function __construct( | ||
#[Autowire('a string as it is')] | ||
private readonly string $argListStringValue, | ||
#[Autowire(null, 'woot.autowire_test')] | ||
private readonly AutowireTestService $argListContainerService, | ||
#[Autowire(null, null, null, null, 'foo')] | ||
private readonly string $argListContainerParam, | ||
#[Autowire(value: 'a string as it is')] | ||
private readonly string $namedArgStringValue, | ||
#[Autowire(service: 'woot.autowire_test')] | ||
private readonly AutowireTestService $namedArgContainerService, | ||
#[Autowire(param: 'foo')] | ||
private readonly string $namedArgContainerParam, | ||
private readonly AutowireTestServiceInterface $noAutowireAttributeContainerService, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
#[CLI\Command(name: 'test_autowire:drupal-container')] | ||
#[CLI\Bootstrap(level: DrupalBootLevels::FULL)] | ||
#[CLI\Help(hidden: true)] | ||
public function drupal(): string | ||
{ | ||
$values = []; | ||
$constructor = new \ReflectionMethod($this, '__construct'); | ||
foreach ($constructor->getParameters() as $param) { | ||
$values[] = (string) $this->{$param->getName()}; | ||
} | ||
return implode("\n", $values); | ||
} | ||
|
||
#[CLI\Command(name: 'test_autowire:drush-container')] | ||
#[CLI\Bootstrap(level: DrupalBootLevels::NONE)] | ||
#[CLI\Help(hidden: true)] | ||
public function drush(): string | ||
{ | ||
$values = []; | ||
$constructor = new \ReflectionMethod($this, '__construct'); | ||
foreach ($constructor->getParameters() as $param) { | ||
$values[] = (string) $this->{$param->getName()}; | ||
} | ||
return implode("\n", $values); | ||
} | ||
} |
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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Unish; | ||
|
||
use Drupal\woot\AutowireTestService; | ||
use Drupal\woot\AutowireTestServiceInterface; | ||
use Drush\Commands\pm\PmCommands; | ||
use Drush\Drush; | ||
|
||
/** | ||
* @covers \Drush\Commands\AutowireTrait::create | ||
* @group base | ||
*/ | ||
class AutowireArgumentsTest extends UnishIntegrationTestCase | ||
{ | ||
public function testWithDrupalContainer(): void | ||
{ | ||
$this->drush(PmCommands::INSTALL, ['woot']); | ||
|
||
$expected = [ | ||
// Autowire('a string as it is') | ||
'a string as it is', | ||
// Autowire(null, 'woot.autowire_test') | ||
'Hello World!', | ||
// Autowire(null, null, null, null, 'foo') | ||
'bar', | ||
// Autowire(value: 'a string as it is') | ||
'a string as it is', | ||
// Autowire(service: 'woot.autowire_test') | ||
'Hello World!', | ||
// Autowire(param: 'foo') | ||
'bar', | ||
// Autowire by service full qualified interface name | ||
// @see \Drupal\woot\AutowireTestServiceInterface | ||
'Hello World!', | ||
]; | ||
|
||
$this->drush('test_autowire:drupal-container'); | ||
$this->assertSame(implode("\n", $expected), $this->getOutput()); | ||
|
||
$this->drush(PmCommands::UNINSTALL, ['woot']); | ||
} | ||
|
||
// @todo This test will fail because I coudn't find a way to add a new | ||
// service to Drush container. | ||
public function testWithDrushContainer(): void | ||
{ | ||
$drushContainer = Drush::getContainer(); | ||
$drushContainer->add('woot.autowire_test', AutowireTestService::class); | ||
$drushContainer->add(AutowireTestServiceInterface::class, AutowireTestService::class); | ||
Drush::setContainer($drushContainer); | ||
|
||
$expected = [ | ||
// Autowire('a string as it is') | ||
'a string as it is', | ||
// Autowire(null, 'woot.autowire_test') | ||
'Hello World!', | ||
// Autowire(null, null, null, null, 'foo') | ||
'bar', | ||
// Autowire(value: 'a string as it is') | ||
'a string as it is', | ||
// Autowire(service: 'woot.autowire_test') | ||
'Hello World!', | ||
// Autowire(param: 'foo') | ||
'bar', | ||
// Autowire by service full qualified interface name | ||
// @see \Drupal\woot\AutowireTestServiceInterface | ||
'Hello World!', | ||
]; | ||
|
||
$this->drush('test_autowire:drush-container'); | ||
$this->assertSame(implode("\n", $expected), $output); | ||
} | ||
} |