Created
November 29, 2023 11:04
-
-
Save joachim-n/56e37400a7e953c2d2015d342716f783 to your computer and use it in GitHub Desktop.
Mock discovery in plugin manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\Tests\list_predefined_options\Kernel; | |
use Drupal\Component\Plugin\Discovery\DiscoveryInterface; | |
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; | |
use Drupal\Core\DependencyInjection\ContainerBuilder; | |
use Drupal\Core\DependencyInjection\ServiceModifierInterface; | |
use Drupal\KernelTests\KernelTestBase; | |
use Drupal\list_predefined_options\ListOptionsManager; | |
/** | |
* Test case class TODO. | |
* | |
* @group list_predefined_options | |
*/ | |
class ListOptionsDefinitionTest extends KernelTestBase implements ServiceModifierInterface { | |
/** | |
* The modules to enable. | |
* | |
* @var array | |
*/ | |
protected static $modules = [ | |
'system', | |
'user', | |
'list_predefined_options', | |
]; | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function setUp(): void { | |
parent::setUp(); | |
} | |
public function alter(ContainerBuilder $container) { | |
// Change the class of the plugin manager service. | |
$original_service_definition = $container->getDefinition('plugin.manager.list_options'); | |
$original_service_definition->setClass(TestService::class); | |
} | |
/** | |
* Tests the TODO. | |
*/ | |
public function testMyTest() { | |
$manager = $this->container->get('plugin.manager.list_options'); | |
$discovery_mock = $this->prophesize(DiscoveryInterface::class); | |
$discovery_mock->getDefinitions()->willReturn([ | |
'bad_plugin' => [ | |
'label' => 'Bad plugin', | |
], | |
]); | |
$manager->setDiscovery($discovery_mock->reveal()); | |
$this->expectException(InvalidPluginDefinitionException::class); | |
$this->container->get('plugin.manager.list_options')->getDefinitions(); | |
} | |
} | |
/** | |
* Extend the plugin manager to allow us to set the discovery. | |
*/ | |
class TestService extends ListOptionsManager { | |
public function setDiscovery($discovery) { | |
$this->discovery = $discovery; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment