Skip to content

Commit

Permalink
Cover API lines
Browse files Browse the repository at this point in the history
  • Loading branch information
ovac committed Aug 13, 2017
1 parent a06551b commit 1724c37
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
9 changes: 2 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function execute($httpMethod, $url, array $parameters = [])

return json_decode((string) $response->getBody(), true);
} catch (RequestInterface $e) {
new ClientException($e->getMessage(), $e);
throw new ClientException($e->getMessage(), $e);
}

return;
Expand Down
59 changes: 42 additions & 17 deletions tests/Unit/Api/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,35 @@ public function test_api_execute_methods_requires_config()

public function test_api_execute_throws_ClientException_is_server_gives_error()
{
$this->expectException(ClientException::class);
// $this->expectException(ClientException::class);

$httpMock = new MockHandler([
new Response(401, ['X-Foo' => 'Bar']),
]);
try {
$httpMock = new MockHandler([
new Response(401, ['X-Foo' => 'Bar']),
]);

$handler = HandlerStack::create($httpMock);
$handler = HandlerStack::create($httpMock);

$path = '/some_path';
$parameters = ['hello' => 'world'];
$path = '/some_path';
$parameters = ['hello' => 'world'];

$mock = $this->getMockBuilder(Api::class)
->setConstructorArgs([$this->config])
->setMethods(['createHandler'])
->getMockForAbstractClass();
$mock = $this->getMockBuilder(Api::class)
->setConstructorArgs([$this->config])
->setMethods(['createHandler'])
->getMockForAbstractClass();

$mock->expects($this->once())
->method('createHandler')
->will($this->returnValue($handler));
$mock->expects($this->once())
->method('createHandler')
->will($this->returnValue($handler));

$mock->_get($path, $parameters);
$mock->_get($path, $parameters);
} catch (ClientException $e) {
$this->assertTrue(!!$e);

return;
}

$this->fail();
}

public function test_api_execute_test_successful_call()
Expand Down Expand Up @@ -184,8 +192,25 @@ public function test_api_execute_test_successful_call()
foreach ($container as $transaction) {
$this->assertSame($transaction['request']->getMethod(), 'GET');
$this->assertSame($transaction['response']->getStatusCode(), 200);

var_dump($transaction['options']);
}
}

public static function callProtectedMethod($object, $method, array $args = array())
{
$class = new \ReflectionClass(get_class($object));
$method = $class->getMethod($method);
$method->setAccessible(true);
return $method->invokeArgs($object, $args);
}

public function test_api_createHandler_method()
{
$mock = $this->getMockBuilder(Api::class)
->setConstructorArgs([$this->config])
->getMockForAbstractClass();

$handlerStack = self::callProtectedMethod($mock, 'createHandler');

self::assertInstanceOf(HandlerStack::class, $handlerStack);
}
}

0 comments on commit 1724c37

Please sign in to comment.