1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\EventBus\Tests\Units\Middlewares\EventDispatcher; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\EventBus\Middlewares\EventDispatcher\EventDispatcherMiddleware; |
15
|
|
|
use Cubiche\Core\EventBus\Tests\Fixtures\Event\LoginUserEvent; |
16
|
|
|
use Cubiche\Core\EventBus\Tests\Fixtures\Event\LoginUserEventListener; |
17
|
|
|
use Cubiche\Core\EventBus\Tests\Units\TestCase; |
18
|
|
|
use Cubiche\Core\EventDispatcher\EventDispatcher; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* EventDispatcherMiddleware class. |
22
|
|
|
* |
23
|
|
|
* Generated by TestGenerator on 2016-04-11 at 15:18:25. |
24
|
|
|
*/ |
25
|
|
|
class EventDispatcherMiddlewareTests extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Test Handle method. |
29
|
|
|
*/ |
30
|
|
|
public function testHandle() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->given($dispatcher = new EventDispatcher()) |
34
|
|
|
->and($middleware = new EventDispatcherMiddleware($dispatcher)) |
35
|
|
|
->and($event = new LoginUserEvent('[email protected]')) |
36
|
|
|
->and($dispatcher->addListener($event->eventName(), array(new LoginUserEventListener(), 'loginUser'))) |
37
|
|
|
->and($dispatcher->addListener($event->eventName(), function (LoginUserEvent $event) { |
38
|
|
|
$this |
39
|
|
|
->string($event->email()) |
40
|
|
|
->isEqualTo('[email protected]') |
41
|
|
|
; |
42
|
|
|
|
43
|
|
|
$event->setEmail('[email protected]'); |
44
|
|
|
})) |
45
|
|
|
->and($callable = function (LoginUserEvent $event) { |
46
|
|
|
$event->setEmail('[email protected]'); |
47
|
|
|
}) |
48
|
|
|
->when($result = $middleware->handle($event, $callable)) |
49
|
|
|
->then() |
50
|
|
|
->string($event->email()) |
51
|
|
|
->isEqualTo('[email protected]') |
52
|
|
|
->exception(function () use ($middleware, $callable) { |
53
|
|
|
$middleware->handle(new \StdClass(), $callable); |
54
|
|
|
})->isInstanceOf(\InvalidArgumentException::class) |
55
|
|
|
; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Test dispatcher method. |
60
|
|
|
*/ |
61
|
|
|
public function testDispatcher() |
62
|
|
|
{ |
63
|
|
|
$this |
64
|
|
|
->given($dispatcher = new EventDispatcher()) |
65
|
|
|
->and($middleware = new EventDispatcherMiddleware($dispatcher)) |
66
|
|
|
->when($result = $middleware->dispatcher()) |
67
|
|
|
->then() |
68
|
|
|
->object($result) |
69
|
|
|
->isEqualTo($dispatcher) |
70
|
|
|
; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|