-
Notifications
You must be signed in to change notification settings - Fork 13
/
EventsTrackerTest.php
242 lines (201 loc) · 9.21 KB
/
EventsTrackerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace JKocik\Laravel\Profiler\Tests\Unit\Trackers;
use Exception;
use Illuminate\Events\Dispatcher;
use JKocik\Laravel\Profiler\Tests\TestCase;
use JKocik\Laravel\Profiler\Events\Tracking;
use JKocik\Laravel\Profiler\Events\Terminating;
use JKocik\Laravel\Profiler\Events\ProfilerBound;
use JKocik\Laravel\Profiler\Events\ResetTrackers;
use JKocik\Laravel\Profiler\Trackers\EventsTracker;
use JKocik\Laravel\Profiler\Events\ExceptionHandling;
use JKocik\Laravel\Profiler\Tests\Support\Fixtures\DummyClassA;
use JKocik\Laravel\Profiler\Tests\Support\Fixtures\DummyClassB;
use JKocik\Laravel\Profiler\Tests\Support\Fixtures\DummyEventA;
use JKocik\Laravel\Profiler\Tests\Support\Fixtures\DummyEventB;
use JKocik\Laravel\Profiler\Tests\Support\Fixtures\DummyListenerA;
class EventsTrackerTest extends TestCase
{
/** @test */
function has_fired_events()
{
$tracker = $this->app->make(EventsTracker::class);
$dispatcher = $this->app->make(Dispatcher::class);
$this->tapLaravelVersionTill(5.3, function () use ($dispatcher) {
$dispatcher->fire('testing: tracker', [new \stdClass()]);
});
$this->tapLaravelVersionFrom(5.4, function () use ($dispatcher) {
$dispatcher->dispatch('testing: tracker', [new \stdClass()]);
});
event(new DummyEventA());
$tracker->terminate();
$events = $tracker->data()->get('events');
$this->assertNotNull($events);
$this->assertTrue($events->contains('name', 'testing: tracker'));
$this->assertTrue($events->contains('name', DummyEventA::class));
$this->assertEquals(2, $tracker->meta()->get('events_count'));
}
/** @test */
function has_data_of_fired_events()
{
$this->app->make('config')->set('profiler.data.events', true);
$this->app->make('config')->set('profiler.group.events', false);
$tracker = $this->app->make(EventsTracker::class);
$user = $this->user(['email' => 'a@example.com']);
$usersA = collect([$this->user(['email' => 'b@example.com']), $this->user(['email' => 'c@example.com'])]);
$usersB = [$this->user(['email' => 'd@example.com']), $this->user(['email' => collect(['email' => 'e@example.com'])])];
$dummyClasses = [new DummyClassA(), new DummyClassB()];
$dataA = ['a' => 1, 'c' => 2];
$dataB = 'c';
event(new DummyEventB($user, $usersA, $usersB, $dummyClasses, $dataA, $dataB));
$tracker->terminate();
$events = $tracker->data()->get('events');
$eventB = $events->where('name', DummyEventB::class)->first();
$this->assertEquals(['email' => 'a@example.com'], $eventB['data']['user']);
$this->assertEquals([
0 => ['email' => 'b@example.com'],
1 => ['email' => 'c@example.com'],
], $eventB['data']['usersA']);
$this->assertEquals([
0 => ['email' => 'd@example.com'],
1 => ['email' => ['email' => 'e@example.com']],
], $eventB['data']['usersB']);
$this->assertEquals([DummyClassA::class, DummyClassB::class], $eventB['data']['dummyClasses']);
$this->assertEquals(['a' => 1, 'c' => 2], $eventB['data']['dataA']);
$this->assertEquals('c', $eventB['data']['dataB']);
}
/** @test */
function has_not_data_of_fired_events_if_data_tracking_is_disabled_in_config()
{
$this->app->make('config')->set('profiler.group.events', false);
$tracker = $this->app->make(EventsTracker::class);
$user = $this->user(['email' => 'a@example.com']);
$usersA = collect([$this->user(['email' => 'b@example.com']), $this->user(['email' => 'c@example.com'])]);
$usersB = [$this->user(['email' => 'd@example.com']), $this->user(['email' => 'e@example.com'])];
$dummyClasses = [new DummyClassA(), new DummyClassB()];
$dataA = ['a' => 1, 'c' => 2];
$dataB = 'c';
event(new DummyEventB($user, $usersA, $usersB, $dummyClasses, $dataA, $dataB));
$tracker->terminate();
$events = $tracker->data()->get('events');
$eventB = $events->where('name', DummyEventB::class)->first();
$this->assertArrayNotHasKey('data', $eventB);
}
/** @test */
function can_group_events_with_the_same_name()
{
$this->app->make('config')->set('profiler.data.events', true);
$tracker = $this->app->make(EventsTracker::class);
$dispatcher = $this->app->make(Dispatcher::class);
$this->tapLaravelVersionTill(5.3, function () use ($dispatcher) {
$dispatcher->fire('testing: eventA', [new \stdClass()]);
$dispatcher->fire('testing: eventB', [new \stdClass()]);
$dispatcher->fire('testing: eventB', [new \stdClass()]);
$dispatcher->fire('testing: eventC', [new \stdClass()]);
$dispatcher->fire('testing: eventC', [new \stdClass()]);
$dispatcher->fire('testing: eventC', [new \stdClass()]);
$dispatcher->fire('testing: eventC', [new \stdClass()]);
$dispatcher->fire('testing: eventD', [new \stdClass()]);
});
$this->tapLaravelVersionFrom(5.4, function () use ($dispatcher) {
$dispatcher->dispatch('testing: eventA', [new \stdClass()]);
$dispatcher->dispatch('testing: eventB', [new \stdClass()]);
$dispatcher->dispatch('testing: eventB', [new \stdClass()]);
$dispatcher->dispatch('testing: eventC', [new \stdClass()]);
$dispatcher->dispatch('testing: eventC', [new \stdClass()]);
$dispatcher->dispatch('testing: eventC', [new \stdClass()]);
$dispatcher->dispatch('testing: eventC', [new \stdClass()]);
$dispatcher->dispatch('testing: eventD', [new \stdClass()]);
});
$tracker->terminate();
$events = $tracker->data()->get('events');
$this->assertEquals([
'name' => 'testing: eventA',
'count' => 1,
'data' => collect([]),
], $events->pull(0));
$this->assertEquals([
'name' => 'testing: eventB',
'count' => 2,
], $events->pull(1));
$this->assertEquals([
'name' => 'testing: eventC',
'count' => 4,
], $events->pull(2));
$this->assertEquals([
'name' => 'testing: eventD',
'count' => 1,
'data' => collect([]),
], $events->pull(3));
$this->assertEquals(8, $tracker->meta()->get('events_count'));
}
/** @test */
function does_not_group_events_if_events_group_is_disabled_in_config()
{
$this->app->make('config')->set('profiler.data.events', true);
$this->app->make('config')->set('profiler.group.events', false);
$tracker = $this->app->make(EventsTracker::class);
$dispatcher = $this->app->make(Dispatcher::class);
$this->tapLaravelVersionTill(5.3, function () use ($dispatcher) {
$dispatcher->fire('testing: eventA', [new \stdClass()]);
$dispatcher->fire('testing: eventB', [new \stdClass()]);
$dispatcher->fire('testing: eventB', [new \stdClass()]);
});
$this->tapLaravelVersionFrom(5.4, function () use ($dispatcher) {
$dispatcher->dispatch('testing: eventA', [new \stdClass()]);
$dispatcher->dispatch('testing: eventB', [new \stdClass()]);
$dispatcher->dispatch('testing: eventB', [new \stdClass()]);
});
$tracker->terminate();
$events = $tracker->data()->get('events');
$this->assertEquals([
'name' => 'testing: eventA',
'count' => 1,
'data' => collect([]),
], $events->pull(0));
$this->assertEquals([
'name' => 'testing: eventB',
'count' => 1,
'data' => collect([]),
], $events->pull(1));
$this->assertEquals([
'name' => 'testing: eventB',
'count' => 1,
'data' => collect([]),
], $events->pull(2));
$this->assertEquals(3, $tracker->meta()->get('events_count'));
}
/** @test */
function does_not_track_laravel_profiler_internal_events()
{
$tracker = $this->app->make(EventsTracker::class);
event(new ExceptionHandling(new Exception()));
event(new ProfilerBound());
event(new ResetTrackers());
event(new Terminating());
event(new Tracking());
$tracker->terminate();
$this->assertEquals(0, $tracker->meta()->get('events_count'));
}
/** @test */
function does_not_track_laravel_framework_events()
{
$tracker = $this->app->make(EventsTracker::class);
event('bootstrapped: ' . \Illuminate\Foundation\Bootstrap\BootProviders::class, [new \stdClass()]);
$tracker->terminate();
$this->assertEquals(0, $tracker->meta()->get('events_count'));
}
/** @test */
function can_reset_events()
{
$tracker = $this->app->make(EventsTracker::class);
event(new DummyEventA());
event(new DummyEventA());
event(new DummyEventA());
profiler_reset();
event(new DummyEventA());
$tracker->terminate();
$this->assertEquals(1, $tracker->meta()->get('events_count'));
$this->assertCount(1, $tracker->data()->get('events'));
}
}