-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from journy-io/tests
Add more tests
- Loading branch information
Showing
1 changed file
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,6 +169,53 @@ public function test_it_upserts_user() | |
} | ||
} | ||
|
||
public function test_it_upserts_user_without_user_id() | ||
{ | ||
$factory = new Psr17Factory(); | ||
$json = '{"message":"The data is correctly stored.","meta":{"status":201,"requestId":"01ETG3HQ4JY4HNNZ84FBJM3CSC"}}'; | ||
$http = new HttpClientFixed(new Response(201, [], $json)); | ||
$client = new Client($http, $factory, $factory, ["apiKey" => "key"]); | ||
$now = new DateTimeImmutable("now"); | ||
|
||
$this->assertEquals( | ||
new CallResult(true, false, 0, 0, [], null), | ||
$client->upsertUser( | ||
[ | ||
"email" => "[email protected]", | ||
"properties" => [ | ||
"string" => "string", | ||
"boolean" => true, | ||
"number" => 22, | ||
"date" => $now, | ||
], | ||
] | ||
) | ||
); | ||
|
||
$request = $http->getLastRequest(); | ||
$this->assertInstanceOf(RequestInterface::class, $request); | ||
|
||
if ($request instanceof RequestInterface) { | ||
$request->getBody()->rewind(); | ||
$body = $request->getBody()->getContents(); | ||
$payload = json_decode($body, true); | ||
$this->assertEquals( | ||
[ | ||
"identification" => [ | ||
"email" => "[email protected]", | ||
], | ||
"properties" => [ | ||
"string" => "string", | ||
"boolean" => "true", | ||
"number" => "22", | ||
"date" => $now->format(DATE_ATOM), | ||
], | ||
], | ||
$payload | ||
); | ||
} | ||
} | ||
|
||
public function test_it_upserts_account() | ||
{ | ||
$factory = new Psr17Factory(); | ||
|
@@ -229,6 +276,65 @@ public function test_it_upserts_account() | |
} | ||
} | ||
|
||
public function test_it_upserts_account_without_account_id() | ||
{ | ||
$factory = new Psr17Factory(); | ||
$json = '{"message":"The data is correctly stored.","meta":{"status":201,"requestId":"01ETG3HQ4JY4HNNZ84FBJM3CSC"}}'; | ||
$http = new HttpClientFixed(new Response(201, [], $json)); | ||
$client = new Client($http, $factory, $factory, ["apiKey" => "key"]); | ||
$now = new DateTimeImmutable("now"); | ||
|
||
$this->assertEquals( | ||
new CallResult(true, false, 0, 0, [], null), | ||
$client->upsertAccount( | ||
[ | ||
"domain" => "journy.io", | ||
"properties" => [ | ||
"string" => "string", | ||
"boolean" => true, | ||
"number" => 22, | ||
"date" => $now, | ||
], | ||
"members" => [ | ||
["userId" => "1"], | ||
["userId" => "2"], | ||
], | ||
] | ||
) | ||
); | ||
|
||
$request = $http->getLastRequest(); | ||
$this->assertInstanceOf(RequestInterface::class, $request); | ||
if ($request instanceof RequestInterface) { | ||
$request->getBody()->rewind(); | ||
$body = $request->getBody()->getContents(); | ||
$payload = json_decode($body, true); | ||
$this->assertEquals( | ||
[ | ||
"identification" => [ | ||
"domain" => "journy.io", | ||
], | ||
"properties" => [ | ||
"string" => "string", | ||
"boolean" => "true", | ||
"number" => "22", | ||
"date" => $now->format(DATE_ATOM), | ||
], | ||
"members" => [ | ||
[ | ||
"identification" => ["userId" => "1"], | ||
], | ||
[ | ||
"identification" => ["userId" => "2"], | ||
], | ||
], | ||
], | ||
$payload | ||
); | ||
} | ||
} | ||
|
||
|
||
public function test_it_triggers_event_for_user() | ||
{ | ||
$factory = new Psr17Factory(); | ||
|
@@ -261,6 +367,38 @@ public function test_it_triggers_event_for_user() | |
} | ||
} | ||
|
||
public function test_it_triggers_event_for_user_with_email() | ||
{ | ||
$factory = new Psr17Factory(); | ||
$json = '{"message":"The data is correctly stored.","meta":{"status":201,"requestId":"01ETG3HQ4JY4HNNZ84FBJM3CSC"}}'; | ||
$http = new HttpClientFixed(new Response(201, [], $json)); | ||
$client = new Client($http, $factory, $factory, ["apiKey" => "key"]); | ||
|
||
$this->assertEquals( | ||
new CallResult(true, false, 0, 0, [], null), | ||
$client->addEvent(Event::forUser("login", UserIdentified::byEmail("[email protected]"))) | ||
); | ||
|
||
$request = $http->getLastRequest(); | ||
$this->assertInstanceOf(RequestInterface::class, $request); | ||
if ($request instanceof RequestInterface) { | ||
$request->getBody()->rewind(); | ||
$body = $request->getBody()->getContents(); | ||
$payload = json_decode($body, true); | ||
$this->assertEquals( | ||
[ | ||
"name" => "login", | ||
"identification" => [ | ||
"user" => [ | ||
"email" => "[email protected]" | ||
], | ||
], | ||
], | ||
$payload | ||
); | ||
} | ||
} | ||
|
||
public function test_it_links_web_visitor_with_user() | ||
{ | ||
$factory = new Psr17Factory(); | ||
|
@@ -292,6 +430,36 @@ public function test_it_links_web_visitor_with_user() | |
} | ||
} | ||
|
||
public function test_it_links_web_visitor_with_user_without_user_id() | ||
{ | ||
$factory = new Psr17Factory(); | ||
$json = '{"message":"The data is correctly stored.","meta":{"status":201,"requestId":"01ETG3HQ4JY4HNNZ84FBJM3CSC"}}'; | ||
$http = new HttpClientFixed(new Response(201, [], $json)); | ||
$client = new Client($http, $factory, $factory, ["apiKey" => "key"]); | ||
|
||
$this->assertEquals( | ||
new CallResult(true, false, 0, 0, [], null), | ||
$client->link(["deviceId" => "deviceId", "email" => "email"]) | ||
); | ||
|
||
$request = $http->getLastRequest(); | ||
$this->assertInstanceOf(RequestInterface::class, $request); | ||
if ($request instanceof RequestInterface) { | ||
$request->getBody()->rewind(); | ||
$body = $request->getBody()->getContents(); | ||
$payload = json_decode($body, true); | ||
$this->assertEquals( | ||
[ | ||
"deviceId" => "deviceId", | ||
"identification" => [ | ||
"email" => "email", | ||
], | ||
], | ||
$payload | ||
); | ||
} | ||
} | ||
|
||
public function test_it_triggers_event_for_user_with_date() | ||
{ | ||
$factory = new Psr17Factory(); | ||
|