Skip to content

Commit 22bab17

Browse files
committed
Move ApiException to separate dir
1 parent da66b42 commit 22bab17

File tree

7 files changed

+48
-12
lines changed

7 files changed

+48
-12
lines changed

Atomx/ApiClient.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Atomx;
22

3+
use Atomx\Exceptions\ApiException;
34
use GuzzleHttp\Client;
45
use GuzzleHttp\Exception\RequestException;
56
use GuzzleHttp\Message\Response;

Atomx/ApiException.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

Atomx/AtomxClient.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Atomx;
22

3+
use Atomx\Exceptions\ApiException;
34
use Exception;
45
use GuzzleHttp\Message\Response;
56

Atomx/Exceptions/ApiException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php namespace Atomx\Exceptions;
2+
3+
use Exception;
4+
5+
class ApiException extends Exception { }

Atomx/Resources/Login.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace Atomx\Resources;
22

3-
use Atomx\ApiException;
3+
use Atomx\Exceptions\ApiException;
44
use Atomx\AtomxClient;
55

66
class Login extends AtomxClient {

Atomx/Resources/Report.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php namespace Atomx\Resources;
22

3-
use Atomx\ApiException;
3+
use Atomx\Exceptions\ApiException;
44
use Atomx\AtomxClient;
55
use Atomx\ReportStreamer;
66
use GuzzleHttp\Message\Response;
7-
use GuzzleHttp\Stream\Stream;
87

98
class Report extends AtomxClient {
109
private $returnStream = false;

tests/ClientTest.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php namespace tests;
22

33
use Atomx\AccountStore;
4-
use Atomx\ApiException;
4+
use Atomx\Exceptions\ApiException;
5+
use Atomx\Exceptions\TotpRequiredException;
56
use Atomx\MemoryAccountStore;
67
use Atomx\Resources\Advertiser;
78
use Atomx\Resources\Domain;
@@ -26,6 +27,7 @@ class TestLoginAccountStore extends MemoryAccountStore {
2627
public function setLoginClient($client) { $this->loginClient = $client; }
2728
protected function getLoginClient() { return $this->loginClient; }
2829
}
30+
2931
class ClientTest extends \PHPUnit_Framework_TestCase {
3032
public function testDiscardInvalidToken()
3133
{
@@ -106,18 +108,51 @@ public function testLogin()
106108
$loginRequest = $history->getIterator()[0]['request'];
107109
$this->assertArrayNotHasKey('Authorization', $loginRequest->getHeaders());
108110
$this->assertArraySubset(['Authorization' => ['Bearer LOGIN_TOKEN']], $history->getLastRequest()->getHeaders());
111+
}
112+
113+
public function testTotp()
114+
{
115+
$login = new Login(new TestAccountStore);
116+
117+
$store = new TestLoginAccountStore();
118+
$store->setLoginClient($login);
119+
120+
$advertiser = new Advertiser($store);
121+
122+
$history = new History();
123+
$mock = new Mock([
124+
$this->getValidLoginResponse(true),
125+
$this->getValidEmptyResponse()
126+
]);
127+
128+
$login->getClient()->getEmitter()->attach($mock);
129+
$login->getClient()->getEmitter()->attach($history);
130+
131+
$advertiser->getClient()->getEmitter()->attach($mock);
132+
$advertiser->getClient()->getEmitter()->attach($history);
133+
134+
$totpException = false;
135+
136+
try {
137+
$advertiser->get(['limit' => 1, 'depth' => 0]);
138+
} catch (TotpRequiredException $e) {
139+
$totpException = true;
140+
}
109141

142+
$this->assertTrue($totpException);
143+
$this->assertEquals('TOTP_TOKEN', $store->getToken());
110144
}
111145

112146
private function getValidEmptyResponse()
113147
{
114148
return new Response(200, [], Stream::factory("[]"));
115149
}
116150

117-
private function getValidLoginResponse()
151+
private function getValidLoginResponse($totp = false)
118152
{
119-
$loginBody = '{"user":{"id":1},"resource":"auth_token","totp_required":false,' .
120-
'"message":"atomx (user id 1) logged in","success":true,"auth_token":"LOGIN_TOKEN"}';
153+
$token = $totp ? 'TOTP_TOKEN' : 'LOGIN_TOKEN';
154+
$loginBody = '{"user":{"id":1},"resource":"auth_token","totp_required":' . var_export($totp, true) . ',' .
155+
'"message":"atomx (user id 1) logged in","success":true,"auth_token":"' . $token . '"}';
121156

122157
return new Response(200,
123158
['Content-Type' => 'application/json; charset=UTF-8'],

0 commit comments

Comments
 (0)