Skip to content

Commit b8c56c6

Browse files
CommandTrait added.
1 parent 67e0d1b commit b8c56c6

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

src/Traits/CommandTrait.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 CommandTrait
20+
{
21+
/**
22+
* @codeCoverageIgnore
23+
*/
24+
private function __construct()
25+
{
26+
}
27+
28+
public function __call(string $method, array $parameters)
29+
{
30+
return Dispatcher::invoke(
31+
$this,
32+
$method,
33+
$parameters,
34+
[
35+
VirtualGetter::class,
36+
]
37+
);
38+
}
39+
40+
public static function __callStatic(string $method, array $parameters)
41+
{
42+
return Dispatcher::invokeStatically(
43+
self::class,
44+
$method,
45+
$parameters,
46+
[
47+
[
48+
NamedConstructor::class,
49+
['methodName' => 'instruct'],
50+
],
51+
]
52+
);
53+
}
54+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\TestCase;
16+
use ScaleUpStack\EasyObject\Tests\Resources\Traits\ForCommandTraitTesting;
17+
use ScaleUpStack\Reflection\Reflection;
18+
19+
/**
20+
* @coversDefaultClass \ScaleUpStack\EasyObject\Traits\CommandTrait
21+
*/
22+
final class CommandTraitTest 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+
$command = ForCommandTraitTesting::instruct($someProperty);
36+
37+
// then the property is accessible via the virtual getter
38+
$this->assertSame($someProperty, $command->someProperty());
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function it_cannot_be_constructed_via_new()
45+
{
46+
// the constructor must not be public
47+
$this->assertFalse(
48+
Reflection::methodOfClass(ForCommandTraitTesting::class, '__construct')->isPublic(),
49+
'__construct() must not be public'
50+
);
51+
}
52+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\CommandTrait;
16+
17+
/**
18+
* @method static self instruct(string $someProperty)
19+
* @method string someProperty()
20+
*/
21+
final class ForCommandTraitTesting
22+
{
23+
use CommandTrait;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $someProperty;
29+
30+
}

0 commit comments

Comments
 (0)