diff options
author | beardyman <nornholdj@gmail.com> | 2015-09-30 21:44:52 -0400 |
---|---|---|
committer | beardyman <nornholdj@gmail.com> | 2015-09-30 21:44:52 -0400 |
commit | 97e0d2643db1969e299349968eea1d9c7a951d7f (patch) | |
tree | 609c4ee26facf953a8d56e855303519c3678c66b | |
parent | c753d55e2025c3948fe8a59a660a881a429b8df2 (diff) | |
download | php-sparkpost-97e0d2643db1969e299349968eea1d9c7a951d7f.zip php-sparkpost-97e0d2643db1969e299349968eea1d9c7a951d7f.tar.gz php-sparkpost-97e0d2643db1969e299349968eea1d9c7a951d7f.tar.bz2 |
Updated tests for new structure
-rw-r--r-- | examples/unwrapped/create_template.php | 3 | ||||
-rw-r--r-- | lib/SparkPost/APIResource.php | 5 | ||||
-rw-r--r-- | lib/SparkPost/SparkPost.php | 2 | ||||
-rw-r--r-- | test/unit/APIResourceTest.php | 48 | ||||
-rw-r--r-- | test/unit/SparkPostTest.php | 66 | ||||
-rw-r--r-- | test/unit/TransmissionTest.php | 57 |
6 files changed, 114 insertions, 67 deletions
diff --git a/examples/unwrapped/create_template.php b/examples/unwrapped/create_template.php index c0e52b7..51ab156 100644 --- a/examples/unwrapped/create_template.php +++ b/examples/unwrapped/create_template.php @@ -1,6 +1,7 @@ <?php namespace Examples\Unwrapped; require_once (dirname(__FILE__).'/../bootstrap.php'); +use SparkPost\SparkPost; use GuzzleHttp\Client; use Ivory\HttpAdapter\Guzzle6HttpAdapter; @@ -14,7 +15,7 @@ try { $templateConfig = [ 'name' => 'Summer Sale!', - 'id'=>'summer-sale', + 'id'=>'jordan-test-summer-sale', 'content'=> [ 'from' => 'from@sparkpostbox.com', 'subject' => 'Summer deals', diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php index 7ec958b..4c4cf08 100644 --- a/lib/SparkPost/APIResource.php +++ b/lib/SparkPost/APIResource.php @@ -28,6 +28,11 @@ class APIResource { */ protected static $structure = []; + /** + * @desc SparkPost reference for httpAdapters and configs + */ + protected $sparkpost; + /** * @desc Initializes config and httpAdapter for use later. * @param $sparkpost SparkPost\SparkPost provides api configuration information diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index aac352b..83200c0 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -52,7 +52,7 @@ class SparkPost { * The new resource is attached to this object as well as returned * @return SparkPost\APIResource - the unwrapped resource */ - public function setupUnwrapped (string $endpoint) { + public function setupUnwrapped ($endpoint) { $this->{$endpoint} = new APIResource($this); $this->{$endpoint}->endpoint = $endpoint; diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php index 03d3650..132d6e2 100644 --- a/test/unit/APIResourceTest.php +++ b/test/unit/APIResourceTest.php @@ -24,14 +24,13 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase { * @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->sparkPostMock = Mockery::mock('SparkPost\SparkPost', function($mock) { + $mock->shouldReceive('getHttpHeaders')->andReturn([]); }); - $this->resource = new APIResource($this->adapterMock, ['key'=>'a key']); + $this->sparkPostMock->httpAdapter = Mockery::mock(); + $this->resource = new APIResource($this->sparkPostMock); self::$utils = new ClassUtils($this->resource); - - self::$utils->setProperty($this->resource, 'httpAdapter', $this->adapterMock); + self::$utils->setProperty($this->resource, 'sparkpost', $this->sparkPostMock); } public function tearDown() @@ -39,37 +38,22 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase { Mockery::close(); } - public function testConstructorSetsUpAdapterAndConfig() { - $adapter = self::$utils->getProperty($this->resource, 'httpAdapter'); - $this->assertRegExp('/php-sparkpost.*/', $adapter->getConfiguration()->getUserAgent()); - } - - /** - * @expectedException Exception - * @expectedExceptionMessageRegExp /valid Ivory\\HttpAdapter/ - */ - public function testSetBadHTTPAdapter() { - $this->resource->setHttpAdapter(new \stdClass()); - } - - /** - * @expectedException Exception - * @expectedExceptionMessageRegExp /API key/ - */ - public function testSetBadConfig() { - $this->resource->setConfig(['not'=>'a key']); + 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->adapterMock->shouldReceive('send')-> + $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)); } @@ -77,7 +61,7 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase { $testInput = ['test'=>'body']; $testBody = ["results"=>["my"=>"test"]]; $responseMock = Mockery::mock(); - $this->adapterMock->shouldReceive('send')-> + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> once()-> with('/.*\/test/', 'PUT', Mockery::type('array'), json_encode($testInput))-> andReturn($responseMock); @@ -89,7 +73,7 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase { public function testGet() { $testBody = ["results"=>["my"=>"test"]]; $responseMock = Mockery::mock(); - $this->adapterMock->shouldReceive('send')-> + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> once()-> with('/.*\/test/', 'GET', Mockery::type('array'), null)-> andReturn($responseMock); @@ -100,7 +84,7 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase { public function testDelete() { $responseMock = Mockery::mock(); - $this->adapterMock->shouldReceive('send')-> + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> once()-> with('/.*\/test/', 'DELETE', Mockery::type('array'), null)-> andReturn($responseMock); @@ -111,7 +95,7 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase { public function testAdapter404Exception() { try { - $this->adapterMock->shouldReceive('send')-> + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> once()-> andThrow($this->getExceptionMock(404)); @@ -124,7 +108,7 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase { public function testAdapter4XXException() { try { - $this->adapterMock->shouldReceive('send')-> + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> once()-> andThrow($this->getExceptionMock(400)); @@ -137,7 +121,7 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase { public function testAdapter5XXException() { try { - $this->adapterMock->shouldReceive('send')-> + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> once()-> andThrow(new \Exception('Something went wrong.')); diff --git a/test/unit/SparkPostTest.php b/test/unit/SparkPostTest.php index 9808c38..aeda7d7 100644 --- a/test/unit/SparkPostTest.php +++ b/test/unit/SparkPostTest.php @@ -3,15 +3,81 @@ namespace SparkPost\Test; use SparkPost\SparkPost; use Ivory\HttpAdapter\CurlHttpAdapter; +use SparkPost\Test\TestUtils\ClassUtils; +use \Mockery; class SparkPostTest extends \PHPUnit_Framework_TestCase { + private static $utils; + private $adapterMock; + 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); + } + + public function tearDown() + { + Mockery::close(); + } + /** * @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()); } + + /** + * @expectedException Exception + * @expectedExceptionMessageRegExp /valid Ivory\\HttpAdapter/ + */ + public function testSetBadHTTPAdapter() { + $this->resource->setHttpAdapter(new \stdClass()); + } + + /** + * @expectedException Exception + * @expectedExceptionMessageRegExp /API key/ + */ + public function testSetBadConfig() { + $this->resource->setConfig(['not'=>'a key']); + } + + + public function testGetHeaders() { + $results = $this->resource->getHttpHeaders(); + $this->assertEquals('a key', $results['Authorization']); + $this->assertEquals('application/json', $results['Content-Type']); + } + + public function testGetHeadersOverride() { + $results = $this->resource->getHttpHeaders(['Content-Type'=>'application/xml']); + $this->assertEquals('application/xml', $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/TransmissionTest.php b/test/unit/TransmissionTest.php index 95fabe4..f09db7d 100644 --- a/test/unit/TransmissionTest.php +++ b/test/unit/TransmissionTest.php @@ -1,6 +1,5 @@ <?php namespace SparkPost\Test; - use SparkPost\Transmission; use SparkPost\Test\TestUtils\ClassUtils; use \Mockery; @@ -8,7 +7,7 @@ use \Mockery; class TransmissionTest extends \PHPUnit_Framework_TestCase { private static $utils; - private $adapterMock; + private $sparkPostMock; private $resource; /** @@ -17,35 +16,27 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { * @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->sparkPostMock = Mockery::mock('SparkPost\SparkPost', function($mock) { + $mock->shouldReceive('getHttpHeaders')->andReturn([]); }); - $this->resource = new Transmission($this->adapterMock, ['key'=>'a key']); + $this->sparkPostMock->httpAdapter = Mockery::mock(); + $this->resource = new Transmission($this->sparkPostMock); 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 tearDown(){ + Mockery::close(); } - 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); + + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/.*\/transmissions/', 'POST', Mockery::type('array'), Mockery::type('string'))-> + andReturn($responseMock); $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); @@ -55,10 +46,10 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { 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); + $this->sparkPostMock->httpAdapter->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)); @@ -68,10 +59,10 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { public function testAllWithOutFilter() { $responseMock = Mockery::mock(); $responseBody = ["results"=>"yay"]; - $this->adapterMock->shouldReceive('send')-> - once()-> - with('/.*\/transmissions/', 'GET', Mockery::type('array'), null)-> - andReturn($responseMock); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/.*\/transmissions/', 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); @@ -81,10 +72,10 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { public function testFind() { $responseMock = Mockery::mock(); $responseBody = ["results"=>"yay"]; - $this->adapterMock->shouldReceive('send')-> - once()-> - with('/.*\/transmissions.*\/test/', 'GET', Mockery::type('array'), null)-> - andReturn($responseMock); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with('/.*\/transmissions.*\/test/', 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody)); |