-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding tests for Invokable classes as event handlers
- Loading branch information
Luke Watts
committed
Jun 15, 2019
1 parent
3bc34ed
commit 6f71988
Showing
3 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |