summaryrefslogtreecommitdiffstats
path: root/test/unit/APIResourceTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit/APIResourceTest.php')
-rw-r--r--test/unit/APIResourceTest.php267
1 files changed, 129 insertions, 138 deletions
diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php
index fc85fa3..8253b1e 100644
--- a/test/unit/APIResourceTest.php
+++ b/test/unit/APIResourceTest.php
@@ -1,144 +1,135 @@
<?php
namespace SparkPost\Test;
-
use SparkPost\APIResource;
-use SparkPost\SparkPost;
-use Guzzle\Plugin\Mock\MockPlugin;
-use Guzzle\Http\Message\Response;
-
+use SparkPost\Test\TestUtils\ClassUtils;
+use \Mockery;
class APIResourceTest extends \PHPUnit_Framework_TestCase {
-
- private $client = null;
-
- /**
- * Allows access to private methods
- *
- * This is needed to mock the GuzzleHttp\Client responses
- *
- * @param string $name
- * @return ReflectionMethod
- */
- private static function getMethod($name) {
- $class = new \ReflectionClass('\SparkPost\APIResource');
- $method = $class->getMethod($name);
- $method->setAccessible(true);
- return $method;
- }
-
- /**
- * (non-PHPdoc)
- * @before
- * @see PHPUnit_Framework_TestCase::setUp()
- */
- public function setUp() {
- SparkPost::setConfig(array('key'=>'blah'));
- $this->client = self::getMethod('getHttpClient')->invoke(null); //so we can bootstrap api responses
- APIResource::$endpoint = 'someValidEndpoint'; // when using APIResource directly an endpoint needs to be set.
- }
-
- /**
- * @desc Ensures that the configuration class is not instantiable.
- */
- public function testConstructorCannotBeCalled() {
- $class = new \ReflectionClass('\SparkPost\Transmission');
- $this->assertFalse($class->isInstantiable());
- }
-
- /**
- * @desc tests happy path
- */
- public function testFetchWithGoodResponse() {
- $mock = new MockPlugin();
- $mock->addResponse(new Response(200, array(), '{"results":[{"test":"This is a test"}, {"test":"two"}]}'));
- $this->client->addSubscriber($mock);
- $this->assertEquals(array("results"=>array(array('test'=>'This is a test'), array('test'=>'two'))), APIResource::fetchResource());
- }
-
- /**
- * @desc tests happy path
- */
- public function testDeleteWithGoodResponse() {
- $mock = new MockPlugin();
- $mock->addResponse(new Response(200, array(), '{"results":[{"test":"This is a test"}]}'));
- $this->client->addSubscriber($mock);
-
- $this->assertEquals(array("results"=>array(array('test'=>'This is a test'))), APIResource::deleteResource('someId'));
- }
-
- /**
- * @desc tests 404 bad response
- * @expectedException Exception
- * @expectedExceptionMessage The specified resource does not exist
- */
- public function testFetchWith404Response() {
- $mock = new MockPlugin();
- $mock->addResponse(new Response(404, array()));
- $this->client->addSubscriber($mock);
- APIResource::fetchResource('someId');
- }
-
- /**
- * @desc tests unknown bad response
- * @expectedException Exception
- * @expectedExceptionMessage Received bad response from SomeValidEndpoint API: 400
- */
- public function testFetchWithOtherBadResponse() {
- $mock = new MockPlugin();
- $mock->addResponse(new Response(400, array()));
- $this->client->addSubscriber($mock);
- APIResource::fetchResource('someId');
- }
-
- /**
- * @desc tests bad response
- * @expectedException Exception
- * @expectedExceptionMessageRegExp /Unable to contact SomeValidEndpoint API:.* /
- */
- public function testFetchForCatchAllException() {
- $mock = new MockPlugin();
- $mock->addResponse(new Response(500));
- $this->client->addSubscriber($mock);
- APIResource::fetchResource('someId');
- }
-
- /**
- * @desc tests happy path
- */
- public function testSuccessfulSend() {
- $body = array("result"=>array("transmission_id"=>"11668787484950529"), "status"=>array("message"=> "ok","code"=> "1000"));
- $mock = new MockPlugin();
- $mock->addResponse(new Response(200, array(), json_encode($body)));
- $this->client->addSubscriber($mock);
-
-
- $this->assertEquals($body, APIResource::sendRequest(array('text'=>'awesome email')));
- }
-
- /**
- * @desc tests bad response
- * @expectedException Exception
- * @expectedExceptionMessage ["This is a fake error"]
- */
- public function testSendFor400Exception() {
- $body = array('errors'=>array('This is a fake error'));
- $mock = new MockPlugin();
- $mock->addResponse(new Response(400, array(), json_encode($body)));
- $this->client->addSubscriber($mock);
- APIResource::sendRequest(array('text'=>'awesome email'));
- }
-
-
- /**
- * @desc tests bad response
- * @expectedException Exception
- * @expectedExceptionMessageRegExp /Unable to contact SomeValidEndpoint API:.* /
- */
- public function testSendForCatchAllException() {
- $mock = new MockPlugin();
- $mock->addResponse(new Response(500));
- $this->client->addSubscriber($mock);
- APIResource::sendRequest(array('text'=>'awesome email'));
- }
-
+
+ private static $utils;
+ private $adapterMock;
+ private $resource;
+
+ private function getExceptionMock($statusCode) {
+ $exception = new \Ivory\HttpAdapter\HttpAdapterException();
+ $response = Mockery::mock('Ivory\HttpAdapter\Message\ResponseInterface');
+ $response->shouldReceive('getStatusCode')->andReturn($statusCode);
+ $exception->setResponse($response);
+ return $exception;
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @before
+ * @see PHPUnit_Framework_TestCase::setUp()
+ */
+ public function setUp() {
+ $this->sparkPostMock = Mockery::mock('SparkPost\SparkPost', function($mock) {
+ $mock->shouldReceive('getHttpHeaders')->andReturn([]);
+ });
+ $this->sparkPostMock->httpAdapter = Mockery::mock();
+ $this->resource = new APIResource($this->sparkPostMock);
+ self::$utils = new ClassUtils($this->resource);
+ self::$utils->setProperty($this->resource, 'sparkpost', $this->sparkPostMock);
+ }
+
+ public function tearDown()
+ {
+ Mockery::close();
+ }
+
+ public function testConstructorSetsUpSparkPostObject() {
+ $this->sparkPostMock->newProp = 'new value';
+ $this->assertEquals($this->sparkPostMock, self::$utils->getProperty($this->resource, 'sparkpost'));
+ }
+
+ public function testCreate() {
+ $testInput = ['test'=>'body'];
+ $testBody = ['results'=>['my'=>'test']];
+ $responseMock = Mockery::mock();
+ $this->sparkPostMock->httpAdapter->shouldReceive('send')->
+ once()->
+ with(Mockery::type('string'), 'POST', Mockery::type('array'), json_encode($testInput))->
+ andReturn($responseMock);
+ $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
+
+
+ $this->assertEquals($testBody, $this->resource->create($testInput));
+ }
+
+ public function testUpdate() {
+ $testInput = ['test'=>'body'];
+ $testBody = ['results'=>['my'=>'test']];
+ $responseMock = Mockery::mock();
+ $this->sparkPostMock->httpAdapter->shouldReceive('send')->
+ once()->
+ with('/.*\/test/', 'PUT', Mockery::type('array'), json_encode($testInput))->
+ andReturn($responseMock);
+ $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
+
+ $this->assertEquals($testBody, $this->resource->update('test', $testInput));
+ }
+
+ public function testGet() {
+ $testBody = ['results'=>['my'=>'test']];
+ $responseMock = Mockery::mock();
+ $this->sparkPostMock->httpAdapter->shouldReceive('send')->
+ once()->
+ with('/.*\/test/', 'GET', Mockery::type('array'), null)->
+ andReturn($responseMock);
+ $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
+
+ $this->assertEquals($testBody, $this->resource->get('test'));
+ }
+
+ public function testDelete() {
+ $responseMock = Mockery::mock();
+ $this->sparkPostMock->httpAdapter->shouldReceive('send')->
+ once()->
+ with('/.*\/test/', 'DELETE', Mockery::type('array'), null)->
+ andReturn($responseMock);
+ $responseMock->shouldReceive('getBody->getContents')->andReturn('');
+
+ $this->assertEquals(null, $this->resource->delete('test'));
+ }
+
+ public function testAdapter404Exception() {
+ try {
+ $this->sparkPostMock->httpAdapter->shouldReceive('send')->
+ once()->
+ andThrow($this->getExceptionMock(404));
+
+ $this->resource->get('test');
+ }
+ catch(\Exception $e) {
+ $this->assertRegExp('/.*resource does not exist.*/', $e->getMessage());
+ }
+ }
+
+ public function testAdapter4XXException() {
+ try {
+ $this->sparkPostMock->httpAdapter->shouldReceive('send')->
+ once()->
+ andThrow($this->getExceptionMock(400));
+
+ $this->resource->get('test');
+ }
+ catch(\Exception $e) {
+ $this->assertRegExp('/Received bad response.*/', $e->getMessage());
+ }
+ }
+
+ public function testAdapter5XXException() {
+ try {
+ $this->sparkPostMock->httpAdapter->shouldReceive('send')->
+ once()->
+ andThrow(new \Exception('Something went wrong.'));
+
+ $this->resource->get('test');
+ }
+ catch(\Exception $e) {
+ $this->assertRegExp('/Unable to contact.*API.*/', $e->getMessage());
+ }
+ }
+
}