summaryrefslogtreecommitdiffstats
path: root/test/unit/SparkPostTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit/SparkPostTest.php')
-rw-r--r--test/unit/SparkPostTest.php88
1 files changed, 77 insertions, 11 deletions
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
+?>