|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SlashEquip\LaravelSegment\Facades\Fakes; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Illuminate\Support\Collection; |
| 7 | +use PHPUnit\Framework\Assert as PHPUnit; |
| 8 | +use SlashEquip\LaravelSegment\Contracts\CanBeIdentifiedForSegment; |
| 9 | +use SlashEquip\LaravelSegment\Contracts\CanBeSentToSegment; |
| 10 | +use SlashEquip\LaravelSegment\PendingUserSegment; |
| 11 | +use SlashEquip\LaravelSegment\SegmentServiceContract; |
| 12 | +use SlashEquip\LaravelSegment\SimpleSegmentEvent; |
| 13 | +use SlashEquip\LaravelSegment\SimpleSegmentIdentify; |
| 14 | + |
| 15 | +class SegmentFake implements SegmentServiceContract |
| 16 | +{ |
| 17 | + private CanBeIdentifiedForSegment $user; |
| 18 | + |
| 19 | + private ?array $context = []; |
| 20 | + |
| 21 | + private array $events = []; |
| 22 | + |
| 23 | + private array $identities = []; |
| 24 | + |
| 25 | + public function setGlobalUser(CanBeIdentifiedForSegment $globalUser): void |
| 26 | + { |
| 27 | + $this->user = $globalUser; |
| 28 | + } |
| 29 | + |
| 30 | + public function setGlobalContext(array $globalContext): void |
| 31 | + { |
| 32 | + $this->context = $globalContext; |
| 33 | + } |
| 34 | + |
| 35 | + public function identify(?array $identifyData = []): void |
| 36 | + { |
| 37 | + $this->identities[] = new SimpleSegmentIdentify($this->user, $identifyData); |
| 38 | + } |
| 39 | + |
| 40 | + public function track(string $event, array $eventData = null): void |
| 41 | + { |
| 42 | + $this->events[] = new SimpleSegmentEvent($this->user, $event, $eventData); |
| 43 | + } |
| 44 | + |
| 45 | + public function forUser(CanBeIdentifiedForSegment $user): PendingUserSegment |
| 46 | + { |
| 47 | + $this->user = $user; |
| 48 | + |
| 49 | + return new PendingUserSegment($this, $user); |
| 50 | + } |
| 51 | + |
| 52 | + public function push(CanBeSentToSegment $segment): void |
| 53 | + { |
| 54 | + if ($segment instanceof SimpleSegmentIdentify) { |
| 55 | + $this->identities[] = $segment; |
| 56 | + } |
| 57 | + |
| 58 | + if ($segment instanceof SimpleSegmentEvent) { |
| 59 | + $this->events[] = $segment; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public function terminate(): void |
| 64 | + { |
| 65 | + } |
| 66 | + |
| 67 | + public function assertIdentified(Closure|int $callback = null): void |
| 68 | + { |
| 69 | + if (is_numeric($callback)) { |
| 70 | + $this->assertIdentifiedTimes($callback); |
| 71 | + |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + PHPUnit::assertTrue( |
| 76 | + $this->identities($callback)->count() > 0, |
| 77 | + 'The expected identities were not called.' |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public function assertIdentifiedTimes(int $times = 1): void |
| 82 | + { |
| 83 | + $count = collect($this->identities)->count(); |
| 84 | + |
| 85 | + PHPUnit::assertSame( |
| 86 | + $times, $count, |
| 87 | + "The identity was called {$count} times instead of {$times} times." |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + public function assertNotIdentified(Closure $callback = null): void |
| 92 | + { |
| 93 | + PHPUnit::assertCount( |
| 94 | + 0, $this->identities($callback), |
| 95 | + 'The unexpected identity was called.' |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + public function assertNothingIdentified(): void |
| 100 | + { |
| 101 | + $identities = collect($this->identities); |
| 102 | + |
| 103 | + PHPUnit::assertEmpty($identities, $identities->count().' events were found unexpectedly.'); |
| 104 | + } |
| 105 | + |
| 106 | + public function assertTracked(Closure|int $callback = null): void |
| 107 | + { |
| 108 | + if (is_numeric($callback)) { |
| 109 | + $this->assertTrackedTimes($callback); |
| 110 | + |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + PHPUnit::assertTrue( |
| 115 | + $this->events($callback)->count() > 0, |
| 116 | + 'The expected events were not called.' |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + public function assertTrackedTimes(int $times = 1): void |
| 121 | + { |
| 122 | + $count = collect($this->events)->count(); |
| 123 | + |
| 124 | + PHPUnit::assertSame( |
| 125 | + $times, $count, |
| 126 | + "The event called {$count} times instead of {$times} times." |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + public function assertEventTracked(string $event, Closure|int $callback = null): void |
| 131 | + { |
| 132 | + PHPUnit::assertTrue( |
| 133 | + $this->events($callback, $event)->count() > 0, |
| 134 | + 'The expected events were not called.' |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + public function assertNotTracked(Closure $callback = null): void |
| 139 | + { |
| 140 | + PHPUnit::assertCount( |
| 141 | + 0, $this->events($callback), |
| 142 | + 'The unexpected event was called.' |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + public function assertEventNotTracked(string $event, Closure|int $callback = null): void |
| 147 | + { |
| 148 | + PHPUnit::assertCount( |
| 149 | + 0, $this->events($callback, $event), |
| 150 | + 'The expected events were not called.' |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + public function assertNothingTracked(): void |
| 155 | + { |
| 156 | + $events = collect($this->events); |
| 157 | + |
| 158 | + PHPUnit::assertEmpty($events, $events->count().' events were found unexpectedly.'); |
| 159 | + } |
| 160 | + |
| 161 | + private function identities(Closure $callback = null): Collection |
| 162 | + { |
| 163 | + $identities = collect($this->identities); |
| 164 | + |
| 165 | + if ($identities->isEmpty()) { |
| 166 | + return collect(); |
| 167 | + } |
| 168 | + |
| 169 | + $callback = $callback ?: fn () => true; |
| 170 | + |
| 171 | + return $identities->filter(fn (SimpleSegmentIdentify $identity) => $callback($identity)); |
| 172 | + } |
| 173 | + |
| 174 | + private function events(Closure $callback = null, string $event = null): Collection |
| 175 | + { |
| 176 | + $events = collect($this->events); |
| 177 | + |
| 178 | + if ($events->isEmpty()) { |
| 179 | + return collect(); |
| 180 | + } |
| 181 | + |
| 182 | + $callback = $callback ?: fn () => true; |
| 183 | + |
| 184 | + return $events |
| 185 | + ->when($event, function (Collection $collection) use ($event) { |
| 186 | + return $collection->filter(function (SimpleSegmentEvent $segmentEvent) use ($event) { |
| 187 | + return $segmentEvent->toSegment()->event === $event; |
| 188 | + }); |
| 189 | + }) |
| 190 | + ->filter(fn (SimpleSegmentEvent $event) => $callback($event)); |
| 191 | + } |
| 192 | +} |
0 commit comments