WebfProjectionsDataCollectorTest.php 8.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

declare(strict_types=1);

namespace Tests\DataCollector;

use Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Tests\Event\Stub\EventStub;
use Tests\Event\Stub\EventStubA;
use Tests\Event\Stub\EventStubB;
use Tests\Projection\Stub\ProjectionStub;
14
use Tests\Projection\Stub\UnserializableProjection;
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
use Tests\Service\Stub\FakeTraceableEventProjector;
use Webf\Projections\Bundle\DataCollector\WebfProjectionsDataCollector;
use Webf\Projections\Core\Event\EventClassManagerInterface;
use Webf\Projections\Core\Event\InMemoryEventClassManager;
use Webf\Projections\Core\Projection\ProjectionFactory;
use Webf\Projections\Core\Service\Data\DispatchedEvent;
use Webf\Projections\Core\Service\EventProjector;
use Webf\Projections\Core\Service\EventProjectorInterface;
use Webf\Projections\Core\Service\ProjectionManager;
use Webf\Projections\Core\Service\ProjectionManagerInterface;
use Webf\Projections\Core\Service\TraceableEventProjector;

/**
 * @internal
 * @covers \Webf\Projections\Bundle\DataCollector\WebfProjectionsDataCollector
 */
class WebfProjectionsDataCollectorTest extends TestCase
{
    public function test_get_name()
    {
        $dataCollector = new WebfProjectionsDataCollector(
            $this->createMock(EventClassManagerInterface::class),
            $this->createMock(EventProjectorInterface::class),
            $this->createMock(ProjectionManagerInterface::class)
        );

        $profile = new Profile(uniqid());
        $profile->addCollector($dataCollector);

        $this->assertSame(
            $dataCollector,
            $profile->getCollector(WebfProjectionsDataCollector::NAME)
        );
    }

    public function test_initial_values()
    {
        $dataCollector = new WebfProjectionsDataCollector(
            $this->createMock(EventClassManagerInterface::class),
            $this->createMock(EventProjectorInterface::class),
            $this->createMock(ProjectionManagerInterface::class)
        );

        $this->assertSame(0, $dataCollector->getNbEvents());
        $this->assertSame(0, $dataCollector->getNbSuccessfulEvents());
        $this->assertSame(0, $dataCollector->getNbFailedEvents());
        $this->assertSame(0, $dataCollector->getNbOrphanedEvents());
        $this->assertSame(0, $dataCollector->getNbEventTypes());
        $this->assertSame(0, $dataCollector->getNbTotalEventTypes());
        $this->assertSame(0, $dataCollector->getNbProjections());
        $this->assertSame(0, $dataCollector->getNbTotalProjections());
        $this->assertSame(0.0, $dataCollector->getExecutionTime());
        $this->assertSame([], $dataCollector->getEvents());
    }

    public function test_late_collect(): void
    {
        $e1 = new DispatchedEvent(new EventStubA(), null);
73
        $e1->projectionStarted($projections[] = new ProjectionStub());
74
        $e1->projectionSucceeded();
75
        $e1->projectionStarted($projections[] = new ProjectionStub());
76
77
78
79
        $e1->projectionSucceeded();
        $e1->dispatched();

        $e2 = new DispatchedEvent(new EventStubA(), null);
80
        $e2->projectionStarted($projections[] = new ProjectionStub());
81
82
83
84
85
86
87
        $e2->projectionFailed(new Exception());
        $e2->dispatched();

        $e3 = new DispatchedEvent(new EventStubB(), null);
        $e3->dispatched();

        $eventClasses = [EventStub::class, EventStubA::class, EventStubB::class];
88
        $allProjections = [new ProjectionStub(), new ProjectionStub()];
89
90
91
92
93

        $dataCollector = new WebfProjectionsDataCollector(
            new InMemoryEventClassManager($eventClasses),
            new FakeTraceableEventProjector([$e1, $e2, $e3]),
            new ProjectionManager([
94
                new ProjectionFactory($allProjections),
95
96
97
98
99
100
101
102
103
104
105
106
107
108
            ])
        );

        $dataCollector->lateCollect();

        $this->assertSame(3, $dataCollector->getNbEvents());
        $this->assertSame(1, $dataCollector->getNbSuccessfulEvents());
        $this->assertSame(1, $dataCollector->getNbFailedEvents());
        $this->assertSame(1, $dataCollector->getNbOrphanedEvents());
        $this->assertSame(2, $dataCollector->getNbEventTypes());
        $this->assertSame(
            count($eventClasses),
            $dataCollector->getNbTotalEventTypes()
        );
109
110
        $this->assertSame(count($projections), $dataCollector->getNbProjections());
        $this->assertSame(count($allProjections), $dataCollector->getNbTotalProjections());
111
112
113
114
        $this->assertSame(
            $e1->getExecutionTime() + $e2->getExecutionTime() + $e3->getExecutionTime(),
            $dataCollector->getExecutionTime()
        );
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
        $events = $dataCollector->getEvents();
        foreach ([$e1, $e2, $e3] as $i => $event) {
            $this->assertEquals(
                get_class($event->getValue()),
                (string) $events[$i]['type']
            );
            $this->assertEquals(
                $event->getExecutionTime(),
                $events[$i]['execution_time']
            );
            $this->assertEquals(
                $event->hasFailed(),
                $events[$i]['failed']
            );
            $this->assertEquals(
                0 === count($event->getProjections()),
                $events[$i]['orphaned']
            );
            $this->assertEquals(
                $event->getCaller(),
                $events[$i]['caller']
            );
            $this->assertEquals(
                get_class($event->getValue()),
                $events[$i]['value']->getType()
            );
            foreach ($event->getProjections() as $j => $projection) {
                $this->assertEquals(
                    get_class($projection->getValue()),
                    (string) $events[$i]['projections'][$j]['type']
                );
                $this->assertEquals(
                    $projection->getExecutionTime(),
                    $events[$i]['projections'][$j]['execution_time']
                );
                $this->assertEquals(
                    $projection->hasFailed(),
                    $events[$i]['projections'][$j]['failed']
                );
                $this->assertEquals(
                    is_object($t = $projection->getThrowable()) ? get_class($t) : gettype($t),
                    $events[$i]['projections'][$j]['throwable']->getType()
                );
            }
        }
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
    }

    public function test_late_collect_need_traceable_event_projector()
    {
        $eventClassManager = new InMemoryEventClassManager([EventStub::class]);

        $projectionManager = new ProjectionManager([
            new ProjectionFactory([new ProjectionStub([EventStub::class])]),
        ]);

        $dataCollector = new WebfProjectionsDataCollector(
            $eventClassManager,
            $eventProjector = new TraceableEventProjector($projectionManager),
            $projectionManager
        );

        $eventProjector->dispatch(new EventStub());
        $dataCollector->lateCollect();
        $this->assertEquals(1, $dataCollector->getNbSuccessfulEvents());

        $dataCollector = new WebfProjectionsDataCollector(
            $eventClassManager,
            $eventProjector = new EventProjector($projectionManager),
            $projectionManager
        );

        $eventProjector->dispatch(new EventStub());
        $dataCollector->lateCollect();
        $this->assertEquals(0, $dataCollector->getNbSuccessfulEvents());
    }

    public function test_reset()
    {
        $eventClassManager = new InMemoryEventClassManager([EventStub::class]);

        $projectionManager = new ProjectionManager([
            new ProjectionFactory([new ProjectionStub([EventStub::class])]),
        ]);

        $dataCollector = new WebfProjectionsDataCollector(
            $eventClassManager,
            $eventProjector = new TraceableEventProjector($projectionManager),
            $projectionManager
        );

        $eventProjector->dispatch(new EventStub());
        $dataCollector->lateCollect();
        $this->assertEquals(1, $dataCollector->getNbSuccessfulEvents());

        $dataCollector->reset();
        $this->assertEquals(0, $dataCollector->getNbSuccessfulEvents());
    }

213
    public function test_projection_can_be_unserializable()
214
215
    {
        $dataCollector = new WebfProjectionsDataCollector(
216
217
218
219
220
221
222
223
224
            new InMemoryEventClassManager([EventStub::class]),
            $eventProjector = new TraceableEventProjector(
                $projectionManager = new ProjectionManager([
                    new ProjectionFactory([
                        new UnserializableProjection([EventStub::class]),
                    ]),
                ])
            ),
            $projectionManager
225
226
        );

227
228
        $eventProjector->dispatch(new EventStub());
        $dataCollector->lateCollect();
229

230
        $this->assertNotEmpty(serialize($dataCollector));
231
232
    }
}