Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
EventTracer
n/a
0 / 0
n/a
0 / 0
4
n/a
0 / 0
 trace
n/a
0 / 0
n/a
0 / 0
2
 count
n/a
0 / 0
n/a
0 / 0
2
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\PhpUnit\Event;
21
22use PHPUnit\Event\Event;
23use PHPUnit\Event\Tracer\Tracer;
24
25/**
26 * Collects PHPUnit events grouped by their concrete event class.
27 *
28 * This tracer MUST store every received event instance under its fully
29 * qualified class name so tests MAY later inspect which events were emitted
30 * and how many times each event type occurred during execution.
31 *
32 * The collected events SHALL remain available in memory for the lifetime of
33 * this tracer instance. Consumers SHOULD treat the stored event collection as
34 * test-support state and SHOULD NOT rely on it for production behavior.
35 *
36 * @codeCoverageIgnore
37 */
38class EventTracer implements Tracer
39{
40    /**
41     * Stores traced events grouped by their concrete event class name.
42     *
43     * Each key MUST be a fully qualified event class name and each value MUST
44     * contain the list of event instances received for that class.
45     *
46     * @var array<class-string<Event>, list<Event>>
47     */
48    private array $events = [];
49
50    /**
51     * Records a PHPUnit event in the internal event registry.
52     *
53     * This method MUST group the event by its concrete runtime class and SHALL
54     * append the received instance to the corresponding event list without
55     * discarding previously traced events of the same type.
56     *
57     * @param Event $event the PHPUnit event instance to record
58     *
59     * @return void
60     */
61    public function trace(Event $event): void
62    {
63        if (! \array_key_exists($event::class, $this->events)) {
64            $this->events[$event::class] = [];
65        }
66
67        $this->events[$event::class][] = $event;
68    }
69
70    /**
71     * Returns the number of traced events for the given event class.
72     *
73     * This method MUST return the exact number of stored events matching the
74     * provided fully qualified event class name. When no events were recorded
75     * for the given class, the method SHALL return 0.
76     *
77     * @param class-string<Event> $eventClass the fully qualified PHPUnit event
78     *                                        class name to count
79     *
80     * @return int the number of recorded events for the specified class
81     */
82    public function count(string $eventClass): int
83    {
84        return \array_key_exists($eventClass, $this->events)
85            ? \count($this->events[$eventClass])
86            : 0;
87    }
88}