diff options
Diffstat (limited to 'test/unit/TransmissionTest.php')
-rw-r--r-- | test/unit/TransmissionTest.php | 225 |
1 files changed, 88 insertions, 137 deletions
diff --git a/test/unit/TransmissionTest.php b/test/unit/TransmissionTest.php index ca30b4a..95fabe4 100644 --- a/test/unit/TransmissionTest.php +++ b/test/unit/TransmissionTest.php @@ -2,143 +2,94 @@ namespace SparkPost\Test; use SparkPost\Transmission; -use SparkPost\SparkPost; -use Guzzle\Plugin\Mock\MockPlugin; -use Guzzle\Http\Message\Response; - +use SparkPost\Test\TestUtils\ClassUtils; +use \Mockery; class TransmissionTest extends \PHPUnit_Framework_TestCase { - - private $client = null; - - /** - * Allows access to private methods in the Transmission class - * - * This is needed to mock the GuzzleHttp\Client responses - * - * @param string $name - * @return ReflectionMethod - */ - private static function getMethod($name) { - $class = new \ReflectionClass('\SparkPost\Transmission'); - $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 - } - - /** - * @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 testAllWithGoodResponse() { - $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 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 resource does not exist - */ - public function testFindWith404Response() { - $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 Transmissions API: 400 - */ - public function testFindWithOtherBadResponse() { - $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 = 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 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')); - } - + + private static $utils; + private $adapterMock; + private $resource; + + /** + * (non-PHPdoc) + * @before + * @see PHPUnit_Framework_TestCase::setUp() + */ + public function setUp() { + $this->adapterMock = Mockery::mock('Ivory\HttpAdapter\HttpAdapterInterface', function($mock) { + $mock->shouldReceive('setConfiguration'); + $mock->shouldReceive('getConfiguration->getUserAgent')->andReturn('php-sparkpost/0.2.0'); + }); + $this->resource = new Transmission($this->adapterMock, ['key'=>'a key']); + self::$utils = new ClassUtils($this->resource); + + self::$utils->setProperty($this->resource, 'httpAdapter', $this->adapterMock); + } + + public function tearDown() + { + Mockery::close(); + } + + public function testConstructorSetsUpAdapterAndConfig() { + $adapter = self::$utils->getProperty($this->resource, 'httpAdapter'); + $this->assertRegExp('/php-sparkpost.*/', $adapter->getConfiguration()->getUserAgent()); + } + + + public function testSend() { + $responseMock = Mockery::mock(); + $body = ['text'=>'awesomesauce', 'content'=>['subject'=>'awesomeness']]; + $responseBody = ["results"=>"yay"]; + $this->adapterMock->shouldReceive('send')-> + once()-> + with('/.*\/transmissions/', 'POST', Mockery::type('array'), Mockery::type('string'))-> + andReturn($responseMock); + + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); + + $this->assertEquals($responseBody, $this->resource->send($body)); + } + + public function testAllWithFilter() { + $responseMock = Mockery::mock(); + $responseBody = ["results"=>"yay"]; + $this->adapterMock->shouldReceive('send')-> + once()-> + with('/.*transmissions.*?campaign_id=campaign&template_id=template/', 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); + + $this->assertEquals($responseBody, $this->resource->all('campaign', 'template')); + } + + public function testAllWithOutFilter() { + $responseMock = Mockery::mock(); + $responseBody = ["results"=>"yay"]; + $this->adapterMock->shouldReceive('send')-> + once()-> + with('/.*\/transmissions/', 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); + + $this->assertEquals($responseBody, $this->resource->all()); + } + + public function testFind() { + $responseMock = Mockery::mock(); + $responseBody = ["results"=>"yay"]; + $this->adapterMock->shouldReceive('send')-> + once()-> + with('/.*\/transmissions.*\/test/', 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); + + $this->assertEquals($responseBody, $this->resource->find('test')); + } + } -?>
\ No newline at end of file +?> |