diff options
-rw-r--r-- | composer.json | 3 | ||||
-rw-r--r-- | lib/SparkPost/APIResource.php | 22 | ||||
-rw-r--r-- | test/unit/APIResourceTest.php | 144 | ||||
-rw-r--r-- | test/unit/SparkPostTest.php | 116 | ||||
-rw-r--r-- | test/unit/TestUtils/ClassUtils.php | 56 | ||||
-rw-r--r-- | test/unit/bootstrap.php | 2 |
6 files changed, 79 insertions, 264 deletions
diff --git a/composer.json b/composer.json index 3917db1..627fbfb 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "autoload": { "psr-4": { "SparkPost\\": "lib/SparkPost/", - "SparkPost\\SendGridCompatibility\\": "lib/SendGridCompatibility/" + "SparkPost\\SendGridCompatibility\\": "lib/SendGridCompatibility/", + "SparkPost\\Test\\TestUtils\\": "test/unit/TestUtils/" } } } diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php index 21514dc..714f1fb 100644 --- a/lib/SparkPost/APIResource.php +++ b/lib/SparkPost/APIResource.php @@ -111,17 +111,6 @@ class APIResource { } /** - * Retrieves the Http Adapter that was previously setup by the user - * @throws \Exception - */ - public function getHttpAdapter() { - if ($this->httpAdapter === null) { - throw new \Exception('No Http Adapter has been provided'); - } - return $this->httpAdapter; - } - - /** * Allows the user to pass in values to override the defaults and set their API key * @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost * @throws \Exception @@ -143,17 +132,6 @@ class APIResource { } /** - * Retrieves the configuration that was previously setup by the user - * @throws \Exception - */ - public function getConfig() { - if ($this->config === null) { - throw new \Exception('No configuration has been provided'); - } - return $this->config; - } - - /** * @desc Private Method helper to reference parameter mappings and set the right value for the right parameter */ protected function setMappedValue (&$model, $mapKey, $value) { diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php index c9f83be..99d7c80 100644 --- a/test/unit/APIResourceTest.php +++ b/test/unit/APIResourceTest.php @@ -1,30 +1,18 @@ <?php namespace SparkPost\Test; - use SparkPost\APIResource; -use SparkPost\SparkPost; -use Guzzle\Plugin\Mock\MockPlugin; -use Guzzle\Http\Message\Response; - +use SparkPost\Test\TestUtils\ClassUtils; +use Ivory\HttpAdapter\CurlHttpAdapter; class APIResourceTest extends \PHPUnit_Framework_TestCase { - private $client = null; + private static $utils; + private $adapterMock; + private $resource; - /** - * Allows access to private methods - * - * This is needed to mock the GuzzleHttp\Client responses - * - * @param string $name - * @return ReflectionMethod - */ - private static function getMethod($name) { - $class = new \ReflectionClass('\SparkPost\APIResource'); - $method = $class->getMethod($name); - $method->setAccessible(true); - return $method; - } + public static function setUpBeforeClass() { + + } /** * (non-PHPdoc) @@ -32,112 +20,14 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase { * @see PHPUnit_Framework_TestCase::setUp() */ public function setUp() { - SparkPost::getHttpHeaders = function ($headers) {return ['what'=>'what']}; - APIResource::$endpoint = 'someValidEndpoint'; // when using APIResource directly an endpoint needs to be set. - } - // - // /** - // * @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 testFetchWithGoodResponse() { - // $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'))), APIResource::fetchResource()); - // } - // - // /** - // * @desc tests happy path - // */ - // public function testDeleteWithGoodResponse() { - // $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'))), APIResource::deleteResource('someId')); - // } - // - // /** - // * @desc tests 404 bad response - // * @expectedException Exception - // * @expectedExceptionMessage The specified resource does not exist - // */ - // public function testFetchWith404Response() { - // $mock = new MockPlugin(); - // $mock->addResponse(new Response(404, array())); - // $this->client->addSubscriber($mock); - // APIResource::fetchResource('someId'); - // } - // - // /** - // * @desc tests unknown bad response - // * @expectedException Exception - // * @expectedExceptionMessage Received bad response from SomeValidEndpoint API: 400 - // */ - // public function testFetchWithOtherBadResponse() { - // $mock = new MockPlugin(); - // $mock->addResponse(new Response(400, array())); - // $this->client->addSubscriber($mock); - // APIResource::fetchResource('someId'); - // } - // - // /** - // * @desc tests bad response - // * @expectedException Exception - // * @expectedExceptionMessageRegExp /Unable to contact SomeValidEndpoint API:.* / - // */ - // public function testFetchForCatchAllException() { - // $mock = new MockPlugin(); - // $mock->addResponse(new Response(500)); - // $this->client->addSubscriber($mock); - // APIResource::fetchResource('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, APIResource::sendRequest(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); - // APIResource::sendRequest(array('text'=>'awesome email')); - // } - // - // - // /** - // * @desc tests bad response - // * @expectedException Exception - // * @expectedExceptionMessageRegExp /Unable to contact SomeValidEndpoint API:.* / - // */ - // public function testSendForCatchAllException() { - // $mock = new MockPlugin(); - // $mock->addResponse(new Response(500)); - // $this->client->addSubscriber($mock); - // APIResource::sendRequest(array('text'=>'awesome email')); - // } + $this->adapterMock = $this->getMockBuilder('CurlHttpAdapter')->getMock(); + + $this->resource = new APIResource(new CurlHttpAdapter(), ['key'=>'a key']); + self::$utils = new ClassUtils($this->resource); + } + + public function testConstructorSetsUpAdapterAndConfig() { + $this->assertEquals('Ivory\HttpAdapter\CurlHttpAdapter', get_class(self::$utils->getProperty($this->resource, 'httpAdapter'))); + } } diff --git a/test/unit/SparkPostTest.php b/test/unit/SparkPostTest.php index 43e499f..9808c38 100644 --- a/test/unit/SparkPostTest.php +++ b/test/unit/SparkPostTest.php @@ -6,122 +6,12 @@ use Ivory\HttpAdapter\CurlHttpAdapter; class SparkPostTest extends \PHPUnit_Framework_TestCase { - /** - * Allows access to private properties in the Transmission class - * - * @param string $name - * @param {*} - * @return ReflectionMethod - */ - private static function setPrivateProperty($name, $value) { - $class = new \ReflectionClass('\SparkPost\SparkPost'); - $prop = $class->getProperty($name); - $prop->setAccessible(true); - $prop->setValue($value); - } - - - public function setUp() { - $this->setPrivateProperty('config', null); - $this->setPrivateProperty('httpAdapter', null); - } - /** * @desc Ensures that the configuration class is not instantiable. */ - public function testConstructorCannotBeCalled() { - $class = new \ReflectionClass('\SparkPost\SparkPost'); - $this->assertFalse($class->isInstantiable()); - } - - /** - * @desc Tests that an exception is thrown when a library tries to recieve the config and it has not yet been set. - * Since its a singleton this test must come before any setConfig tests. - * @expectedException Exception - * @expectedExceptionMessage No configuration has been provided - */ - public function testGetConfigEmptyException() { - SparkPost::unsetConfig(); - SparkPost::getConfig(); - } - - /** - * @desc Tests that the api key is set when setting the config - * @expectedException Exception - * @expectedExceptionMessage You must provide an API key - */ - public function testSetConfigAPIKeyNotSetException() { - SparkPost::setConfig(['something'=>'other than an API Key']); - } - - /** - * @desc Tests that the api key is set when setting the config and that its not empty - * @expectedException Exception - * @expectedExceptionMessage You must provide an API key - */ - public function testSetConfigAPIKeyEmptyException() { - SparkPost::setConfig(['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']); - - $testConfig = SparkPost::getConfig(); - $this->assertEquals('lala', $testConfig['key']); - $this->assertEquals('v8', $testConfig['version']); - $this->assertEquals(1024, $testConfig['port']); - $this->assertNotContains('someOtherValue', array_keys($testConfig)); - $this->assertEquals('https', $testConfig['protocol']); - $this->assertEquals('api.sparkpost.com', $testConfig['host']); - $this->assertEquals(true, $testConfig['strictSSL']); + public function testConstructorSetsUpTransmissions() { + $sparky = new SparkPost(new CurlHttpAdapter(), ['key'=>'a key']); + $this->assertEquals('SparkPost\Transmission', get_class($sparky->transmission)); } - - /** - * @desc tests getting an unset - * @expectedException Exception - * @expectedExceptionMessageRegExp /No Http Adapter/ - */ - public function testGetHttpAdapterForIsset() { - SparkPost::getHttpAdapter(); - } - - /** - * @desc tests failing validation for http adapters - * @expectedException Exception - * @expectedExceptionMessageRegExp /must be a valid Ivory\\HttpAdapter/ - */ - public function testSetInvalidHttpAdapter() { - SparkPost::setHttpAdapter(new \stdClass()); - } - - public function testSetAndGetValidHttpAdapter() { - SparkPost::setConfig(['key'=>'lala']); - SparkPost::setHttpAdapter(new CurlHttpAdapter()); - $this->assertEquals('Ivory\HttpAdapter\CurlHttpAdapter', get_class(Sparkpost::getHttpAdapter())); - } - - public function testConfigure() { - SparkPost::configure(new CurlHttpAdapter(), ['key'=>'lala']); - $this->assertEquals('Ivory\HttpAdapter\CurlHttpAdapter', get_class(Sparkpost::getHttpAdapter())); - } - - public function testDefaultHeaders() { - $key = 'lala'; - SparkPost::setConfig(['key'=>$key]); - $this->assertEquals($key, Sparkpost::getHttpHeaders()['Authorization']); - $this->assertEquals('application/json', Sparkpost::getHttpHeaders()['Content-Type']); - } - - public function testOverrideDefaultHeaders() { - $key = 'lala'; - $headers=['Content-Type'=>'my/type']; - SparkPost::setConfig(['key'=>$key]); - $this->assertEquals($key, Sparkpost::getHttpHeaders($headers)['Authorization']); - $this->assertEquals('my/type', Sparkpost::getHttpHeaders($headers)['Content-Type']); - } - } ?> diff --git a/test/unit/TestUtils/ClassUtils.php b/test/unit/TestUtils/ClassUtils.php new file mode 100644 index 0000000..26d264c --- /dev/null +++ b/test/unit/TestUtils/ClassUtils.php @@ -0,0 +1,56 @@ +<?php +namespace SparkPost\Test\TestUtils; + + +class ClassUtils { + + private $class; + + public function __construct($fqClassName) { + $this->class = new \ReflectionClass($fqClassName); + } + + /** + * 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); + 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/bootstrap.php b/test/unit/bootstrap.php index 7e73606..835369b 100644 --- a/test/unit/bootstrap.php +++ b/test/unit/bootstrap.php @@ -1,3 +1,3 @@ <?php require_once dirname(__FILE__).'/../../vendor/autoload.php'; -?>
\ No newline at end of file +?> |