Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Event/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

namespace PhpSchool\PhpWorkshop\Event;

use DI\Definition\Helper\CreateDefinitionHelper;

use function DI\create;

if (!function_exists('PhpSchool\PhpWorkshop\Event\containerListener')) {

function containerListener(string $service, string $method = '__invoke'): callable
/**
* A container definition rather than a closure, so containers that use it can be compiled.
*/
function containerListener(string $service, string $method = '__invoke'): CreateDefinitionHelper
{
return fn() => new ContainerListenerHelper($service, $method);
return create(ContainerListenerHelper::class)->constructor($service, $method);
}
}
23 changes: 23 additions & 0 deletions test/ContainerCompilationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshopTest;

use DI\ContainerBuilder;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;

class ContainerCompilationTest extends BaseTest
{
public function testConfigCanBeCompiled(): void
{
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions(['basePath' => __DIR__ . '/..']);
$containerBuilder->addDefinitions(require __DIR__ . '/../app/config.php');
$containerBuilder->enableCompilation($this->getTemporaryDirectory(), 'CompiledWorkshopContainer');

$container = $containerBuilder->build();

$this->assertInstanceOf(EventDispatcher::class, $container->get(EventDispatcher::class));
}
}
11 changes: 5 additions & 6 deletions test/Factory/EventDispatcherFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
use PhpSchool\PhpWorkshop\Event\Event;
use Psr\Container\ContainerInterface;
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
use PhpSchool\PhpWorkshop\Event\ContainerListenerHelper;
use PhpSchool\PhpWorkshop\Factory\EventDispatcherFactory;
use PhpSchool\PhpWorkshop\ResultAggregator;
use PhpSchool\PhpWorkshopTest\Asset\CallableStub;
use PHPUnit\Framework\TestCase;

use function PhpSchool\PhpWorkshop\Event\containerListener;

class EventDispatcherFactoryTest extends TestCase
{
public function testCreateWithNoConfig(): void
Expand Down Expand Up @@ -113,7 +112,7 @@ public function testExceptionIsThrownIfEventsListenerContainerEntryNotExist(): v
{
$eventConfig = [
'my-group' => [
'someEvent' => [containerListener('nonExistingContainerEntry')()],
'someEvent' => [new ContainerListenerHelper('nonExistingContainerEntry')],
],
];

Expand Down Expand Up @@ -172,7 +171,7 @@ public function testListenerFromContainerIsNotFetchedDuringAttaching(): void
{
$eventConfig = [
'my-group' => [
'someEvent' => [containerListener('containerEntry')()],
'someEvent' => [new ContainerListenerHelper('containerEntry')],
],
];

Expand All @@ -199,7 +198,7 @@ public function testListenerFromContainerIsFetchedWhenEventDispatched(): void
{
$eventConfig = [
'my-group' => [
'someEvent' => [containerListener('containerEntry')()],
'someEvent' => [new ContainerListenerHelper('containerEntry')],
],
];

Expand Down Expand Up @@ -232,7 +231,7 @@ public function testExceptionIsThrownIfMethodDoesNotExistOnContainerEntry(): voi
{
$eventConfig = [
'my-group' => [
'someEvent' => [containerListener('containerEntry', 'notHere')()],
'someEvent' => [new ContainerListenerHelper('containerEntry', 'notHere')],
],
];

Expand Down
Loading