diff options
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/APIResourceExceptionTest.php | 61 | ||||
-rw-r--r-- | test/unit/APIResourceTest.php | 340 | ||||
-rw-r--r-- | test/unit/MessageEventTest.php | 115 | ||||
-rw-r--r-- | test/unit/SendGridCompatibiility/EmailTest.php | 331 | ||||
-rw-r--r-- | test/unit/SparkPostTest.php | 125 | ||||
-rw-r--r-- | test/unit/TestUtils/ClassUtils.php | 102 | ||||
-rw-r--r-- | test/unit/TransmissionTest.php | 210 | ||||
-rw-r--r-- | test/unit/bootstrap.php | 4 |
8 files changed, 675 insertions, 613 deletions
diff --git a/test/unit/APIResourceExceptionTest.php b/test/unit/APIResourceExceptionTest.php index 5bb4758..8ae80de 100644 --- a/test/unit/APIResourceExceptionTest.php +++ b/test/unit/APIResourceExceptionTest.php @@ -1,36 +1,43 @@ <?php -namespace SparkPost\Test; -use SparkPost\APIResponseException; -class APIResourceExceptionTest extends \PHPUnit_Framework_TestCase { +namespace SparkPost\Test; - private $message; - private $code; - private $description; - private $exception; +use SparkPost\APIResponseException; - /** - * (non-PHPdoc) - * @before - * @see PHPUnit_Framework_TestCase::setUp() - */ - public function setUp() { - $this->message = 'Test message'; - $this->code = 400; - $this->description = 'Test description'; - $this->exception = new APIResponseException(NULL, 0, $this->message, $this->code, $this->description); - } +class APIResourceExceptionTest extends \PHPUnit_Framework_TestCase +{ + private $message; + private $code; + private $description; + private $exception; - public function testAPIMessage() { - $this->assertEquals($this->message, $this->exception->getAPIMessage()); - } + /** + * (non-PHPdoc). + * + * @before + * + * @see PHPUnit_Framework_TestCase::setUp() + */ + public function setUp() + { + $this->message = 'Test message'; + $this->code = 400; + $this->description = 'Test description'; + $this->exception = new APIResponseException(null, 0, $this->message, $this->code, $this->description); + } - public function testAPICode() { - $this->assertEquals($this->code, $this->exception->getAPICode()); - } + public function testAPIMessage() + { + $this->assertEquals($this->message, $this->exception->getAPIMessage()); + } - public function testAPIDescription() { - $this->assertEquals($this->description, $this->exception->getAPIDescription()); - } + public function testAPICode() + { + $this->assertEquals($this->code, $this->exception->getAPICode()); + } + public function testAPIDescription() + { + $this->assertEquals($this->description, $this->exception->getAPIDescription()); + } } diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php index 8552815..67c37ab 100644 --- a/test/unit/APIResourceTest.php +++ b/test/unit/APIResourceTest.php @@ -1,184 +1,196 @@ <?php + namespace SparkPost\Test; + use SparkPost\APIResource; use SparkPost\Test\TestUtils\ClassUtils; -use \Mockery; - -class APIResourceTest extends \PHPUnit_Framework_TestCase { - - 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() +use Mockery; + +class APIResourceTest extends \PHPUnit_Framework_TestCase +{ + 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('getStatusCode')->andReturn(200); - $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('getStatusCode')->andReturn(200); - $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('getStatusCode')->andReturn(200); - $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); - - $this->assertEquals($testBody, $this->resource->get('test')); - } - - public function testGetCommaSeparated() { - $testBody = ['results'=>['my'=>'test']]; - $requestArray = [ - "param1" => "param1val", - "param2" => ["param2val1", "param2val2"] - ]; - $expectedGetParams = "param1=param1val¶m2=" . urlencode("param2val1,param2val2"); - - $responseMock = Mockery::mock(); - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with(matchesPattern("/.*\/test\?{$expectedGetParams}/"), 'GET', Mockery::type('array'), null)-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(200); - $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); - - $this->assertEquals($testBody, $this->resource->get('test', $requestArray)); - } - - public function testDelete() { - $responseMock = Mockery::mock(); - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with('/.*\/test/', 'DELETE', Mockery::type('array'), null)-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(200); - $responseMock->shouldReceive('getBody->getContents')->andReturn(''); - - $this->assertEquals(null, $this->resource->delete('test')); - } - - public function testAdapter404Exception() { - try { - $responseMock = Mockery::mock(); - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(404); - - $this->resource->get('test'); + public function testConstructorSetsUpSparkPostObject() + { + $this->sparkPostMock->newProp = 'new value'; + $this->assertEquals($this->sparkPostMock, self::$utils->getProperty($this->resource, 'sparkpost')); } - catch(\Exception $e) { - $this->assertRegExp('/.*resource does not exist.*/', $e->getMessage()); + + 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('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); + + $this->assertEquals($testBody, $this->resource->create($testInput)); } - } - - public function testAdapter403Exception() { - $testBody = [ 'errors' => [ - [ - 'message' => 'Forbidden.' - ] - ]]; - try { - $responseMock = Mockery::mock(); - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(403); - $responseMock->shouldReceive('getBody')->andReturn(json_encode($testBody)); - - $this->resource->get('test'); + + 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('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); + + $this->assertEquals($testBody, $this->resource->update('test', $testInput)); } - catch(\Exception $e) { - $this->assertRegExp('/Request forbidden/', $e->getMessage()); + + 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('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); + + $this->assertEquals($testBody, $this->resource->get('test')); } - } - - public function testAdapter4XXException() { - try { - $testBody = ['errors'=>['my'=>'test']]; - $responseMock = Mockery::mock(); - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(400); - $responseMock->shouldReceive('getBody')->andReturn(json_encode($testBody)); - - $this->resource->get('test'); + + public function testGetCommaSeparated() + { + $testBody = ['results' => ['my' => 'test']]; + $requestArray = [ + 'param1' => 'param1val', + 'param2' => ['param2val1', 'param2val2'], + ]; + $expectedGetParams = 'param1=param1val¶m2='.urlencode('param2val1,param2val2'); + + $responseMock = Mockery::mock(); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with(matchesPattern("/.*\/test\?{$expectedGetParams}/"), 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); + + $this->assertEquals($testBody, $this->resource->get('test', $requestArray)); } - catch(\Exception $e) { - $this->assertRegExp('/Received bad response.*/', $e->getMessage()); + + public function testDelete() + { + $responseMock = Mockery::mock(); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/.*\/test/', 'DELETE', Mockery::type('array'), null)-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody->getContents')->andReturn(''); + + $this->assertEquals(null, $this->resource->delete('test')); } - } - public function testAdapter5XXException() { - try { - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - andThrow(new \Exception('Something went wrong.')); + public function testAdapter404Exception() + { + try { + $responseMock = Mockery::mock(); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(404); + + $this->resource->get('test'); + } catch (\Exception $e) { + $this->assertRegExp('/.*resource does not exist.*/', $e->getMessage()); + } + } - $this->resource->get('test'); + public function testAdapter403Exception() + { + $testBody = ['errors' => [ + [ + 'message' => 'Forbidden.', + ], + ]]; + try { + $responseMock = Mockery::mock(); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(403); + $responseMock->shouldReceive('getBody')->andReturn(json_encode($testBody)); + + $this->resource->get('test'); + } catch (\Exception $e) { + $this->assertRegExp('/Request forbidden/', $e->getMessage()); + } } - catch(\Exception $e) { - $this->assertRegExp('/Unable to contact.*API.*/', $e->getMessage()); + + public function testAdapter4XXException() + { + try { + $testBody = ['errors' => ['my' => 'test']]; + $responseMock = Mockery::mock(); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(400); + $responseMock->shouldReceive('getBody')->andReturn(json_encode($testBody)); + + $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()); + } + } } diff --git a/test/unit/MessageEventTest.php b/test/unit/MessageEventTest.php index 48657e6..3d92412 100644 --- a/test/unit/MessageEventTest.php +++ b/test/unit/MessageEventTest.php @@ -2,70 +2,73 @@ namespace SparkPost; -use \Mockery; - +use Mockery; class MessageEventTest extends \PHPUnit_Framework_TestCase { - private $sparkPostMock; - private $sut; + private $sparkPostMock; + private $sut; - /** - * (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->sut = new MessageEvents($this->sparkPostMock); - } + /** + * (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->sut = new MessageEvents($this->sparkPostMock); + } - public function testDateTimeConversion() - { - $testBody = ['results' => ['my' => 'test']]; - $testFrom = new \DateTime("1978-08-27 04:05:02"); - $testFromStr = urlencode("1978-08-27T04:05"); - $testTo = new \DateTime("2016-04-04 19:00"); - $testToStr = urlencode("2016-04-04T19:00"); + public function testDateTimeConversion() + { + $testBody = ['results' => ['my' => 'test']]; + $testFrom = new \DateTime('1978-08-27 04:05:02'); + $testFromStr = urlencode('1978-08-27T04:05'); + $testTo = new \DateTime('2016-04-04 19:00'); + $testToStr = urlencode('2016-04-04T19:00'); - $responseMock = Mockery::mock(); - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with("/message-events/?from={$testFromStr}&to={$testToStr}", 'GET', Mockery::type('array'), null)-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(200); - $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); + $responseMock = Mockery::mock(); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with("/message-events/?from={$testFromStr}&to={$testToStr}", 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); - $this->assertEquals($testBody, $this->sut->search(["from" => $testFrom, "to" => $testTo])); - } + $this->assertEquals($testBody, $this->sut->search(['from' => $testFrom, 'to' => $testTo])); + } - public function testDocumentation() { - $testBody = ['results' => ['my' => 'test']]; - $responseMock = Mockery::mock(); - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with("/message-events/events/documentation", 'GET', Mockery::type('array'), null)-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(200); - $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); + public function testDocumentation() + { + $testBody = ['results' => ['my' => 'test']]; + $responseMock = Mockery::mock(); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/message-events/events/documentation', 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); - $this->assertEquals($testBody, $this->sut->documentation()); - } + $this->assertEquals($testBody, $this->sut->documentation()); + } - public function testSamples() { - $testBody = ['results' => ['my' => 'test']]; - $responseMock = Mockery::mock(); - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with("/message-events/events/samples?events=".urlencode("delivery,bounce"), 'GET', Mockery::type('array'), null)-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(200); - $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); + public function testSamples() + { + $testBody = ['results' => ['my' => 'test']]; + $responseMock = Mockery::mock(); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/message-events/events/samples?events='.urlencode('delivery,bounce'), 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); - $this->assertEquals($testBody, $this->sut->samples(["delivery", "bounce"])); - } -}
\ No newline at end of file + $this->assertEquals($testBody, $this->sut->samples(['delivery', 'bounce'])); + } +} diff --git a/test/unit/SendGridCompatibiility/EmailTest.php b/test/unit/SendGridCompatibiility/EmailTest.php index 2b86d7a..2846e9e 100644 --- a/test/unit/SendGridCompatibiility/EmailTest.php +++ b/test/unit/SendGridCompatibiility/EmailTest.php @@ -1,161 +1,178 @@ <?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']); - } +use SparkPost\SendGridCompatibility\Email; - /** - * @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()); - } +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()); + } } - -?> diff --git a/test/unit/SparkPostTest.php b/test/unit/SparkPostTest.php index ed10b55..26563c3 100644 --- a/test/unit/SparkPostTest.php +++ b/test/unit/SparkPostTest.php @@ -1,4 +1,5 @@ <?php + namespace SparkPost\Test; use Ivory\HttpAdapter\CurlHttpAdapter; @@ -6,71 +7,77 @@ use Mockery; use SparkPost\SparkPost; use SparkPost\Test\TestUtils\ClassUtils; -class SparkPostTest extends \PHPUnit_Framework_TestCase { - - private static $utils; - private $adapterMock; - /** @var SparkPost */ - private $resource; - - /** - * (non-PHPdoc) - * @before - * @see PHPUnit_Framework_TestCase::setUp() - */ - public function setUp() { - //setup mock for the adapter - $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 SparkPost($this->adapterMock, ['key'=>'a key']); - self::$utils = new ClassUtils($this->resource); - self::$utils->setProperty($this->resource, 'httpAdapter', $this->adapterMock); - } +class SparkPostTest extends \PHPUnit_Framework_TestCase +{ + private static $utils; + private $adapterMock; + /** @var SparkPost */ + private $resource; - public function tearDown(){ - Mockery::close(); - } + /** + * (non-PHPdoc). + * + * @before + * + * @see PHPUnit_Framework_TestCase::setUp() + */ + public function setUp() + { + //setup mock for the adapter + $this->adapterMock = Mockery::mock('Ivory\HttpAdapter\HttpAdapterInterface', function ($mock) { + $mock->shouldReceive('setConfiguration'); + $mock->shouldReceive('getConfiguration->getUserAgent')->andReturn('php-sparkpost/0.2.0'); + }); - /** - * @desc Ensures that the configuration class is not instantiable. - */ - public function testConstructorSetsUpTransmissions() { - $sparky = new SparkPost(new CurlHttpAdapter(), ['key'=>'a key']); - $this->assertEquals('SparkPost\Transmission', get_class($sparky->transmission)); - $adapter = self::$utils->getProperty($this->resource, 'httpAdapter'); - $this->assertRegExp('/php-sparkpost.*/', $adapter->getConfiguration()->getUserAgent()); - } + $this->resource = new SparkPost($this->adapterMock, ['key' => 'a key']); + self::$utils = new ClassUtils($this->resource); + self::$utils->setProperty($this->resource, 'httpAdapter', $this->adapterMock); + } - public function testSetConfigStringKey() { - $this->resource->setConfig('a key'); - $config = self::$utils->getProperty($this->resource, 'config'); - $this->assertEquals('a key', $config['key']); - } + public function tearDown() + { + Mockery::close(); + } - /** - * @expectedException Exception - * @expectedExceptionMessageRegExp /API key/ - */ - public function testSetBadConfig() { - $this->resource->setConfig(['not'=>'a key']); - } + /** + * @desc Ensures that the configuration class is not instantiable. + */ + public function testConstructorSetsUpTransmissions() + { + $sparky = new SparkPost(new CurlHttpAdapter(), ['key' => 'a key']); + $this->assertEquals('SparkPost\Transmission', get_class($sparky->transmission)); + $adapter = self::$utils->getProperty($this->resource, 'httpAdapter'); + $this->assertRegExp('/php-sparkpost.*/', $adapter->getConfiguration()->getUserAgent()); + } + public function testSetConfigStringKey() + { + $this->resource->setConfig('a key'); + $config = self::$utils->getProperty($this->resource, 'config'); + $this->assertEquals('a key', $config['key']); + } - public function testGetHeaders() { - $results = $this->resource->getHttpHeaders(); - $this->assertEquals('a key', $results['Authorization']); - $this->assertEquals('application/json', $results['Content-Type']); - } + /** + * @expectedException Exception + * @expectedExceptionMessageRegExp /API key/ + */ + public function testSetBadConfig() + { + $this->resource->setConfig(['not' => 'a key']); + } - public function testSetUnwrapped() { - $results = $this->resource->setupUnwrapped('ASweetEndpoint'); - $this->assertEquals($this->resource->ASweetEndpoint, $results); - $this->assertInstanceOf('SparkPost\APIResource', $results); - $this->assertEquals('ASweetEndpoint', $results->endpoint); - } + public function testGetHeaders() + { + $results = $this->resource->getHttpHeaders(); + $this->assertEquals('a key', $results['Authorization']); + $this->assertEquals('application/json', $results['Content-Type']); + } + public function testSetUnwrapped() + { + $results = $this->resource->setupUnwrapped('ASweetEndpoint'); + $this->assertEquals($this->resource->ASweetEndpoint, $results); + $this->assertInstanceOf('SparkPost\APIResource', $results); + $this->assertEquals('ASweetEndpoint', $results->endpoint); + } } -?> diff --git a/test/unit/TestUtils/ClassUtils.php b/test/unit/TestUtils/ClassUtils.php index 26d264c..b25ca56 100644 --- a/test/unit/TestUtils/ClassUtils.php +++ b/test/unit/TestUtils/ClassUtils.php @@ -1,56 +1,64 @@ <?php + namespace SparkPost\Test\TestUtils; +class ClassUtils +{ + private $class; -class ClassUtils { + public function __construct($fqClassName) + { + $this->class = new \ReflectionClass($fqClassName); + } - private $class; + /** + * Allows access to private methods. + * + * This is needed to mock the GuzzleHttp\Client responses + * + * @param string $name + * + * @return ReflectionMethod + */ + public function getMethod($method) + { + $method = $this->class->getMethod($name); + $method->setAccessible(true); - public function __construct($fqClassName) { - $this->class = new \ReflectionClass($fqClassName); - } + return $method; + } - /** - * Allows access to private methods - * - * This is needed to mock the GuzzleHttp\Client responses - * - * @param string $name - * @return ReflectionMethod - */ - public function getMethod($method) { - $method = $this->class->getMethod($name); - $method->setAccessible(true); - return $method; - } + /** + * Allows access to private properties in the Transmission class. + * + * This is needed to mock the GuzzleHttp\Client responses + * + * @param string $name + * @param {*} + * + * @return ReflectionMethod + */ + public function getProperty($instance, $property) + { + $prop = $this->class->getProperty($property); + $prop->setAccessible(true); - /** - * Allows access to private properties in the Transmission class - * - * This is needed to mock the GuzzleHttp\Client responses - * - * @param string $name - * @param {*} - * @return ReflectionMethod - */ - public function getProperty($instance, $property) { - $prop = $this->class->getProperty($property); - $prop->setAccessible(true); - return $prop->getValue($instance); - } - /** - * Allows access to private properties in the Transmission class - * - * This is needed to mock the GuzzleHttp\Client responses - * - * @param string $name - * @param {*} - * @return ReflectionMethod - */ - public function setProperty($instance, $property, $value) { - $prop = $this->class->getProperty($property); - $prop->setAccessible(true); - $prop->setValue($instance, $value); - } + return $prop->getValue($instance); + } + /** + * Allows access to private properties in the Transmission class. + * + * This is needed to mock the GuzzleHttp\Client responses + * + * @param string $name + * @param {*} + * + * @return ReflectionMethod + */ + public function setProperty($instance, $property, $value) + { + $prop = $this->class->getProperty($property); + $prop->setAccessible(true); + $prop->setValue($instance, $value); + } } -?> diff --git a/test/unit/TransmissionTest.php b/test/unit/TransmissionTest.php index ba29b3b..d606c53 100644 --- a/test/unit/TransmissionTest.php +++ b/test/unit/TransmissionTest.php @@ -1,106 +1,114 @@ <?php + namespace SparkPost\Test; + use SparkPost\Transmission; use SparkPost\Test\TestUtils\ClassUtils; -use \Mockery; - -class TransmissionTest extends \PHPUnit_Framework_TestCase { - - private static $utils; - private $sparkPostMock; - private $resource; - - /** - * (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 Transmission($this->sparkPostMock); - self::$utils = new ClassUtils($this->resource); - } - - public function tearDown(){ - Mockery::close(); - } - - public function testSend() { - $responseMock = Mockery::mock(); - $body = ['text'=>'awesomesauce', 'content'=>['subject'=>'awesomeness']]; - $responseBody = ['results'=>'yay']; - - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with('/.*\/transmissions/', 'POST', Mockery::type('array'), Mockery::type('string'))-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(200); - - $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); - - $this->assertEquals($responseBody, $this->resource->send($body)); - } - - public function testSendDateTimeConversion() - { - $testStartTime = new \DateTime("2016-08-27 13:01:02", new \DateTimeZone("UTC")); - - $responseMock = Mockery::mock(); - $responseBody = ['results'=>'yay']; - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with('/.*\/transmissions/', 'POST', Mockery::type('array'), matchesPattern('/"start_time":"2016-08-27T13:01:02\+00:00"/'))-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(200); - $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); - - $this->assertEquals($responseBody, $this->resource->send(['startTime'=>$testStartTime])); - } - - public function testAllWithFilter() { - $responseMock = Mockery::mock(); - $responseBody = ['results'=>'yay']; - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with('/.*transmissions.*?campaign_id=campaign&template_id=template/', 'GET', Mockery::type('array'), null)-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(200); - - $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->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with('/.*\/transmissions/', 'GET', Mockery::type('array'), null)-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(200); - - $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); - - $this->assertEquals($responseBody, $this->resource->all()); - } - - public function testFind() { - $responseMock = Mockery::mock(); - $responseBody = ['results'=>'yay']; - $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with('/.*\/transmissions.*\/test/', 'GET', Mockery::type('array'), null)-> - andReturn($responseMock); - $responseMock->shouldReceive('getStatusCode')->andReturn(200); - - $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); - - $this->assertEquals($responseBody, $this->resource->find('test')); - } - +use Mockery; + +class TransmissionTest extends \PHPUnit_Framework_TestCase +{ + private static $utils; + private $sparkPostMock; + private $resource; + + /** + * (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 Transmission($this->sparkPostMock); + self::$utils = new ClassUtils($this->resource); + } + + public function tearDown() + { + Mockery::close(); + } + + public function testSend() + { + $responseMock = Mockery::mock(); + $body = ['text' => 'awesomesauce', 'content' => ['subject' => 'awesomeness']]; + $responseBody = ['results' => 'yay']; + + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/.*\/transmissions/', 'POST', Mockery::type('array'), Mockery::type('string'))-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); + + $this->assertEquals($responseBody, $this->resource->send($body)); + } + + public function testSendDateTimeConversion() + { + $testStartTime = new \DateTime('2016-08-27 13:01:02', new \DateTimeZone('UTC')); + + $responseMock = Mockery::mock(); + $responseBody = ['results' => 'yay']; + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/.*\/transmissions/', 'POST', Mockery::type('array'), matchesPattern('/"start_time":"2016-08-27T13:01:02\+00:00"/'))-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); + + $this->assertEquals($responseBody, $this->resource->send(['startTime' => $testStartTime])); + } + + public function testAllWithFilter() + { + $responseMock = Mockery::mock(); + $responseBody = ['results' => 'yay']; + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/.*transmissions.*?campaign_id=campaign&template_id=template/', 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + + $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->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/.*\/transmissions/', 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); + + $this->assertEquals($responseBody, $this->resource->all()); + } + + public function testFind() + { + $responseMock = Mockery::mock(); + $responseBody = ['results' => 'yay']; + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/.*\/transmissions.*\/test/', 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); + + $this->assertEquals($responseBody, $this->resource->find('test')); + } } -?> diff --git a/test/unit/bootstrap.php b/test/unit/bootstrap.php index 0b851be..5a41f1c 100644 --- a/test/unit/bootstrap.php +++ b/test/unit/bootstrap.php @@ -1,3 +1,3 @@ <?php - require_once dirname(__FILE__).'/../../vendor/autoload.php'; -?> + +require_once dirname(__FILE__).'/../../vendor/autoload.php'; |