Skip to content

Commit 0529870

Browse files
Test coverage
1 parent 2dcccbd commit 0529870

File tree

7 files changed

+211
-0
lines changed

7 files changed

+211
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\woot;
6+
7+
final class AutowireTestService implements AutowireTestServiceInterface
8+
{
9+
public function __toString(): string
10+
{
11+
return 'Hello World!';
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\woot;
6+
7+
interface AutowireTestServiceInterface extends \Stringable {}

sut/modules/unish/woot/woot.services.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ services:
1111
class: Drupal\woot\EventSubscriber\PreRowDeleteTestSubscriber
1212
tags:
1313
- { name: event_subscriber }
14+
woot.autowire_test:
15+
class: Drupal\woot\AutowireTestService
16+
Drupal\woot\AutowireTestServiceInterface: '@woot.autowire_test'
17+
18+
parameters:
19+
foo: bar
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+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Custom\Library\Drush\Commands;
6+
7+
use Drupal\woot\AutowireTestService;
8+
use Drupal\woot\AutowireTestServiceInterface;
9+
use Drush\Attributes as CLI;
10+
use Drush\Boot\DrupalBootLevels;
11+
use Drush\Commands\AutowireTrait;
12+
use Drush\Commands\DrushCommands;
13+
use League\Container\ContainerAwareInterface;
14+
use League\Container\ContainerAwareTrait;
15+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
16+
17+
final class AutowireTestCommands extends DrushCommands
18+
{
19+
use AutowireTrait;
20+
21+
public function __construct(
22+
#[Autowire('a string as it is')]
23+
private readonly string $argListStringValue,
24+
#[Autowire(null, 'woot.autowire_test')]
25+
private readonly AutowireTestService $argListContainerService,
26+
#[Autowire(null, null, null, null, 'foo')]
27+
private readonly string $argListContainerParam,
28+
#[Autowire(value: 'a string as it is')]
29+
private readonly string $namedArgStringValue,
30+
#[Autowire(service: 'woot.autowire_test')]
31+
private readonly AutowireTestService $namedArgContainerService,
32+
#[Autowire(param: 'foo')]
33+
private readonly string $namedArgContainerParam,
34+
private readonly AutowireTestServiceInterface $noAutowireAttributeContainerService,
35+
) {
36+
parent::__construct();
37+
}
38+
39+
#[CLI\Command(name: 'test_autowire:drupal-container')]
40+
#[CLI\Bootstrap(level: DrupalBootLevels::FULL)]
41+
#[CLI\Help(hidden: true)]
42+
public function drupal(): string
43+
{
44+
$values = [];
45+
$constructor = new \ReflectionMethod($this, '__construct');
46+
foreach ($constructor->getParameters() as $param) {
47+
$values[] = (string) $this->{$param->getName()};
48+
}
49+
return implode("\n", $values);
50+
}
51+
52+
#[CLI\Command(name: 'test_autowire:drush-container')]
53+
#[CLI\Bootstrap(level: DrupalBootLevels::NONE)]
54+
#[CLI\Help(hidden: true)]
55+
public function drush(): string
56+
{
57+
$values = [];
58+
$constructor = new \ReflectionMethod($this, '__construct');
59+
foreach ($constructor->getParameters() as $param) {
60+
$values[] = (string) $this->{$param->getName()};
61+
}
62+
return implode("\n", $values);
63+
}
64+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Unish;
6+
7+
use Custom\Library\AutowireTestService;
8+
use Custom\Library\AutowireTestServiceInterface;
9+
use Drush\Commands\pm\PmCommands;
10+
use Drush\Drush;
11+
12+
/**
13+
* @covers \Drush\Commands\AutowireTrait::create
14+
* @group base
15+
*/
16+
class AutowireArgumentsTest extends UnishIntegrationTestCase
17+
{
18+
public function testWithDrupalContainer(): void
19+
{
20+
$this->drush(PmCommands::INSTALL, ['woot']);
21+
22+
$expected = [
23+
// Autowire('a string as it is')
24+
'a string as it is',
25+
// Autowire(null, 'woot.autowire_test')
26+
'Hello World!',
27+
// Autowire(null, null, null, null, 'foo')
28+
'bar',
29+
// Autowire(value: 'a string as it is')
30+
'a string as it is',
31+
// Autowire(service: 'woot.autowire_test')
32+
'Hello World!',
33+
// Autowire(param: 'foo')
34+
'bar',
35+
// Autowire by service full qualified interface name
36+
// @see \Drupal\woot\AutowireTestServiceInterface
37+
'Hello World!',
38+
];
39+
40+
$this->drush('test_autowire:drupal-container');
41+
$this->assertSame(implode("\n", $expected), $this->getOutput());
42+
43+
$this->drush(PmCommands::UNINSTALL, ['woot']);
44+
}
45+
46+
// @todo This test will fail because I coudn't find a way to add a new
47+
// service to Drush container.
48+
public function testWithDrushContainer(): void
49+
{
50+
$drushContainer = Drush::getContainer();
51+
$drushContainer->add('woot.autowire_test', AutowireTestService::class);
52+
$drushContainer->add(AutowireTestServiceInterface::class, AutowireTestService::class);
53+
Drush::setContainer($drushContainer);
54+
55+
$expected = [
56+
// Autowire('a string as it is')
57+
'a string as it is',
58+
// Autowire(null, 'woot.autowire_test')
59+
'Hello World!',
60+
// Autowire(null, null, null, null, 'foo')
61+
'bar',
62+
// Autowire(value: 'a string as it is')
63+
'a string as it is',
64+
// Autowire(service: 'woot.autowire_test')
65+
'Hello World!',
66+
// Autowire(param: 'foo')
67+
'bar',
68+
// Autowire by service full qualified interface name
69+
// @see \Drupal\woot\AutowireTestServiceInterface
70+
'Hello World!',
71+
];
72+
73+
$this->drush('test_autowire:drush-container');
74+
$this->assertSame(implode("\n", $expected), $output);
75+
}
76+
}

0 commit comments

Comments
 (0)