Skip to content

Commit

Permalink
Add now methods to interface and Fake (#18)
Browse files Browse the repository at this point in the history
* add now methods to interface and Fake

* add test

* improve dockblocks for testing

---------

Co-authored-by: Luke Renfrew <[email protected]>
  • Loading branch information
lukerenfrew and Luke Renfrew authored Sep 2, 2024
1 parent 90d3b55 commit 80ccbb2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Contracts/SegmentServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ public function setGlobalContext(array $globalContext): void;
*/
public function track(string $event, ?array $eventData = null): void;

/**
* @param array<string, mixed> $eventData
*/
public function trackNow(string $event, ?array $eventData = null): void;

/**
* @param array<string, mixed> $identifyData
*/
public function identify(?array $identifyData = null): void;

/**
* @param array<string, mixed> $identifyData
*/
public function identifyNow(?array $identifyData = null): void;


public function forUser(CanBeIdentifiedForSegment $user): PendingUserSegment;

public function push(CanBeSentToSegment $segment): void;
Expand Down
16 changes: 16 additions & 0 deletions src/Facades/Fakes/SegmentFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public function identify(?array $identifyData = []): void
$this->identities[] = new SimpleSegmentIdentify($this->user, $identifyData);
}

/**
* @param array<string, mixed> $identifyData
*/
public function identifyNow(?array $identifyData = []): void
{
$this->identities[] = new SimpleSegmentIdentify($this->user, $identifyData);
}

/**
* @param array<string, mixed> $eventData
*/
Expand All @@ -54,6 +62,14 @@ public function track(string $event, ?array $eventData = null): void
$this->events[] = new SimpleSegmentEvent($this->user, $event, $eventData);
}

/**
* @param array<string, mixed> $eventData
*/
public function trackNow(string $event, ?array $eventData = null): void
{
$this->events[] = new SimpleSegmentEvent($this->user, $event, $eventData);
}

public function forUser(CanBeIdentifiedForSegment $user): PendingUserSegment
{
$this->user = $user;
Expand Down
12 changes: 11 additions & 1 deletion src/Facades/Segment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SlashEquip\LaravelSegment\Facades;

use Closure;
use Illuminate\Support\Facades\Facade;
use SlashEquip\LaravelSegment\Contracts\CanBeIdentifiedForSegment;
use SlashEquip\LaravelSegment\Contracts\CanBeSentToSegment;
Expand All @@ -19,7 +20,16 @@
* @method static PendingUserSegment forUser(CanBeIdentifiedForSegment $user)
* @method static void push(CanBeSentToSegment $segment)
* @method static void terminate()
*
* @method static void assertIdentified(Closure|int|null $callback = null)
* @method static void assertIdentifiedTimes(int $times)
* @method static void assertNotIdentified(Closure $callback = null)
* @method static void assertNothingIdentified()
* @method static void assertTracked(Closure|int|null $callback = null)
* @method static void assertTrackedTimes(int $times)
* @method static void assertEventTracked(string $event,Closure|int|null $callback = null)
* @method static void assertNotTracked(Closure $callback = null)
* @method static void assertNothingTracked()
* @see \SlashEquip\LaravelSegment\SegmentService
* @see SegmentFake
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/SegmentFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ public function getSegmentIdentifier(): string
Segment::assertIdentifiedTimes(1);
});

it('can test that an identity was called immediately', function () {
Segment::fake();

Segment::forUser($this->user)->identifyNow([
'first_name' => 'Lorem',
'last_name' => 'Ipsum',
]);

Segment::assertIdentified();
Segment::assertIdentifiedTimes(1);
});

it('can test that an identity was called one times', function () {
Segment::fake();

Expand Down Expand Up @@ -116,6 +128,19 @@ public function getSegmentIdentifier(): string
Segment::assertTrackedTimes(1);
});


it('can test that an event was tracked immediately', function () {
Segment::fake();

Segment::forUser($this->user)->trackNow('some_event', [
'first_name' => 'Lorem',
'last_name' => 'Ipsum',
]);

Segment::assertTracked();
Segment::assertTrackedTimes(1);
});

it('it can assert an event was tracked one time', function () {
Segment::fake();

Expand Down

0 comments on commit 80ccbb2

Please sign in to comment.