Skip to content

Commit ba46774

Browse files
committed
Add tests for AtomxClient
1 parent d64e3d6 commit ba46774

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

Atomx/ApiClient.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ function __construct()
2020
$this->client = new Client(['base_url' => $this->apiBase]);
2121
}
2222

23+
public function getClient()
24+
{
25+
return $this->client;
26+
}
27+
2328
public function clearFields()
2429
{
2530
$this->fields = [];

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Package Test Suite">
15+
<directory suffix=".php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

tests/ClientTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php namespace tests;
2+
3+
use Atomx\AccountStore;
4+
use Atomx\ApiException;
5+
use Atomx\Resources\Advertiser;
6+
use Atomx\Resources\Domain;
7+
use GuzzleHttp\Message\Response;
8+
use GuzzleHttp\Stream\Stream;
9+
use GuzzleHttp\Subscriber\History;
10+
use GuzzleHttp\Subscriber\Mock;
11+
12+
class MemoryAccountStore implements AccountStore {
13+
private $token = 'TEST_TOKEN';
14+
15+
public function getToken() { return $this->token; }
16+
public function storeToken($token) { $this->token = $token; }
17+
public function getUsername() { return ''; }
18+
public function getPassword() { return ''; }
19+
public function getApiBase() { return 'https://api.atomx.com/v3/'; }
20+
}
21+
22+
class ClientTest extends \PHPUnit_Framework_TestCase {
23+
public function testDiscardInvalidToken()
24+
{
25+
$store = new MemoryAccountStore();
26+
$advertiser = new Advertiser($store);
27+
28+
$mock = new Mock([
29+
"HTTP/1.1 401 Unknown Error\r\nContent-Length: 55\r\n\r\n{\"success\":false,\"error\":\"User unauthorized\",\"errno\":1}"
30+
]);
31+
32+
$advertiser->getClient()->getEmitter()->attach($mock);
33+
34+
try {
35+
$advertiser->get(['limit' => 1, 'depth' => 0]);
36+
} catch (ApiException $e) {}
37+
38+
$this->assertNull($store->getToken());
39+
}
40+
41+
public function testRequestWithoutLogin()
42+
{
43+
$store = new MemoryAccountStore();
44+
$domain = new Domain($store);
45+
46+
$history = new History();
47+
$mock = new Mock([ $this->getValidEmptyResponse() ]);
48+
49+
$domain->getClient()->getEmitter()->attach($mock);
50+
$domain->getClient()->getEmitter()->attach($history);
51+
52+
$domain->get(['limit' => 1, 'depth' => 0]);
53+
54+
$this->assertArrayNotHasKey('Authorization', $history->getLastRequest()->getHeaders());
55+
}
56+
57+
public function testRequestWithAuthToken()
58+
{
59+
$store = new MemoryAccountStore();
60+
$advertiser = new Advertiser($store);
61+
62+
$history = new History();
63+
$mock = new Mock([ $this->getValidEmptyResponse() ]);
64+
65+
$advertiser->getClient()->getEmitter()->attach($mock);
66+
$advertiser->getClient()->getEmitter()->attach($history);
67+
68+
$advertiser->get(['limit' => 1, 'depth' => 0]);
69+
70+
$this->assertArraySubset(['Authorization' => ['Bearer TEST_TOKEN']], $history->getLastRequest()->getHeaders());
71+
}
72+
73+
public function testLogin()
74+
{
75+
$store = new MemoryAccountStore();
76+
$store->storeToken(null);
77+
78+
$advertiser = new Advertiser($store);
79+
80+
$history = new History();
81+
$mock = new Mock([
82+
$this->getValidLoginResponse(),
83+
$this->getValidEmptyResponse()
84+
]);
85+
86+
$advertiser->getClient()->getEmitter()->attach($mock);
87+
$advertiser->getClient()->getEmitter()->attach($history);
88+
89+
$advertiser->get(['limit' => 1, 'depth' => 0]);
90+
91+
92+
$this->assertEquals(2, count($history));
93+
$loginRequest = $history->getIterator()[0]['request'];
94+
$this->assertArrayNotHasKey('Authorization', $loginRequest->getHeaders());
95+
$this->assertArraySubset(['Authorization' => ['Bearer LOGIN_TOKEN']], $history->getLastRequest()->getHeaders());
96+
97+
}
98+
99+
private function getValidEmptyResponse()
100+
{
101+
return new Response(200, [], Stream::factory("[]"));
102+
}
103+
104+
private function getValidLoginResponse()
105+
{
106+
$loginBody = '{"user":{"id":1},"resource":"auth_token","totp_required":false,' .
107+
'"message":"atomx (user id 1) logged in","success":true,"auth_token":"LOGIN_TOKEN"}';
108+
109+
return new Response(200,
110+
['Content-Type' => 'application/json; charset=UTF-8'],
111+
Stream::factory($loginBody)
112+
);
113+
}
114+
}

0 commit comments

Comments
 (0)