PreprocessEventPassTest.php in Hook Event Dispatcher 8
File
tests/src/Unit/Preprocess/PreprocessEventPassTest.php
View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\Preprocess;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\hook_event_dispatcher\Event\Preprocess\EckEntityPreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\HtmlPreprocessEvent;
use Drupal\hook_event_dispatcher\Service\PreprocessEventPass;
use Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\FakePreprocessEvent;
use Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\FakePreprocessEventFactory;
use Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\SpyEventDispatcher;
use Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\YamlDefinitionsLoader;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class PreprocessEventPassTest extends UnitTestCase {
private $builder;
private $pass;
public function setUp() {
$this->builder = new ContainerBuilder();
$locator = new FileLocator([
dirname(dirname(dirname(dirname(__DIR__)))),
]);
$loader = new YamlFileLoader($this->builder, $locator);
$loader
->load('hook_event_dispatcher.services.yml');
$this->builder
->set('event_dispatcher', new SpyEventDispatcher());
$this->pass = new PreprocessEventPass();
}
public function testBuilderHasAllServices() {
$this->pass
->process($this->builder);
$this->builder
->compile();
$services = YamlDefinitionsLoader::getInstance()
->getServices();
foreach (array_keys($services) as $id) {
self::assertTrue($this->builder
->has($id));
}
}
public function testOverwritingDefaultFactory() {
$fakeEckEntityFactory = new Definition(FakePreprocessEventFactory::class, [
EckEntityPreprocessEvent::getHook(),
]);
$fakeEckEntityFactory
->addTag('preprocess_event_factory');
$this->builder
->setDefinition('preprocess_event.fake_factory.eck_entity', $fakeEckEntityFactory);
$fakeHtmlFactory = new Definition(FakePreprocessEventFactory::class, [
HtmlPreprocessEvent::getHook(),
]);
$fakeHtmlFactory
->addTag('preprocess_event_factory');
$this->builder
->setDefinition('preprocess_event.fake_factory.html', $fakeHtmlFactory);
$this->pass
->process($this->builder);
$this->builder
->compile();
$mapper = $this->builder
->get('preprocess_event.factory_mapper');
$variables = [];
$eckMappedFactory = $mapper
->getFactory(EckEntityPreprocessEvent::getHook());
self::assertInstanceOf(FakePreprocessEventFactory::class, $eckMappedFactory);
self::assertEquals(EckEntityPreprocessEvent::getHook(), $eckMappedFactory
->getEventHook());
self::assertInstanceOf(FakePreprocessEvent::class, $eckMappedFactory
->createEvent($variables));
$htmlMappedFactory = $mapper
->getFactory(HtmlPreprocessEvent::getHook());
self::assertInstanceOf(FakePreprocessEventFactory::class, $htmlMappedFactory);
self::assertEquals(HtmlPreprocessEvent::getHook(), $htmlMappedFactory
->getEventHook());
self::assertInstanceOf(FakePreprocessEvent::class, $htmlMappedFactory
->createEvent($variables));
}
}