diff options
Diffstat (limited to 'test/unit/TransmissionTest.php')
-rw-r--r-- | test/unit/TransmissionTest.php | 118 |
1 files changed, 68 insertions, 50 deletions
diff --git a/test/unit/TransmissionTest.php b/test/unit/TransmissionTest.php index ae7d59d..de72f2a 100644 --- a/test/unit/TransmissionTest.php +++ b/test/unit/TransmissionTest.php @@ -1,11 +1,10 @@ <?php namespace SparkPost\Test; -use MessageSystems\Transmission; -use MessageSystems\SparkPost; -use GuzzleHttp\Subscriber\Mock; -use GuzzleHttp\Message\Response; -use GuzzleHttp\Stream\Stream; +use SparkPost\Transmission; +use SparkPost\SparkPost; +use Guzzle\Plugin\Mock\MockPlugin; +use Guzzle\Http\Message\Response; class TransmissionTest extends \PHPUnit_Framework_TestCase { @@ -21,7 +20,7 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { * @return ReflectionMethod */ private static function getMethod($name) { - $class = new \ReflectionClass('\MessageSystems\Transmission'); + $class = new \ReflectionClass('\SparkPost\Transmission'); $method = $class->getMethod($name); $method->setAccessible(true); return $method; @@ -33,7 +32,7 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { * @see PHPUnit_Framework_TestCase::setUp() */ public function setUp() { - SparkPost::setConfig(['key'=>'blah']); + SparkPost::setConfig(array('key'=>'blah')); $this->client = self::getMethod('getHttpClient')->invoke(null); //so we can bootstrap api responses } @@ -41,7 +40,7 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { * @desc Ensures that the configuration class is not instantiable. */ public function testConstructorCannotBeCalled() { - $class = new \ReflectionClass('\MessageSystems\Transmission'); + $class = new \ReflectionClass('\SparkPost\Transmission'); $this->assertFalse($class->isInstantiable()); } @@ -49,77 +48,96 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { * @desc tests happy path */ public function testAllWithGoodResponse() { - $mock = new Mock([new Response(200, [], Stream::factory('{"results":[{"test":"This is a test"}, {"test":"two"}]}'))]); - $this->client->getEmitter()->attach($mock); - $this->assertEquals(["results"=>[['test'=>'This is a test'], ['test'=>'two']]], Transmission::all()); - $this->client->getEmitter()->detach($mock); + $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'))), Transmission::all()); } /** * @desc tests happy path */ public function testFindWithGoodResponse() { - $mock = new Mock([new Response(200, [], Stream::factory('{"results":[{"test":"This is a test"}]}'))]); - $this->client->getEmitter()->attach($mock); - $this->assertEquals(["results"=>[['test'=>'This is a test']]], Transmission::find('someId')); - $this->client->getEmitter()->detach($mock); + $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'))), Transmission::find('someId')); } /** * @desc tests 404 bad response + * @expectedException Exception + * @expectedExceptionMessage The specified Transmission ID does not exist */ public function testFindWith404Response() { - $mock = new Mock([new Response(404, [])]); - $this->client->getEmitter()->attach($mock); - try { - Transmission::find('someId'); - } catch (\Exception $e) { - $this->assertEquals('The specified Transmission ID does not exist', $e->getMessage()); - } finally { - $this->client->getEmitter()->detach($mock); - } + $mock = new MockPlugin(); + $mock->addResponse(new Response(404, array())); + $this->client->addSubscriber($mock); + Transmission::find('someId'); } /** * @desc tests unknown bad response + * @expectedException Exception + * @expectedExceptionMessage Received bad response from Transmission API: 400 */ public function testFindWithOtherBadResponse() { - $mock = new Mock([new Response(400, [])]); - $this->client->getEmitter()->attach($mock); - try { - Transmission::find('someId'); - } catch (\Exception $e) { - $this->assertEquals('Received bad response from Transmission API: 400', $e->getMessage()); - } finally { - $this->client->getEmitter()->detach($mock); - } + $mock = new MockPlugin(); + $mock->addResponse(new Response(400, array())); + $this->client->addSubscriber($mock); + Transmission::find('someId'); + } + + /** + * @desc tests bad response + * @expectedException Exception + * @expectedExceptionMessageRegExp /Unable to contact Transmissions API:.* / + */ + public function testFindForCatchAllException() { + $mock = new MockPlugin(); + $mock->addResponse(new Response(500)); + $this->client->addSubscriber($mock); + Transmission::find('someId'); } /** * @desc tests happy path */ public function testSuccessfulSend() { - $body = ["result"=>["transmission_id"=> "11668787484950529"], "status"=>["message"=> "ok","code"=> "1000"]]; - $mock = new Mock([new Response(200, [], Stream::factory(json_encode($body)))]); - $this->client->getEmitter()->attach($mock); - $this->assertEquals($body, Transmission::send(['text'=>'awesome email'])); - $this->client->getEmitter()->detach($mock); + $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, Transmission::send(array('text'=>'awesome email'))); } /** * @desc tests bad response + * @expectedException Exception + * @expectedExceptionMessage ["This is a fake error"] */ - public function testSendForRequestException() { - $body = ['errors'=>['This is a fake error']]; - $mock = new Mock([new Response(400, [], Stream::factory(json_encode($body)))]); - $this->client->getEmitter()->attach($mock); - try { - Transmission::send(['text'=>'awesome email']); - } catch (\Exception $e) { - $this->assertEquals('["This is a fake error"]', $e->getMessage()); - } finally { - $this->client->getEmitter()->detach($mock); - } + 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); + Transmission::send(array('text'=>'awesome email')); + } + + + /** + * @desc tests bad response + * @expectedException Exception + * @expectedExceptionMessageRegExp /Unable to contact Transmissions API:.* / + */ + public function testSendForCatchAllException() { + $mock = new MockPlugin(); + $mock->addResponse(new Response(500)); + $this->client->addSubscriber($mock); + Transmission::send(array('text'=>'awesome email')); } } |