Skip to content

Commit 6e4de62

Browse files
committed
add XApiClient and Handler spec examples
1 parent 1d1bdd9 commit 6e4de62

File tree

4 files changed

+171
-118
lines changed

4 files changed

+171
-118
lines changed

spec/Request/HandlerSpec.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace spec\Xabbuh\XApi\Client\Request;
4+
5+
use Guzzle\Http\Message\RequestInterface;
6+
use Guzzle\Http\Message\Response;
7+
use Guzzle\Service\ClientInterface;
8+
use PhpSpec\ObjectBehavior;
9+
10+
class HandlerSpec extends ObjectBehavior
11+
{
12+
function let(ClientInterface $client)
13+
{
14+
$this->beConstructedWith($client, '1.0.1');
15+
}
16+
17+
function it_throws_an_exception_if_a_request_is_created_with_an_invalid_method()
18+
{
19+
$this->shouldThrow('\InvalidArgumentException')->during('createRequest', array('options', '/xapi/statements'));
20+
}
21+
22+
function it_sets_the_experience_api_version_header_and_the_content_type_header_when_creating_a_request(ClientInterface $client, RequestInterface $request)
23+
{
24+
$client->get('/xapi/statements')->willReturn($request);
25+
$request->addHeader('X-Experience-API-Version', '1.0.1')->shouldBeCalled();
26+
$request->addHeader('Content-Type', 'application/json')->shouldBeCalled();
27+
$request->setAuth(null, null)->shouldBeCalled();
28+
29+
$this->createRequest('get', '/xapi/statements');
30+
}
31+
32+
function it_returns_get_request_created_by_the_http_client(ClientInterface $client, RequestInterface $request)
33+
{
34+
$client->get('/xapi/statements')->willReturn($request);
35+
36+
$this->createRequest('get', '/xapi/statements')->shouldReturn($request);
37+
}
38+
39+
function it_returns_post_request_created_by_the_http_client(ClientInterface $client, RequestInterface $request)
40+
{
41+
$client->post('/xapi/statements', null, 'body')->willReturn($request);
42+
43+
$this->createRequest('post', '/xapi/statements', array(), 'body')->shouldReturn($request);
44+
}
45+
46+
function it_returns_put_request_created_by_the_http_client(ClientInterface $client, RequestInterface $request)
47+
{
48+
$client->put('/xapi/statements', null, 'body')->willReturn($request);
49+
50+
$this->createRequest('put', '/xapi/statements', array(), 'body')->shouldReturn($request);
51+
}
52+
53+
function it_returns_delete_request_created_by_the_http_client(ClientInterface $client, RequestInterface $request)
54+
{
55+
$client->delete('/xapi/statements')->willReturn($request);
56+
57+
$this->createRequest('delete', '/xapi/statements')->shouldReturn($request);
58+
}
59+
60+
function it_throws_an_access_denied_exception_when_a_401_status_code_is_returned(RequestInterface $request, Response $response)
61+
{
62+
$request->send()->willReturn($response);
63+
$response->getStatusCode()->willReturn(401);
64+
$response->getBody(true)->willReturn('body');
65+
66+
$this->shouldThrow('Xabbuh\XApi\Common\Exception\AccessDeniedException')->during('executeRequest', array($request, array(200)));
67+
}
68+
69+
function it_throws_an_access_denied_exception_when_a_403_status_code_is_returned(RequestInterface $request, Response $response)
70+
{
71+
$request->send()->willReturn($response);
72+
$response->getStatusCode()->willReturn(403);
73+
$response->getBody(true)->willReturn('body');
74+
75+
$this->shouldThrow('Xabbuh\XApi\Common\Exception\AccessDeniedException')->during('executeRequest', array($request, array(200)));
76+
}
77+
78+
function it_throws_a_not_found_exception_when_a_404_status_code_is_returned(RequestInterface $request, Response $response)
79+
{
80+
$request->send()->willReturn($response);
81+
$response->getStatusCode()->willReturn(404);
82+
$response->getBody(true)->willReturn('body');
83+
84+
$this->shouldThrow('Xabbuh\XApi\Common\Exception\NotFoundException')->during('executeRequest', array($request, array(200)));
85+
}
86+
87+
function it_throws_a_conflict_exception_when_a_409_status_code_is_returned(RequestInterface $request, Response $response)
88+
{
89+
$request->send()->willReturn($response);
90+
$response->getStatusCode()->willReturn(409);
91+
$response->getBody(true)->willReturn('body');
92+
93+
$this->shouldThrow('Xabbuh\XApi\Common\Exception\ConflictException')->during('executeRequest', array($request, array(200)));
94+
}
95+
96+
function it_throws_an_xapi_exception_when_an_unexpected_status_code_is_returned(RequestInterface $request, Response $response)
97+
{
98+
$request->send()->willReturn($response);
99+
$response->getStatusCode()->willReturn(204);
100+
$response->getBody(true)->willReturn('body');
101+
102+
$this->shouldThrow('Xabbuh\XApi\Common\Exception\XApiException')->during('executeRequest', array($request, array(200)));
103+
}
104+
105+
function it_returns_the_response_on_success(RequestInterface $request, Response $response)
106+
{
107+
$request->send()->willReturn($response);
108+
$response->getStatusCode()->willReturn(200);
109+
$response->getBody(true)->willReturn('body');
110+
111+
$this->executeRequest($request, array(200))->shouldReturn($response);
112+
}
113+
}

spec/XApiClientBuilderSpec.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ function it_creates_an_xapi_client()
1717
$this->build()->shouldHaveType('Xabbuh\XApi\Client\XApiClientInterface');
1818
}
1919

20+
function its_methods_can_be_chained()
21+
{
22+
$this->setBaseUrl('http://example.com/xapi/')->shouldReturn($this);
23+
$this->setVersion('1.0.0')->shouldReturn($this);
24+
$this->setAuth('foo', 'bar')->shouldReturn($this);
25+
$this->setOAuthCredentials('consumer key', 'consumer secret', 'token', 'token secret')->shouldReturn($this);
26+
}
27+
2028
function it_throws_an_exception_if_the_base_uri_is_not_configured()
2129
{
2230
$this->shouldThrow('\LogicException')->during('build');

spec/XApiClientSpec.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace spec\Xabbuh\XApi\Client;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Xabbuh\XApi\Client\Request\HandlerInterface;
7+
use Xabbuh\XApi\Serializer\ActorSerializerInterface;
8+
use Xabbuh\XApi\Serializer\DocumentDataSerializerInterface;
9+
use Xabbuh\XApi\Serializer\SerializerRegistryInterface;
10+
use Xabbuh\XApi\Serializer\StatementResultSerializerInterface;
11+
use Xabbuh\XApi\Serializer\StatementSerializerInterface;
12+
13+
class XApiClientSpec extends ObjectBehavior
14+
{
15+
function let(
16+
HandlerInterface $requestHandler,
17+
SerializerRegistryInterface $serializerRegistry,
18+
ActorSerializerInterface $actorSerializer,
19+
DocumentDataSerializerInterface $documentDataSerializer,
20+
StatementSerializerInterface $statementSerializer,
21+
StatementResultSerializerInterface $statementResultSerializer
22+
) {
23+
$serializerRegistry->getActorSerializer()->willReturn($actorSerializer);
24+
$serializerRegistry->getDocumentDataSerializer()->willReturn($documentDataSerializer);
25+
$serializerRegistry->getStatementSerializer()->willReturn($statementSerializer);
26+
$serializerRegistry->getStatementResultSerializer()->willReturn($statementResultSerializer);
27+
28+
$this->beConstructedWith($requestHandler, $serializerRegistry, '1.0.1');
29+
}
30+
31+
function it_returns_a_statements_api_client_instance()
32+
{
33+
$this->getStatementsApiClient()->shouldBeAnInstanceOf('\Xabbuh\XApi\Client\Api\StatementsApiClientInterface');
34+
}
35+
36+
function it_returns_an_activity_profile_api_client_instance()
37+
{
38+
$this->getActivityProfileApiClient()->shouldBeAnInstanceOf('\Xabbuh\XApi\Client\Api\ActivityProfileApiClientInterface');
39+
}
40+
41+
function it_returns_an_agent_profile_api_client_instance()
42+
{
43+
$this->getAgentProfileApiClient()->shouldBeAnInstanceOf('\Xabbuh\XApi\Client\Api\AgentProfileApiClientInterface');
44+
}
45+
46+
function it_returns_a_state_api_client_instance()
47+
{
48+
$this->getStateApiClient()->shouldBeAnInstanceOf('\Xabbuh\XApi\Client\Api\StateApiClientInterface');
49+
}
50+
}

tests/XApiClientTest.php

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

0 commit comments

Comments
 (0)