Skip to content

Commit 811901b

Browse files
Test coverage
1 parent 2dcccbd commit 811901b

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

tests/fixtures/lib/AutowireTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Custom\Library;
4+
5+
use Drush\Commands\AutowireTrait;
6+
use Drush\Commands\DrushCommands;
7+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
8+
9+
final class AutowireTest extends DrushCommands
10+
{
11+
use AutowireTrait;
12+
13+
public function __construct(
14+
#[Autowire('a string as it is')]
15+
public readonly string $argListStringValue,
16+
#[Autowire(null, 'autowire_test')]
17+
public readonly AutowireTestService $argListContainerService,
18+
#[Autowire(null, null, null, null, 'foo')]
19+
public readonly string $argListContainerParam,
20+
#[Autowire(value: 'a string as it is')]
21+
public readonly string $namedArgStringValue,
22+
#[Autowire(service: 'autowire_test')]
23+
public readonly AutowireTestService $namedArgContainerService,
24+
#[Autowire(param: 'foo')]
25+
public readonly string $namedArgContainerParam,
26+
public readonly AutowireTestServiceInterface $noAutowireAttributeContainerService,
27+
) {
28+
parent::__construct();
29+
}
30+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Custom\Library;
4+
5+
final class AutowireTestService implements AutowireTestServiceInterface
6+
{
7+
public function greeting(): string
8+
{
9+
return 'Hello World!';
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Custom\Library;
4+
5+
interface AutowireTestServiceInterface
6+
{
7+
public function greeting(): string;
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Custom\Library;
4+
5+
use Drupal\Component\DependencyInjection\ContainerInterface;
6+
use Drupal\Core\Routing\RedirectDestinationInterface;
7+
use Drush\Commands\DrushCommands;
8+
use Psr\Log\LoggerInterface;
9+
10+
final class CreateFactoryExpectsDrupalContainer extends DrushCommands
11+
{
12+
public function __construct(
13+
public readonly string $string,
14+
public readonly RedirectDestinationInterface $redirectDestination,
15+
) {
16+
parent::__construct();
17+
}
18+
19+
public static function create(ContainerInterface $container): self
20+
{
21+
return new self('a string as it is', $container->get('redirect.destination'));
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Custom\Library;
4+
5+
use Drush\Commands\DrushCommands;
6+
use League\Container\DefinitionContainerInterface;
7+
use Psr\Log\LoggerInterface;
8+
9+
final class CreateExpectsDrushContainer extends DrushCommands
10+
{
11+
public function __construct(
12+
public readonly string $string,
13+
public readonly LoggerInterface $log,
14+
) {
15+
parent::__construct();
16+
}
17+
18+
public static function create(DefinitionContainerInterface $container): self
19+
{
20+
return new self('a string as it is', $container->get('logger'));
21+
}
22+
}

tests/unit/AutowireArgumentsTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)