Skip to content

Commit d944228

Browse files
EventTrait implemented.
1 parent b1e0eb6 commit d944228

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

src/Traits/EventTrait.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* This file is part of ScaleUpStack/EasyObject
5+
*
6+
* For the full copyright and license information, please view the README.md and LICENSE.md files that were distributed
7+
* with this source code.
8+
*
9+
* @copyright 2019 - present ScaleUpVentures GmbH, https://www.scaleupventures.com
10+
* @link https://github.com/scaleupstack/easy-object
11+
*/
12+
13+
namespace ScaleUpStack\EasyObject\Traits;
14+
15+
use ScaleUpStack\EasyObject\Magic\Dispatcher;
16+
use ScaleUpStack\EasyObject\Magic\NamedConstructor;
17+
use ScaleUpStack\EasyObject\Magic\VirtualGetter;
18+
19+
trait EventTrait
20+
{
21+
public function __call(string $method, array $parameters)
22+
{
23+
return Dispatcher::invoke(
24+
$this,
25+
$method,
26+
$parameters,
27+
[
28+
VirtualGetter::class,
29+
]
30+
);
31+
}
32+
33+
public static function __callStatic(string $method, array $parameters)
34+
{
35+
return Dispatcher::invokeStatically(
36+
self::class,
37+
$method,
38+
$parameters,
39+
[
40+
[
41+
NamedConstructor::class,
42+
['methodName' => 'occur'],
43+
],
44+
]
45+
);
46+
}
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* This file is part of ScaleUpStack/EasyObject
5+
*
6+
* For the full copyright and license information, please view the README.md and LICENSE.md files that were distributed
7+
* with this source code.
8+
*
9+
* @copyright 2019 - present ScaleUpVentures GmbH, https://www.scaleupventures.com
10+
* @link https://github.com/scaleupstack/easy-object
11+
*/
12+
13+
namespace ScaleUpStack\EasyObject\Tests\PhpUnit\Traits;
14+
15+
use ScaleUpStack\EasyObject\Tests\Resources\Traits\ForEventTraitTesting;
16+
use ScaleUpStack\EasyObject\Traits\EventTrait;
17+
use ScaleUpStack\EasyObject\Tests\Resources\TestCase;
18+
19+
/**
20+
* @coversDefaultClass \ScaleUpStack\EasyObject\Traits\EventTrait
21+
*/
22+
final class EventTraitTest extends TestCase
23+
{
24+
/**
25+
* @test
26+
* @covers ::__call()
27+
* @covers ::__callStatic()
28+
*/
29+
public function it_uses_magic_methods_for_named_constructor_and_virtual_getters()
30+
{
31+
// given some required parameter
32+
$someProperty = 'some value';
33+
34+
// when creating the event
35+
$event = ForEventTraitTesting::occur($someProperty);
36+
37+
// then the property is accessible via the virtual getter
38+
$this->assertSame($someProperty, $event->someProperty());
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* This file is part of ScaleUpStack/EasyObject
5+
*
6+
* For the full copyright and license information, please view the README.md and LICENSE.md files that were distributed
7+
* with this source code.
8+
*
9+
* @copyright 2019 - present ScaleUpVentures GmbH, https://www.scaleupventures.com
10+
* @link https://github.com/scaleupstack/easy-object
11+
*/
12+
13+
namespace ScaleUpStack\EasyObject\Tests\Resources\Traits;
14+
15+
use ScaleUpStack\EasyObject\Traits\EventTrait;
16+
17+
/**
18+
* @method static self occur(string $someProperty)
19+
* @method string someProperty()
20+
*/
21+
final class ForEventTraitTesting
22+
{
23+
use EventTrait;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $someProperty;
29+
}

0 commit comments

Comments
 (0)