1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
<?php
declare(strict_types=1);
namespace LaminasTest\EventManager;
use Laminas\EventManager\EventManagerInterface;
use LaminasTest\EventManager\TestAsset\MockListenerAggregateTrait;
use PHPUnit\Framework\TestCase;
use function in_array;
class ListenerAggregateTraitTest extends TestCase
{
public function testDetachRemovesAttachedListeners(): void
{
$aggregate = new MockListenerAggregateTrait();
$events = $this->createMock(EventManagerInterface::class);
$events->expects(self::atLeast(2))
->method('attach')
->with(
self::callback(static function (string $value): bool {
self::assertTrue(in_array($value, ['foo.bar', 'foo.baz'], true));
return true;
}),
self::callback(static function (array $value) use ($aggregate): bool {
self::assertSame($aggregate, $value[0] ?? null);
self::assertSame('doFoo', $value[1] ?? null);
return true;
}),
)->willReturnArgument(1);
$events->expects(self::exactly(2))
->method('detach')
->with([$aggregate, 'doFoo']);
$aggregate->attach($events);
$listeners = $aggregate->getCallbacks();
self::assertCount(2, $listeners);
foreach ($listeners as $listener) {
self::assertSame([$aggregate, 'doFoo'], $listener);
}
$aggregate->detach($events);
self::assertSame([], $aggregate->getCallbacks());
}
}
|