Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Commit

Permalink
Added exception expectation to CollectionConsumer
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Lamoureux committed Sep 30, 2014
1 parent dcd1bb6 commit a0f8f41
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion tests/unit/src/v2/collection/CollectionConsumerTest.php
Expand Up @@ -7,11 +7,13 @@
*/
namespace Crystalgorithm\DurmandScriptorium\v2\converter;

use Crystalgorithm\DurmandScriptorium\exceptions\BadRequestException;
use Crystalgorithm\DurmandScriptorium\utils\BatchRequestManager;
use Crystalgorithm\DurmandScriptorium\utils\Constants;
use Crystalgorithm\DurmandScriptorium\v2\collection\CollectionConsumer;
use Crystalgorithm\DurmandScriptorium\v2\collection\CollectionRequestFactory;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Message\Response;
use Mockery;
Expand All @@ -21,12 +23,16 @@ class CollectionConsumerTest extends PHPUnit_Framework_TestCase
{

const VALID_ID = 100;
const INVALID_ID = -1;
const NB_PAGE = 2;
const ONE_PAGE = 1;
const VALID_PAGE = 1;
const INVALID_PAGE = 1;
const TEXT = 'text';
const EXCEPTION_MESSAGE = 'error';

/**
* @var ConverterConsumer
* @var CollectionConsumer
*/
protected $consumer;

Expand Down Expand Up @@ -55,13 +61,19 @@ class CollectionConsumerTest extends PHPUnit_Framework_TestCase
*/
protected $response;

/**
* @var ClientException mock
*/
protected $exception;

protected function setUp()
{
$this->client = Mockery::mock('GuzzleHttp\Client');
$this->requestFactory = Mockery::mock('Crystalgorithm\DurmandScriptorium\v2\collection\CollectionRequestFactory');
$this->batchRequestManager = Mockery::mock('Crystalgorithm\DurmandScriptorium\utils\BatchRequestManager');
$this->request = Mockery::mock('GuzzleHttp\Message\Request');
$this->response = Mockery::mock('GuzzleHttp\Message\Response');
$this->exception = Mockery::mock('GuzzleHttp\Exception\ClientException');

$this->consumer = new CollectionConsumer($this->client, $this->requestFactory, $this->batchRequestManager);
}
Expand Down Expand Up @@ -143,4 +155,38 @@ public function testGivenPageThenGet()
$this->consumer->getPage(self::VALID_PAGE);
}

public function testGivenInvalidIdThenThrow()
{
$this->requestFactory->shouldReceive('idRequest')->with(self::INVALID_ID)->once()->andReturn($this->request);
$this->exception->shouldReceive('getResponse')->andReturn($this->response);
$this->response->shouldReceive('json')->andReturn([self::TEXT => self::EXCEPTION_MESSAGE]);
$this->client->shouldReceive('send')->with($this->request)->once()->andThrow($this->exception);

try
{
$this->consumer->get(self::INVALID_ID);
}
catch (BadRequestException $ex)
{
$this->assertEquals(self::EXCEPTION_MESSAGE, $ex->getMessage());
}
}

public function testGivenInvalidPageThenThrow()
{
$this->requestFactory->shouldReceive('pageRequest')->with(self::INVALID_PAGE, null)->once()->andReturn($this->request);
$this->exception->shouldReceive('getResponse')->andReturn($this->response);
$this->response->shouldReceive('json')->andReturn([self::TEXT => self::EXCEPTION_MESSAGE]);
$this->client->shouldReceive('send')->with($this->request)->once()->andThrow($this->exception);

try
{
$this->consumer->getPage(self::INVALID_PAGE);
}
catch (BadRequestException $ex)
{
$this->assertEquals(self::EXCEPTION_MESSAGE, $ex->getMessage());
}
}

}

0 comments on commit a0f8f41

Please sign in to comment.