summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorbeardyman <nornholdj@gmail.com>2015-09-16 23:25:03 -0400
committerbeardyman <nornholdj@gmail.com>2015-09-16 23:25:03 -0400
commitb51ce2d0a8fad2577c164e1ab1c8382b1ae23fae (patch)
tree0cfd23aff75b957b0beb7e88e4c0a544c5c107b5 /test
parent0c4b276fd3c51e938474071fcde5b294a7651256 (diff)
downloadphp-sparkpost-b51ce2d0a8fad2577c164e1ab1c8382b1ae23fae.zip
php-sparkpost-b51ce2d0a8fad2577c164e1ab1c8382b1ae23fae.tar.gz
php-sparkpost-b51ce2d0a8fad2577c164e1ab1c8382b1ae23fae.tar.bz2
finished code updates/refactoring updated examples and began refactoring tests
Diffstat (limited to 'test')
-rw-r--r--test/unit/APIResourceTest.php223
-rw-r--r--test/unit/SparkPostTest.php88
2 files changed, 188 insertions, 123 deletions
diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php
index fc85fa3..c9f83be 100644
--- a/test/unit/APIResourceTest.php
+++ b/test/unit/APIResourceTest.php
@@ -8,14 +8,14 @@ use Guzzle\Http\Message\Response;
class APIResourceTest extends \PHPUnit_Framework_TestCase {
-
+
private $client = null;
-
+
/**
* Allows access to private methods
- *
+ *
* This is needed to mock the GuzzleHttp\Client responses
- *
+ *
* @param string $name
* @return ReflectionMethod
*/
@@ -25,120 +25,119 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase {
$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
+ 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'));
- }
-
+ //
+ // /**
+ // * @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'));
+ // }
+
}
diff --git a/test/unit/SparkPostTest.php b/test/unit/SparkPostTest.php
index f40461d..43e499f 100644
--- a/test/unit/SparkPostTest.php
+++ b/test/unit/SparkPostTest.php
@@ -2,17 +2,38 @@
namespace SparkPost\Test;
use SparkPost\SparkPost;
+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());
+ $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.
@@ -23,31 +44,31 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase {
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(array('something'=>'other than an API Key'));
+ 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(array('key'=>''));
+ SparkPost::setConfig(['key'=>'']);
}
-
+
/**
* @desc Tests overridable values are set while invalid values are ignored
*/
public function testSetConfigMultipleValuesAndGetConfig() {
- SparkPost::setConfig(array('key'=>'lala', 'version'=>'v8', 'port'=>1024, 'someOtherValue'=>'fakeValue'));
-
+ SparkPost::setConfig(['key'=>'lala', 'version'=>'v8', 'port'=>1024, 'someOtherValue'=>'fakeValue']);
+
$testConfig = SparkPost::getConfig();
$this->assertEquals('lala', $testConfig['key']);
$this->assertEquals('v8', $testConfig['version']);
@@ -57,5 +78,50 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals('api.sparkpost.com', $testConfig['host']);
$this->assertEquals(true, $testConfig['strictSSL']);
}
+
+ /**
+ * @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']);
+ }
+
}
-?> \ No newline at end of file
+?>