Skip to content

Commit

Permalink
introduced PropagationAwareTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
overclokk committed Sep 24, 2023
1 parent 344a2be commit 9251654
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/PropagationAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace ItalyStrap\Event;

trait PropagationAwareTrait
{
private bool $stopPropagation = false;

public function isPropagationStopped(): bool
{
return $this->stopPropagation;
}

public function stopPropagation(): void
{
$this->stopPropagation = true;
}
}
9 changes: 3 additions & 6 deletions tests/integration/ImplementationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,9 @@ public function testDispatcherWithGlobalImplementationAndStoppableImplementation
$state = new \ItalyStrap\Event\GlobalState();

$event = new class implements StoppableEventInterface {
public bool $stopped = false;
use \ItalyStrap\Event\PropagationAwareTrait;

public string $name = '';
public function isPropagationStopped(): bool
{
return $this->stopped;
}
};

$eventName = \get_class($event);
Expand All @@ -355,7 +352,7 @@ public function isPropagationStopped(): bool
}, 10);

$listenerProvider->addListener($eventName, function (object $event) {
$event->stopped = true;
\method_exists($event, 'stopPropagation') and $event->stopPropagation();
}, 11);

$listenerProvider->addListener($eventName, function (object $event) {
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/PropagationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace ItalyStrap\Event\Tests\Integration;

use ItalyStrap\Event\PropagationAwareTrait;
use ItalyStrap\Tests\IntegrationTestCase;
use ItalyStrap\Tests\PropagationTestTrait;

class PropagationTest extends IntegrationTestCase
{
use PropagationTestTrait;

public function makeInstance(): object
{
return new class {
use PropagationAwareTrait;
};
}
}
21 changes: 21 additions & 0 deletions tests/src/PropagationTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace ItalyStrap\Tests;

trait PropagationTestTrait
{
public function testIsPropagationStopped(): void
{
$instance = $this->makeInstance();
$this->assertFalse($instance->isPropagationStopped());
}

public function testStopPropagation(): void
{
$instance = $this->makeInstance();
$instance->stopPropagation();
$this->assertTrue($instance->isPropagationStopped());
}
}
21 changes: 21 additions & 0 deletions tests/unit/PropagationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace ItalyStrap\Event\Tests\Unit;

use ItalyStrap\Event\PropagationAwareTrait;
use ItalyStrap\Tests\PropagationTestTrait;
use ItalyStrap\Tests\UnitTestCase;

class PropagationTest extends UnitTestCase
{
use PropagationTestTrait;

public function makeInstance(): object
{
return new class {
use PropagationAwareTrait;
};
}
}

0 comments on commit 9251654

Please sign in to comment.