Skip to content

Commit

Permalink
Adding tests for Invokable classes as event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Watts committed Jun 15, 2019
1 parent 3bc34ed commit 6f71988
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
17 changes: 17 additions & 0 deletions tests/Assets/InvokableEventHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Affinity4\Magic\Tests\Assets;

class InvokableEventHandler
{
private $number = 0;

public function __construct(int $number)
{
$this->number = $number;
}

public function __invoke(string $save)
{
echo "$save {$this->number}\n";
}
}
5 changes: 2 additions & 3 deletions tests/MagicEventCallbacksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ public function testAddingEventHandlersAndTriggeringEvent()

ob_start();
$MagicEvent->onSave('Save');
$output = ob_get_clean();
$output = ob_get_contents();
ob_end_clean();

$this->assertEquals("Save 1\nSave 2\nSave 3\n", $output);
}


}
45 changes: 45 additions & 0 deletions tests/MagicEventInvokableClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace Affinity4\Magic\Tests;

use PHPUnit\Framework\TestCase;
use Affinity4\Magic\Tests\Assert\Assertions;
use Affinity4\Magic\Tests\Assets\MagicEvent;
use Affinity4\Magic\Tests\Assets\InvokableEventHandler;
use Affinity4\Magic\Magic;

/**
* @group MagicEventTest
* @group MagicEventInvokableClassTest
*/
class MagicEventInvokableClassTest extends TestCase
{
use Assertions;

public function testClassHasMagicTrait()
{
$ReflectionClass = new \ReflectionClass(MagicEvent::class);
$traits = $ReflectionClass->getTraits();

$this->assertArrayHasKey(Magic::class, $traits);
}

public function testOnSavePropertyExists()
{
$this->assertPropertyExists(MagicEvent::class, 'onSave');
}

public function testAddingEventHandlersAndTriggeringEvent()
{
$MagicEvent = new MagicEvent;
$MagicEvent->onSave[] = new InvokableEventHandler(1);
$MagicEvent->onSave[] = new InvokableEventHandler(2);
$MagicEvent->onSave[] = new InvokableEventHandler(3);

ob_start();
$MagicEvent->onSave('Save');
$output = ob_get_contents();
ob_end_clean();

$this->assertEquals("Save 1\nSave 2\nSave 3\n", $output);
}
}

0 comments on commit 6f71988

Please sign in to comment.