Skip to content

Commit 80ccbb2

Browse files
lukerenfrewLuke Renfrew
andauthored
Add now methods to interface and Fake (#18)
* add now methods to interface and Fake * add test * improve dockblocks for testing --------- Co-authored-by: Luke Renfrew <luke.renfrew@googlemail.com>
1 parent 90d3b55 commit 80ccbb2

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

src/Contracts/SegmentServiceContract.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,22 @@ public function setGlobalContext(array $globalContext): void;
1818
*/
1919
public function track(string $event, ?array $eventData = null): void;
2020

21+
/**
22+
* @param array<string, mixed> $eventData
23+
*/
24+
public function trackNow(string $event, ?array $eventData = null): void;
25+
2126
/**
2227
* @param array<string, mixed> $identifyData
2328
*/
2429
public function identify(?array $identifyData = null): void;
2530

31+
/**
32+
* @param array<string, mixed> $identifyData
33+
*/
34+
public function identifyNow(?array $identifyData = null): void;
35+
36+
2637
public function forUser(CanBeIdentifiedForSegment $user): PendingUserSegment;
2738

2839
public function push(CanBeSentToSegment $segment): void;

src/Facades/Fakes/SegmentFake.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function identify(?array $identifyData = []): void
4646
$this->identities[] = new SimpleSegmentIdentify($this->user, $identifyData);
4747
}
4848

49+
/**
50+
* @param array<string, mixed> $identifyData
51+
*/
52+
public function identifyNow(?array $identifyData = []): void
53+
{
54+
$this->identities[] = new SimpleSegmentIdentify($this->user, $identifyData);
55+
}
56+
4957
/**
5058
* @param array<string, mixed> $eventData
5159
*/
@@ -54,6 +62,14 @@ public function track(string $event, ?array $eventData = null): void
5462
$this->events[] = new SimpleSegmentEvent($this->user, $event, $eventData);
5563
}
5664

65+
/**
66+
* @param array<string, mixed> $eventData
67+
*/
68+
public function trackNow(string $event, ?array $eventData = null): void
69+
{
70+
$this->events[] = new SimpleSegmentEvent($this->user, $event, $eventData);
71+
}
72+
5773
public function forUser(CanBeIdentifiedForSegment $user): PendingUserSegment
5874
{
5975
$this->user = $user;

src/Facades/Segment.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SlashEquip\LaravelSegment\Facades;
44

5+
use Closure;
56
use Illuminate\Support\Facades\Facade;
67
use SlashEquip\LaravelSegment\Contracts\CanBeIdentifiedForSegment;
78
use SlashEquip\LaravelSegment\Contracts\CanBeSentToSegment;
@@ -19,7 +20,16 @@
1920
* @method static PendingUserSegment forUser(CanBeIdentifiedForSegment $user)
2021
* @method static void push(CanBeSentToSegment $segment)
2122
* @method static void terminate()
22-
*
23+
* @method static void assertIdentified(Closure|int|null $callback = null)
24+
* @method static void assertIdentifiedTimes(int $times)
25+
* @method static void assertNotIdentified(Closure $callback = null)
26+
* @method static void assertNothingIdentified()
27+
* @method static void assertTracked(Closure|int|null $callback = null)
28+
* @method static void assertTrackedTimes(int $times)
29+
* @method static void assertEventTracked(string $event,Closure|int|null $callback = null)
30+
* @method static void assertNotTracked(Closure $callback = null)
31+
* @method static void assertNothingTracked()
32+
2333
* @see \SlashEquip\LaravelSegment\SegmentService
2434
* @see SegmentFake
2535
*/

tests/Unit/SegmentFakeTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ public function getSegmentIdentifier(): string
3838
Segment::assertIdentifiedTimes(1);
3939
});
4040

41+
it('can test that an identity was called immediately', function () {
42+
Segment::fake();
43+
44+
Segment::forUser($this->user)->identifyNow([
45+
'first_name' => 'Lorem',
46+
'last_name' => 'Ipsum',
47+
]);
48+
49+
Segment::assertIdentified();
50+
Segment::assertIdentifiedTimes(1);
51+
});
52+
4153
it('can test that an identity was called one times', function () {
4254
Segment::fake();
4355

@@ -116,6 +128,19 @@ public function getSegmentIdentifier(): string
116128
Segment::assertTrackedTimes(1);
117129
});
118130

131+
132+
it('can test that an event was tracked immediately', function () {
133+
Segment::fake();
134+
135+
Segment::forUser($this->user)->trackNow('some_event', [
136+
'first_name' => 'Lorem',
137+
'last_name' => 'Ipsum',
138+
]);
139+
140+
Segment::assertTracked();
141+
Segment::assertTrackedTimes(1);
142+
});
143+
119144
it('it can assert an event was tracked one time', function () {
120145
Segment::fake();
121146

0 commit comments

Comments
 (0)