diff options
author | nornholdj <nornholdj@gmail.com> | 2014-11-04 17:42:49 -0500 |
---|---|---|
committer | nornholdj <nornholdj@gmail.com> | 2014-11-04 17:42:49 -0500 |
commit | 39f2699a7e7850397882a6d2ee3a8438a105b72d (patch) | |
tree | ca192a9ea0084b7f59807ff5fd3c262bb05ebb6d /test/unit | |
parent | 5d346d241fc0d0b565d7aa6ce0179a5da4c73dc0 (diff) | |
parent | 2a35edc5094276f9a9b1566bdb678386fae14d3d (diff) | |
download | php-sparkpost-39f2699a7e7850397882a6d2ee3a8438a105b72d.zip php-sparkpost-39f2699a7e7850397882a6d2ee3a8438a105b72d.tar.gz php-sparkpost-39f2699a7e7850397882a6d2ee3a8438a105b72d.tar.bz2 |
Merge branch 'feature/MA-946' into develop
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/SparkPostTest.php | 60 | ||||
-rw-r--r-- | test/unit/TransmissionTest.php | 126 | ||||
-rw-r--r-- | test/unit/bootstrap.php | 3 |
3 files changed, 189 insertions, 0 deletions
diff --git a/test/unit/SparkPostTest.php b/test/unit/SparkPostTest.php new file mode 100644 index 0000000..1b86a8f --- /dev/null +++ b/test/unit/SparkPostTest.php @@ -0,0 +1,60 @@ +<?php +namespace SparkPost\Test; + +use MessageSystems\SparkPost; + +class SparkPostTest extends \PHPUnit_Framework_TestCase { + + /** + * @desc Ensures that the configuration class is not instantiable. + */ + public function testConstructorCannotBeCalled() { + $class = new \ReflectionClass('\MessageSystems\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::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']); + } +} +?>
\ No newline at end of file diff --git a/test/unit/TransmissionTest.php b/test/unit/TransmissionTest.php new file mode 100644 index 0000000..ae7d59d --- /dev/null +++ b/test/unit/TransmissionTest.php @@ -0,0 +1,126 @@ +<?php +namespace SparkPost\Test; + +use MessageSystems\Transmission; +use MessageSystems\SparkPost; +use GuzzleHttp\Subscriber\Mock; +use GuzzleHttp\Message\Response; +use GuzzleHttp\Stream\Stream; + + +class TransmissionTest extends \PHPUnit_Framework_TestCase { + + private $client = null; + + /** + * Allows access to private methods in the Transmission class + * + * This is needed to mock the GuzzleHttp\Client responses + * + * @param string $name + * @return ReflectionMethod + */ + private static function getMethod($name) { + $class = new \ReflectionClass('\MessageSystems\Transmission'); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method; + } + + /** + * (non-PHPdoc) + * @before + * @see PHPUnit_Framework_TestCase::setUp() + */ + public function setUp() { + SparkPost::setConfig(['key'=>'blah']); + $this->client = self::getMethod('getHttpClient')->invoke(null); //so we can bootstrap api responses + } + + /** + * @desc Ensures that the configuration class is not instantiable. + */ + public function testConstructorCannotBeCalled() { + $class = new \ReflectionClass('\MessageSystems\Transmission'); + $this->assertFalse($class->isInstantiable()); + } + + /** + * @desc tests happy path + */ + public function testAllWithGoodResponse() { + $mock = new Mock([new Response(200, [], Stream::factory('{"results":[{"test":"This is a test"}, {"test":"two"}]}'))]); + $this->client->getEmitter()->attach($mock); + $this->assertEquals(["results"=>[['test'=>'This is a test'], ['test'=>'two']]], Transmission::all()); + $this->client->getEmitter()->detach($mock); + } + + /** + * @desc tests happy path + */ + public function testFindWithGoodResponse() { + $mock = new Mock([new Response(200, [], Stream::factory('{"results":[{"test":"This is a test"}]}'))]); + $this->client->getEmitter()->attach($mock); + $this->assertEquals(["results"=>[['test'=>'This is a test']]], Transmission::find('someId')); + $this->client->getEmitter()->detach($mock); + } + + /** + * @desc tests 404 bad response + */ + public function testFindWith404Response() { + $mock = new Mock([new Response(404, [])]); + $this->client->getEmitter()->attach($mock); + try { + Transmission::find('someId'); + } catch (\Exception $e) { + $this->assertEquals('The specified Transmission ID does not exist', $e->getMessage()); + } finally { + $this->client->getEmitter()->detach($mock); + } + } + + /** + * @desc tests unknown bad response + */ + public function testFindWithOtherBadResponse() { + $mock = new Mock([new Response(400, [])]); + $this->client->getEmitter()->attach($mock); + try { + Transmission::find('someId'); + } catch (\Exception $e) { + $this->assertEquals('Received bad response from Transmission API: 400', $e->getMessage()); + } finally { + $this->client->getEmitter()->detach($mock); + } + } + + /** + * @desc tests happy path + */ + public function testSuccessfulSend() { + $body = ["result"=>["transmission_id"=> "11668787484950529"], "status"=>["message"=> "ok","code"=> "1000"]]; + $mock = new Mock([new Response(200, [], Stream::factory(json_encode($body)))]); + $this->client->getEmitter()->attach($mock); + $this->assertEquals($body, Transmission::send(['text'=>'awesome email'])); + $this->client->getEmitter()->detach($mock); + } + + /** + * @desc tests bad response + */ + public function testSendForRequestException() { + $body = ['errors'=>['This is a fake error']]; + $mock = new Mock([new Response(400, [], Stream::factory(json_encode($body)))]); + $this->client->getEmitter()->attach($mock); + try { + Transmission::send(['text'=>'awesome email']); + } catch (\Exception $e) { + $this->assertEquals('["This is a fake error"]', $e->getMessage()); + } finally { + $this->client->getEmitter()->detach($mock); + } + } + +} +?>
\ No newline at end of file diff --git a/test/unit/bootstrap.php b/test/unit/bootstrap.php new file mode 100644 index 0000000..7e73606 --- /dev/null +++ b/test/unit/bootstrap.php @@ -0,0 +1,3 @@ +<?php + require_once dirname(__FILE__).'/../../vendor/autoload.php'; +?>
\ No newline at end of file |