Passed
Branch develop (bd4658)
by Michał
02:31
created

testIncorrectEventObjectFromNoneExistingClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
    /**
20
     * @expectedException \LogicException
21
     */
22
    public function testIncorrectEventObject()
23
    {
24
        new Event(
25
            [
26
                'event_object' => TestClass\SimpleClass::class,
27
                'event_config' => [],
28
            ],
29
            new Log(new SimpleLog)
30
        );
31
    }
32
33
    /**
34
     * @expectedException \LogicException
35
     * @expectedExceptionMessage Class don't exists: SomeClass
36
     */
37
    public function testIncorrectEventObjectFromNoneExistingClass()
38
    {
39
        new Event(
40
            [
41
                'event_object' => 'SomeClass',
42
                'event_config' => [],
43
            ],
44
            new Log(new SimpleLog)
45
        );
46
    }
47
48
    /**
49
     * @expectedException \LogicException
50
     * @expectedExceptionMessage Cannot create Event instance: Test\TestClass\SimpleClass
51
     */
52
    public function testCreateEventInstanceFromIncorrectObject()
53
    {
54
        new Event(
55
            [
56
                'event_object' => new \Test\TestClass\SimpleClass,
57
                'event_config' => [],
58
            ],
59
            new Log(new SimpleLog)
60
        );
61
    }
62
63
    //test create event object
64
    public function testCreateEventObject()
65
    {
66
        $event1 = new Event(
67
            [
68
                'event_object' => new EventDispatcher(),
69
                'event_config' => [],
70
            ],
71
            new Log(new SimpleLog)
72
        );
73
        $event2 = new Event(
74
            [
75
                'event_object' => EventDispatcher::class,
76
                'event_config' => [],
77
            ],
78
            new Log(new SimpleLog)
79
        );
80
81
        $this->assertInstanceOf(Event::class, $event1);
82
        $this->assertInstanceOf(Event::class, $event2);
83
    }
84
    
85
86
87
    public function testCallEvent()
88
    {
89
        $testData = [];
90
        $eventConfig = [
91
            'register_before_create' => [
92
                'object' => RegisterEvent::class,
93
                'listeners' => [
94
                    function ($event) use (&$testData) {
95
                        /** @var $event \BlueRegister\Events\RegisterEvent */
96
                        $testData['register_before_create'] = $event->getEventParameters();
97
                    }
98
                ]
99
            ]
100
        ];
101
102
        $event = new Event(
103
            [
104
                'event_object' => new EventDispatcher(['events' => $eventConfig]),
105
                'event_config' => [],
106
            ],
107
            new Log(new SimpleLog)
108
        );
109
110
        $this->assertEmpty($testData);
111
        $event->callEvent('register_before_create', []);
112
113
        $this->assertNotEmpty($testData);
114
        $this->assertInstanceOf(EventDispatcher::class, $testData['register_before_create'][0]['event_object']);
115
    }
116
117
    public function testCallEventWithLog()
118
    {
119
        $logPath = __DIR__ . '/log';
120
        $eventConfig = [
121
            'register_before_create' => [
122
                'object' => RegisterEvent::class,
123
                'listeners' => []
124
            ]
125
        ];
126
127
        $this->clearLog($logPath);
128
129
        $simpleLog = new SimpleLog;
130
        $simpleLog->setOption('log_path', $logPath)
131
            ->setOption('level', 'debug');
132
133
        $log = new Log($simpleLog);
134
135
        $event = new Event(
136
            [
137
                'event_object' => new EventDispatcher(['events' => $eventConfig]),
138
                'event_config' => [],
139
            ],
140
            $log
141
        );
142
143
        $this->assertFileNotExists($logPath . self::REGISTER_LOG_NAME);
144
145
        $event->callEvent('register_before_create', []);
146
147
        $this->assertFileExists($logPath . self::REGISTER_LOG_NAME);
148
        $this->clearLog($logPath);
149
    }
150
151
    protected function clearLog($logPath)
152
    {
153
        $logFile = $logPath . self::REGISTER_LOG_NAME;
154
155
        if (file_exists($logFile)) {
156
            unlink($logFile);
157
        }
158
    }
159
}
160