Skip to content

Commit

Permalink
Merge pull request #4 from journy-io/tests
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
hansott authored Mar 30, 2021
2 parents dbe921b + bcc2fb0 commit 456a8a1
Showing 1 changed file with 168 additions and 0 deletions.
168 changes: 168 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 456a8a1

Please sign in to comment.