diff options
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/SendGridCompatibiility/EmailTest.php | 161 | ||||
-rw-r--r-- | test/unit/SparkPostTest.php | 10 | ||||
-rw-r--r-- | test/unit/TransmissionTest.php | 118 |
3 files changed, 234 insertions, 55 deletions
diff --git a/test/unit/SendGridCompatibiility/EmailTest.php b/test/unit/SendGridCompatibiility/EmailTest.php new file mode 100644 index 0000000..c60801c --- /dev/null +++ b/test/unit/SendGridCompatibiility/EmailTest.php @@ -0,0 +1,161 @@ +<?php +use SparkPost\SendGridCompatibility\Email; + +class SendGridCompatibilityEmailTest extends \PHPUnit_Framework_TestCase { + + private $email; + + public function setup() { + $this->email = new Email(); + } + + public function testConstruct() { + $email = new Email(); + + $this->assertInstanceOf('SparkPost\SendGridCompatibility\Email', $email); + $this->assertInternalType('array', $email->model); + } + + public function testAddTo() { + $fakeEmail = 'joe.schmoe@test.com'; + $this->email->addTo($fakeEmail); + + $this->assertEquals(array(array('address'=>array('email'=>$fakeEmail))), $this->email->model['recipients']); + } + + public function testAddToWithName() { + $fakeEmail = 'joe.schmoe@test.com'; + $fakeName = 'Joe Schmoe'; + $this->email->addTo($fakeEmail, $fakeName); + + $this->assertEquals(array(array('address'=>array('email'=>$fakeEmail, 'name'=>$fakeName))), $this->email->model['recipients']); + } + + public function testSetTos() { + $tos = array(); + array_push($tos, array('address'=>array('email'=>'joe.schmoe@test.com', 'name'=>'Joe Schmoe'))); + array_push($tos, array('address'=>array('email'=>'jill.schmoe@test.com', 'name'=>'Jill Schmoe'))); + $this->email->setTos($tos); + + $this->assertEquals($tos, $this->email->model['recipients']); + } + + public function testSetFrom() { + $this->email->setFrom('test@email.com'); + + $this->assertEquals(array('email'=>'test@email.com'), $this->email->model['from']); + } + + + public function testSetFromName() { + $this->email->setFrom('test@email.com'); + $this->email->setFromName('Test Bot'); + + $this->assertEquals(array('email'=>'test@email.com', 'name'=>'Test Bot'), $this->email->model['from']); + } + + /** + * @desc Tests that setting the fromName prior to setting the From field throws an exception + * @expectedException Exception + * @expectedExceptionMessage Must set "From" prior to setting "From Name". + */ + public function testSetFromNameWithoutAddress() { + $this->email->setFromName('Test Bot'); + } + + public function testSetReplyto() { + $this->email->setReplyTo('test@email.com'); + + $this->assertEquals('test@email.com', $this->email->model['replyTo']); + } + /** + * @expectedException Exception + * @expectedExceptionMessage Adding bcc recipients is not yet supported, try adding them as a "to" address + */ + public function testAddBcc() { + $this->email->addBcc('test@email.com'); + } + + public function testSetSubject() { + $this->email->setSubject('Awesome Subject'); + + $this->assertEquals('Awesome Subject', $this->email->model['subject']); + } + + public function testSetText() { + $value = 'This is some plain/text'; + $this->email->setText($value); + + $this->assertEquals($value, $this->email->model['text']); + } + + public function testSetHtml() { + $value = '<html><body><p>This is some html</p></body></html>'; + $this->email->setHtml($value); + + $this->assertEquals($value, $this->email->model['html']); + } + + /** + * @desc test that adding a category throws an exception since we don't support tags at transmission level yet + * @expectedException Exception + * @expectedExceptionMessage Adding categories is not yet supported + */ + public function testAddCategory() { + $this->email->addCategory(''); + } + + /** + * @desc Tests that setting an attachment throws a meaningful exception + * @expectedException Exception + * @expectedExceptionMessage Adding attachments is not yet supported + */ + public function testAddAttachment() { + $this->email->addAttachment('blah'); + } + + public function testAddSubstitution() { + $this->email->addSubstitution('item', 'baseball bat'); + + $this->assertEquals(array('item'=>'baseball bat'), $this->email->model['substitutionData']); + } + + public function testAddSection() { + $this->email->addSection('item', 'baseball bat'); + + $this->assertEquals(array('item'=>'baseball bat'), $this->email->model['substitutionData']); + } + + /** + * @desc Tests that setting an attachment throws a meaningful exception + * @expectedException Exception + * @expectedExceptionMessage Adding Unique Arguments is not yet supported + */ + public function testAddUniqueArguement() { + $this->email->addUniqueArg('blah', 'someblah'); + } + + + /** + * @desc Tests that setting an unique argument throws a meaningful exception + * @expectedException Exception + * @expectedExceptionMessage Setting Unique Arguments is not yet supported + */ + public function testSetUniqueArgs() { + $this->email->setUniqueArgs(array('blah', 'andBlah')); + } + + + public function testAddHeader() { + $value = 'My Header'; + $this->email->addHeader('X-header', $value); + + $this->assertEquals(array('X-header'=>$value), $this->email->model['customHeaders']); + } + + public function testToSparkPostTransmission() { + $this->assertInternalType('array', $this->email->toSparkPostTransmission()); + } +} + +?>
\ No newline at end of file diff --git a/test/unit/SparkPostTest.php b/test/unit/SparkPostTest.php index 1b86a8f..650cb36 100644 --- a/test/unit/SparkPostTest.php +++ b/test/unit/SparkPostTest.php @@ -1,7 +1,7 @@ <?php namespace SparkPost\Test; -use MessageSystems\SparkPost; +use SparkPost\SparkPost; class SparkPostTest extends \PHPUnit_Framework_TestCase { @@ -9,7 +9,7 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase { * @desc Ensures that the configuration class is not instantiable. */ public function testConstructorCannotBeCalled() { - $class = new \ReflectionClass('\MessageSystems\SparkPost'); + $class = new \ReflectionClass('\SparkPost\SparkPost'); $this->assertFalse($class->isInstantiable()); } @@ -29,7 +29,7 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase { * @expectedExceptionMessage You must provide an API key */ public function testSetConfigAPIKeyNotSetException() { - SparkPost::setConfig(['something'=>'other than an API Key']); + SparkPost::setConfig(array('something'=>'other than an API Key')); } /** @@ -38,14 +38,14 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase { * @expectedExceptionMessage You must provide an API key */ public function testSetConfigAPIKeyEmptyException() { - SparkPost::setConfig(['key'=>'']); + SparkPost::setConfig(array('key'=>'')); } /** * @desc Tests overridable values are set while invalid values are ignored */ public function testSetConfigMultipleValuesAndGetConfig() { - SparkPost::setConfig(['key'=>'lala', 'version'=>'v8', 'port'=>1024, 'someOtherValue'=>'fakeValue']); + SparkPost::setConfig(array('key'=>'lala', 'version'=>'v8', 'port'=>1024, 'someOtherValue'=>'fakeValue')); $testConfig = SparkPost::getConfig(); $this->assertEquals('lala', $testConfig['key']); 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')); } } |