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 811901b
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/fixtures/lib/AutowireTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Custom\Library;

use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

final class AutowireTest extends DrushCommands
{
use AutowireTrait;

public function __construct(
#[Autowire('a string as it is')]
public readonly string $argListStringValue,
#[Autowire(null, 'autowire_test')]
public readonly AutowireTestService $argListContainerService,
#[Autowire(null, null, null, null, 'foo')]
public readonly string $argListContainerParam,
#[Autowire(value: 'a string as it is')]
public readonly string $namedArgStringValue,
#[Autowire(service: 'autowire_test')]
public readonly AutowireTestService $namedArgContainerService,
#[Autowire(param: 'foo')]
public readonly string $namedArgContainerParam,
public readonly AutowireTestServiceInterface $noAutowireAttributeContainerService,
) {
parent::__construct();
}
}
11 changes: 11 additions & 0 deletions tests/fixtures/lib/AutowireTestService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Custom\Library;

final class AutowireTestService implements AutowireTestServiceInterface
{
public function greeting(): string
{
return 'Hello World!';
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/lib/AutowireTestServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Custom\Library;

interface AutowireTestServiceInterface
{
public function greeting(): string;
}
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'));
}
}
77 changes: 77 additions & 0 deletions tests/unit/AutowireArgumentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Unish;

use Custom\Library\AutowireTest;
use Custom\Library\AutowireTestService;
use Custom\Library\AutowireTestServiceInterface;
use Custom\Library\CreateExpectsDrushContainer;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drush\Commands\AutowireTrait;
use Drush\Log\Logger;
use League\Container\Container;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\DependencyInjection\Reference;

/**
* @covers \Drush\Commands\AutowireTrait::create
* @group base
*/
class AutowireArgumentsTest extends TestCase
{
public function testDrupalContainer(): void
{
$drupalContainer = new ContainerBuilder();
$drupalContainer->register('autowire_test', AutowireTestService::class);
$drupalContainer->setAlias(AutowireTestServiceInterface::class, new Reference('autowire_test'));
$drupalContainer->setParameter('foo', 'bar');
$drupalContainer->compile();
$drushContainer = new Container();
$drushContainer->delegate($drupalContainer);

$instance = AutowireTest::create($drupalContainer, $drushContainer);
$this->assertInstanceOf(AutowireTest::class, $instance);

$this->assertSame('a string as it is', $instance->argListStringValue);
$this->assertSame('Hello World!', $instance->argListContainerService->greeting());
$this->assertSame('bar', $instance->argListContainerParam);
$this->assertSame('a string as it is', $instance->namedArgStringValue);
$this->assertSame('Hello World!', $instance->namedArgContainerService->greeting());
$this->assertSame('bar', $instance->namedArgContainerParam);
$this->assertSame('Hello World!', $instance->noAutowireAttributeContainerService->greeting());
}

public function testDrushContainer(): void
{
$drushContainer = new Container();
$drushContainer->add('autowire_test', AutowireTestService::class);
$drushContainer->add(AutowireTestServiceInterface::class, AutowireTestService::class);

$instance = AutowireTest::create($drushContainer);
$this->assertInstanceOf(AutowireTest::class, $instance);

$this->assertSame('a string as it is', $instance->argListStringValue);
$this->assertSame('Hello World!', $instance->argListContainerService->greeting());
// Drush container has no Drupal container as delegate. It can't resolve the container param.
$this->assertSame('%foo%', $instance->argListContainerParam);
$this->assertSame('a string as it is', $instance->namedArgStringValue);
$this->assertSame('Hello World!', $instance->namedArgContainerService->greeting());
// Drush container has no Drupal container as delegate. It can't resolve the container param.
$this->assertSame('%foo%', $instance->namedArgContainerParam);
$this->assertSame('Hello World!', $instance->noAutowireAttributeContainerService->greeting());
}

public function testClassWithStaticCreateFactory(): void
{
$drushContainer = new Container();
$drushContainer->add('logger', Logger::class)->addArgument(new NullOutput());

$instance = CreateExpectsDrushContainer::create($drushContainer);
$this->assertInstanceOf(CreateExpectsDrushContainer::class, $instance);

$this->assertSame('a string as it is', $instance->string);
// If logger injection failed, this line will fail the test.
$instance->log->debug('debug string');
}
}

0 comments on commit 811901b

Please sign in to comment.