Skip to content

Commit

Permalink
Test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiu-cristea committed Nov 3, 2024
1 parent 2dcccbd commit 02471ee
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 0 deletions.
13 changes: 13 additions & 0 deletions sut/modules/unish/woot/src/AutowireTestService.php
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!';
}
}
7 changes: 7 additions & 0 deletions sut/modules/unish/woot/src/AutowireTestServiceInterface.php
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 {}
6 changes: 6 additions & 0 deletions sut/modules/unish/woot/woot.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ services:
class: Drupal\woot\EventSubscriber\PreRowDeleteTestSubscriber
tags:
- { name: event_subscriber }
woot.autowire_test:
class: Drupal\woot\AutowireTestService
Drupal\woot\AutowireTestServiceInterface: '@woot.autowire_test'

parameters:
foo: bar
23 changes: 23 additions & 0 deletions tests/fixtures/lib/CreateExpectsDrupalContainer.php
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'));
}
}
22 changes: 22 additions & 0 deletions tests/fixtures/lib/CreateExpectsDrushContainer.php
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 tests/fixtures/lib/Drush/Commands/AutowireTestCommands.php
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);
}
}
76 changes: 76 additions & 0 deletions tests/integration/AutowireArgumentsTest.php
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);
}
}

0 comments on commit 02471ee

Please sign in to comment.