-
-
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.
Extend autowiring to container params & plain values
- Loading branch information
1 parent
937127e
commit 3e0ade3
Showing
5 changed files
with
131 additions
and
8 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
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,35 @@ | ||
<?php | ||
|
||
namespace Custom\Library\AutowireTestClasses; | ||
|
||
use Drush\Commands\AutowireTrait; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
|
||
class AutowireTest | ||
{ | ||
use AutowireTrait; | ||
|
||
public function __construct( | ||
#[Autowire('a string as it is')] | ||
protected string $argListPlainValue, | ||
#[Autowire(null, 'autowire_test')] | ||
protected AutowireTestService $argListContainerService, | ||
#[Autowire(null, null, null, null, 'foo')] | ||
protected string $argListContainerParam, | ||
#[Autowire(value: 'a string as it is')] | ||
protected string $namedArgPlainValue, | ||
#[Autowire(service: 'autowire_test')] | ||
protected AutowireTestService $namedArgContainerService, | ||
#[Autowire(param: 'foo')] | ||
protected string $namedArgContainerParam, | ||
protected AutowireTestServiceInterface $noAutowireAttributeContainerService, | ||
) { | ||
assert($argListPlainValue === 'a string as it is'); | ||
assert($argListContainerService->greeting() === 'Hello World!'); | ||
assert($argListContainerParam === 'bar'); | ||
assert($namedArgPlainValue === 'a string as it is'); | ||
assert($namedArgContainerService->greeting() === 'Hello World!'); | ||
assert($namedArgContainerParam === 'bar'); | ||
assert($noAutowireAttributeContainerService->greeting() === 'Hello World!'); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/fixtures/lib/AutowireTestClasses/AutowireTestService.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,11 @@ | ||
<?php | ||
|
||
namespace Custom\Library\AutowireTestClasses; | ||
|
||
class AutowireTestService implements AutowireTestServiceInterface | ||
{ | ||
public function greeting(): string | ||
{ | ||
return 'Hello World!'; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/lib/AutowireTestClasses/AutowireTestServiceInterface.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,7 @@ | ||
<?php | ||
|
||
namespace Custom\Library\AutowireTestClasses; | ||
|
||
interface AutowireTestServiceInterface | ||
{ | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Unish; | ||
|
||
use Custom\Library\AutowireTestClasses\AutowireTest; | ||
use Custom\Library\AutowireTestClasses\AutowireTestService; | ||
use Custom\Library\AutowireTestClasses\AutowireTestServiceInterface; | ||
use Drupal\Core\DependencyInjection\ContainerBuilder; | ||
use Drush\Commands\AutowireTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @group base | ||
*/ | ||
class AutowireArgumentsTest extends TestCase | ||
{ | ||
/** | ||
* @covers \Drush\Commands\AutowireTrait::create | ||
*/ | ||
public function test() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('autowire_test', AutowireTestService::class); | ||
$container->setAlias(AutowireTestServiceInterface::class, new Reference('autowire_test')); | ||
$container->setParameter('foo', 'bar'); | ||
$container->compile(); | ||
|
||
$instance = AutowireTest::create($container); | ||
$this->assertInstanceOf(AutowireTest::class, $instance); | ||
} | ||
} |