EventTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 80
dl 0
loc 169
rs 10
c 2
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testIncorrectEventObjectFromNoneExistingClass() 0 14 1
A clearLog() 0 6 2
A testCreateEventInstanceFromIncorrectObject() 0 14 1
A createLogObject() 0 9 1
A testCallEvent() 0 33 1
A testCallEventWithLog() 0 28 1
A testCreateEventObject() 0 24 1
A testIncorrectEventObject() 0 13 1
1
<?php
2
3
namespace Test;
4
5
use PHPUnit\Framework\TestCase;
6
use BlueEvent\Event\Base\EventDispatcher;
7
use BlueRegister\Events\Event;
8
use BlueRegister\Events\RegisterEvent;
9
use BlueRegister\Log;
10
use SimpleLog\Log as SimpleLog;
11
12
class EventTest extends TestCase
13
{
14
    /**
15
     * name of test event log file
16
     */
17
    const REGISTER_LOG_NAME = '/debug.log';
18
19
    public function testIncorrectEventObject()
20
    {
21
        $this->expectException(\LogicException::class);
22
23
        $logPath = __DIR__ . '/log';
24
        $this->clearLog($logPath);
25
26
        new Event(
27
            [
28
                'event_object' => TestClass\SimpleClass::class,
29
                'event_config' => [],
30
            ],
31
            $this->createLogObject()
32
        );
33
    }
34
35
    public function testIncorrectEventObjectFromNoneExistingClass()
36
    {
37
        $this->expectExceptionMessage("Class don't exists: SomeClass");
38
        $this->expectException(\LogicException::class);
39
40
        $logPath = __DIR__ . '/log';
41
        $this->clearLog($logPath);
42
43
        new Event(
44
            [
45
                'event_object' => 'SomeClass',
46
                'event_config' => [],
47
            ],
48
            $this->createLogObject()
49
        );
50
    }
51
52
    public function testCreateEventInstanceFromIncorrectObject()
53
    {
54
        $this->expectExceptionMessage("Cannot create Event instance: Test\TestClass\SimpleClass");
55
        $this->expectException(\LogicException::class);
56
57
        $logPath = __DIR__ . '/log';
58
        $this->clearLog($logPath);
59
60
        new Event(
61
            [
62
                'event_object' => new \Test\TestClass\SimpleClass,
63
                'event_config' => [],
64
            ],
65
            $this->createLogObject()
66
        );
67
    }
68
69
    public function testCreateEventObject()
70
    {
71
        $logPath = __DIR__ . '/log';
72
        $this->clearLog($logPath);
73
        $log = $this->createLogObject();
74
75
        $event1 = new Event(
76
            [
77
                'event_object' => new EventDispatcher(),
78
                'event_config' => [],
79
            ],
80
            $log
81
        );
82
        $event2 = new Event(
83
            [
84
                'event_object' => EventDispatcher::class,
85
                'event_config' => [],
86
            ],
87
            $log
88
        );
89
90
        $this->assertInstanceOf(Event::class, $event1);
91
        $this->assertInstanceOf(Event::class, $event2);
92
        $this->clearLog($logPath);
93
    }
94
95
    public function testCallEvent()
96
    {
97
        $logPath = __DIR__ . '/log';
98
        $testData = [];
99
        $eventConfig = [
100
            'register_before_create' => [
101
                'object' => RegisterEvent::class,
102
                'listeners' => [
103
                    function ($event) use (&$testData) {
104
                        /** @var $event \BlueRegister\Events\RegisterEvent */
105
                        $testData['register_before_create'] = $event->getEventParameters();
106
                    }
107
                ]
108
            ]
109
        ];
110
111
        $log = $this->createLogObject();
112
113
        $event = new Event(
114
            [
115
                'event_object' => new EventDispatcher(['events' => $eventConfig]),
116
                'event_config' => [],
117
            ],
118
            $log
119
        );
120
121
        $this->assertEmpty($testData);
122
        $event->callEvent('register_before_create', []);
123
124
        $this->assertNotEmpty($testData);
125
        $this->assertInstanceOf(EventDispatcher::class, $testData['register_before_create'][0]['event_object']);
126
127
        $this->clearLog($logPath);
128
    }
129
130
    public function testCallEventWithLog()
131
    {
132
        $logPath = __DIR__ . '/log';
133
        $eventConfig = [
134
            'register_before_create' => [
135
                'object' => RegisterEvent::class,
136
                'listeners' => []
137
            ]
138
        ];
139
140
        $this->clearLog($logPath);
141
142
        $log = $this->createLogObject();
143
144
        $event = new Event(
145
            [
146
                'event_object' => new EventDispatcher(['events' => $eventConfig]),
147
                'event_config' => [],
148
            ],
149
            $log
150
        );
151
152
        $this->assertFileDoesNotExist($logPath . self::REGISTER_LOG_NAME);
153
154
        $event->callEvent('register_before_create', []);
155
156
        $this->assertFileExists($logPath . self::REGISTER_LOG_NAME);
157
        $this->clearLog($logPath);
158
    }
159
160
    protected function clearLog($logPath)
161
    {
162
        $logFile = $logPath . self::REGISTER_LOG_NAME;
163
164
        if (file_exists($logFile)) {
165
            unlink($logFile);
166
        }
167
    }
168
169
    /**
170
     * @return Log
171
     */
172
    protected function createLogObject()
173
    {
174
        $logPath = __DIR__ . '/log';
175
176
        $simpleLog = new SimpleLog;
177
        $simpleLog->setOption('log_path', $logPath)
178
            ->setOption('level', 'debug');
179
180
        return new Log($simpleLog);
181
    }
182
}
183