Commit e0f7d183 authored by catch's avatar catch
Browse files

task: #2620330 Move...

task: #2620330 Move ContainerDerivativeDiscoveryDecoratorTest::testGetDefinitions() to DerivativeDiscoveryDecoratorTest

By: mile23
By: buelto
By: phenaproxima
(cherry picked from commit 3ea00aac)
parent 4a97f88d
Loading
Loading
Loading
Loading
Loading
+0 −59
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\Core\Plugin\Discovery;

use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator.
 */
#[CoversClass(ContainerDerivativeDiscoveryDecorator::class)]
#[Group('Plugin')]
class ContainerDerivativeDiscoveryDecoratorTest extends UnitTestCase {

  /**
   * Tests get definitions.
   *
   * @legacy-covers ::getDefinitions
   */
  public function testGetDefinitions(): void {
    $example_service = $this->createMock('Symfony\Contracts\EventDispatcher\EventDispatcherInterface');
    $example_container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
      ->onlyMethods(['get'])
      ->getMock();
    $example_container->expects($this->once())
      ->method('get')
      ->with($this->equalTo('example_service'))
      ->willReturn($example_service);

    \Drupal::setContainer($example_container);

    $definitions = [];
    $definitions['container_aware_discovery'] = [
      'id' => 'container_aware_discovery',
      'deriver' => '\Drupal\Tests\Core\Plugin\Discovery\TestContainerDerivativeDiscovery',
    ];
    $definitions['non_container_aware_discovery'] = [
      'id' => 'non_container_aware_discovery',
      'deriver' => '\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery',
    ];

    $discovery_main = $this->createMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
    $discovery_main->expects($this->any())
      ->method('getDefinitions')
      ->willReturn($definitions);

    $discovery = new ContainerDerivativeDiscoveryDecorator($discovery_main);
    $definitions = $discovery->getDefinitions();

    // Ensure that both the instances from container and non-container test
    // derivatives got added.
    $this->assertCount(4, $definitions);
  }

}
+41 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\Component\Plugin\Definition\DerivablePluginDefinitionInterface;
use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator;
use Drupal\Component\Plugin\Exception\InvalidDeriverException;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
@@ -62,6 +63,46 @@ public function testGetDerivativeFetcher(): void {
    $this->assertEquals('\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery', $definitions['non_container_aware_discovery:test_discovery_1']['deriver']);
  }

  /**
   * Tests get definitions.
   *
   * @legacy-covers ::getDefinitions
   */
  public function testGetDefinitions(): void {
    $example_service = $this->createMock('Symfony\Contracts\EventDispatcher\EventDispatcherInterface');
    $example_container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
      ->onlyMethods(['get'])
      ->getMock();
    $example_container->expects($this->once())
      ->method('get')
      ->with($this->equalTo('example_service'))
      ->willReturn($example_service);

    \Drupal::setContainer($example_container);

    $definitions = [];
    $definitions['container_aware_discovery'] = [
      'id' => 'container_aware_discovery',
      'deriver' => '\Drupal\Tests\Core\Plugin\Discovery\TestContainerDerivativeDiscovery',
    ];
    $definitions['non_container_aware_discovery'] = [
      'id' => 'non_container_aware_discovery',
      'deriver' => '\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery',
    ];

    $discovery_main = $this->createMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
    $discovery_main->expects($this->any())
      ->method('getDefinitions')
      ->willReturn($definitions);

    $discovery = new ContainerDerivativeDiscoveryDecorator($discovery_main);
    $definitions = $discovery->getDefinitions();

    // Ensure that both the instances from container and non-container test
    // derivatives got added.
    $this->assertCount(4, $definitions);
  }

  /**
   * Tests the getDerivativeFetcher method with objects instead of arrays.
   */