diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/unit/SparkPostTest.php (renamed from test/unit/ConfigurationTest.php) | 16 | ||||
-rw-r--r-- | test/unit/TransmissionTest.php | 197 |
2 files changed, 46 insertions, 167 deletions
diff --git a/test/unit/ConfigurationTest.php b/test/unit/SparkPostTest.php index 52c60be..218cb6e 100644 --- a/test/unit/ConfigurationTest.php +++ b/test/unit/SparkPostTest.php @@ -1,15 +1,15 @@ <?php namespace SparkPost\Test; -use MessageSystems\Configuration; +use MessageSystems\SparkPost; -class ConfigurationTest extends \PHPUnit_Framework_TestCase { +class SparkPostTest extends \PHPUnit_Framework_TestCase { /** * @desc Ensures that the configuration class is not instantiable. */ public function testConstructorCannotBeCalled() { - $class = new \ReflectionClass('\MessageSystems\Configuration'); + $class = new \ReflectionClass('\MessageSystems\SparkPost'); $this->assertFalse($class->isInstantiable()); } @@ -20,7 +20,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase { * @expectedExceptionMessage No configuration has been provided */ public function testGetConfigEmptyException() { - Configuration::getConfig(); + SparkPost::getConfig(); } /** @@ -29,7 +29,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase { * @expectedExceptionMessage You must provide an API key */ public function testSetConfigAPIKeyNotSetException() { - Configuration::setConfig(['something'=>'other than an API Key']); + SparkPost::setConfig(['something'=>'other than an API Key']); } /** @@ -38,16 +38,16 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase { * @expectedExceptionMessage You must provide an API key */ public function testSetConfigAPIKeyEmptyException() { - Configuration::setConfig(['key'=>'']); + SparkPost::setConfig(['key'=>'']); } /** * @desc Tests overridable values are set while invalid values are ignored */ public function testSetConfigMultipleValuesAndGetConfig() { - Configuration::setConfig(['key'=>'lala', 'version'=>'v8', 'port'=>1024, 'someOtherValue'=>'fakeValue']); + SparkPost::setConfig(['key'=>'lala', 'version'=>'v8', 'port'=>1024, 'someOtherValue'=>'fakeValue']); - $testConfig = Configuration::getConfig(); + $testConfig = SparkPost::getConfig(); $this->assertEquals('lala', $testConfig['key']); $this->assertEquals('v8', $testConfig['version']); $this->assertEquals(1024, $testConfig['port']); diff --git a/test/unit/TransmissionTest.php b/test/unit/TransmissionTest.php index 3d2f6d3..8da2645 100644 --- a/test/unit/TransmissionTest.php +++ b/test/unit/TransmissionTest.php @@ -2,7 +2,7 @@ namespace SparkPost\Test; use MessageSystems\Transmission; -use MessageSystems\Configuration; +use MessageSystems\SparkPost; use GuzzleHttp\Subscriber\Mock; use GuzzleHttp\Message\Response; use GuzzleHttp\Stream\Stream; @@ -14,7 +14,6 @@ use GuzzleHttp\Stream\Stream; */ class TransmissionTest extends \PHPUnit_Framework_TestCase { - private $transmission = null; private $client = null; private static function getMethod($name) { @@ -24,58 +23,22 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { return $method; } - private static function getProperty($obj, $name) { - $class = new \ReflectionClass('\MessageSystems\Transmission'); - $property = $class->getProperty($name); - $property->setAccessible(true); - return $property->getValue($obj); - } - - /** - * Dynamically dereferences an array for a path list provided by the $dereferenceArray - * @param array $array Nested array to be dereferenced - * @param array $dereferenceArray list of key values to dereference - * @return mixed - */ - private static function dereference($array, $dereferenceArray) { - $value = $array; - foreach($dereferenceArray as $derefValue) { - $value = $value[$derefValue]; - } - return $value; - } - /** * (non-PHPdoc) * @before * @see PHPUnit_Framework_TestCase::setUp() */ public function setUp() { - Configuration::setConfig(['key'=>'blah']); - $this->transmission = new Transmission(); - $this->client = self::getProperty($this->transmission, 'request'); //so we can bootstrap api responses + SparkPost::setConfig(['key'=>'blah']); + $this->client = self::getMethod('getHttpClient')->invoke(null); //so we can bootstrap api responses } /** - * @desc Constructor will not set invalid keys + * @desc Ensures that the configuration class is not instantiable. */ - public function testConstructorWillNotSetInvalidKeys() { - $transmission = new Transmission(['blah'=>'blah']); - $this->assertArrayNotHasKey('blah', $this->getProperty($transmission, 'model')); - } - - /** - * - */ - public function testConstructorWillSetValidKeys() { - $transmission = new Transmission(['description'=>'this is a test', 'text'=>'test txt', 'openTracking'=>false, 'clickTracking'=>false, 'useDraftTemplate'=>false, 'recipientList'=>'my recip list']); - $this->assertArrayHasKey('description', $this->getProperty($transmission, 'model')); - $this->assertEquals('this is a test', $this->getProperty($transmission, 'model')['description']); - $this->assertEquals('test txt', $this->getProperty($transmission, 'model')['content']['text']); - $this->assertEquals(false, $this->getProperty($transmission, 'model')['options']['open_tracking']); - $this->assertEquals(false, $this->getProperty($transmission, 'model')['options']['click_tracking']); - $this->assertEquals(false, $this->getProperty($transmission, 'model')['content']['use_draft_template']); - $this->assertEquals('my recip list', $this->getProperty($transmission, 'model')['recipients']['list_id']); + public function testConstructorCannotBeCalled() { + $class = new \ReflectionClass('\MessageSystems\Transmission'); + $this->assertFalse($class->isInstantiable()); } /** @@ -84,7 +47,8 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { 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']]], $this->transmission->all()); + $this->assertEquals(["results"=>[['test'=>'This is a test'], ['test'=>'two']]], Transmission::all()); + $this->client->getEmitter()->detach($mock); } /** @@ -93,40 +57,40 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { 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']]], $this->transmission->find('someId')); + $this->assertEquals(["results"=>[['test'=>'This is a test']]], Transmission::find('someId')); + $this->client->getEmitter()->detach($mock); } /** - * @expectedException Exception - * @expectedExceptionMessage The specified Transmission ID does not exist + * */ public function testFindWith404Response() { $mock = new Mock([new Response(404, [])]); $this->client->getEmitter()->attach($mock); - $this->transmission->find('someId'); + try { + Transmission::find('someId'); + } catch (\Exception $e) { + $this->assertEquals('The specified Transmission ID does not exist', $e->getMessage()); + } finally { + $this->client->getEmitter()->detach($mock); + } } /** - * @expectedException Exception - * @expectedExceptionMessage Received bad response from Transmission API: 400 + * */ public function testFindWithOtherBadResponse() { $mock = new Mock([new Response(400, [])]); $this->client->getEmitter()->attach($mock); - $this->transmission->find('someId'); + try { + Transmission::find('someId'); + } catch (\Exception $e) { + $this->assertEquals('Received bad response from Transmission API: 400', $e->getMessage()); + } finally { + $this->client->getEmitter()->detach($mock); + } } -// /** -// * @expectedException Exception -// * @expectedExceptionMessageRegExp /Unable to contact Transmissions API:.* / -// */ -// public function testFindForUnableToContactServer() { -// $mock = new Mock([new Response(500)]); -// $this->client->getEmitter()->attach($mock); -// $this->transmission->find('someId'); -// } - - /** * */ @@ -134,109 +98,24 @@ class TransmissionTest extends \PHPUnit_Framework_TestCase { $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, $this->transmission->send()); + $this->assertEquals($body, Transmission::send(['text'=>'awesome email'])); + $this->client->getEmitter()->detach($mock); } /** - * @expectedException Exception - * @expectedExceptionMessage This is a fake error + * */ - public function testFindForRequestException() { + 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); - $this->transmission->send(); - } - -// /** -// * @expectedException Exception -// * @expectedExceptionMessageRegExp /Unable to contact Traissions API: \w+/ -// */ -// public function testSendForUnableToContactServer() { -// $mock = new Mock([new Response(500, [])]); -// $this->client->getEmitter()->attach($mock); -// $this->transmission->send(); -// } - - - public function setProvider() { - return [ - ['setMetadata', ['metadata']], - ['setSubstitutionData', ['substitution_data']], - ['setCampaign', ['campaign_id']], - ['setDescription', ['description']], - ['setReturnPath', ['return_path']], - ['setReplyTo', ['content', 'reply_to']], - ['setSubject', ['content', 'subject']], - ['setFrom', ['content', 'from']], - ['setHTMLContent', ['content', 'html']], - ['setTextContent', ['content', 'text']], - ['setRfc822Content', ['content', 'email_rfc822']], - - ['useRecipientList', ['recipients', 'list_id']], - ['useStoredTemplate', ['content', 'template_id']] - ]; - } - - /** - * @dataProvider setProvider - */ - public function testSimpleSetFunctions($setterFunction, $dereference) { - $testValue = 'test'; - $returnValue = $this->transmission->$setterFunction($testValue); - $this->assertInstanceOf('MessageSystems\Transmission', $returnValue); - $this->assertSame($this->transmission, $returnValue); - $this->assertEquals($testValue, self::dereference(self::getProperty($this->transmission, 'model'), $dereference)); - } - - public function complexSetFunctionsProvider() { - return [ - ['addRecipient', ['recipients'], ['address'=>'testRecipient@example.com'], [['address'=>'testRecipient@example.com']]], - ['addRecipients', ['recipients'], [ - ['address'=>'testRecipient1@example.com'], - ['address'=>'testRecipient2@example.com'], - ['address'=>'testRecipient3@example.com'] - ], - [ - ['address'=>'testRecipient1@example.com'], - ['address'=>'testRecipient2@example.com'], - ['address'=>'testRecipient3@example.com'] - ]], - ['setContentHeaders', ['content', 'headers'], ['x-head'=>'somevalue'], ['x-head'=>'somevalue']], - ]; - } - - /** - * @dataProvider complexSetFunctionsProvider - */ - public function testComplexSetFunctions($setterFunction, $dereference, $setValue, $expected) { - $returnValue = $this->transmission->$setterFunction($setValue); - $this->assertInstanceOf('MessageSystems\Transmission', $returnValue); - $this->assertSame($this->transmission, $returnValue); - $this->assertEquals($expected, self::dereference(self::getProperty($this->transmission, 'model'), $dereference)); - } - - - - public function optionsProvider() { - return [ - ['enableClickTracking', ['options', 'click_tracking'], true], - ['disableClickTracking', ['options', 'click_tracking'], false], - ['enableOpenTracking', ['options', 'open_tracking'], true], - ['disableOpenTracking', ['options', 'open_tracking'], false], - ['useDraftTemplate', ['content', 'use_draft_template'], true], - ['usePublishedTemplate', ['content', 'use_draft_template'], false] - ]; - } - - /** - * @dataProvider optionsProvider - */ - public function testOptionsFunctions($setterFunction, $dereference, $expected) { - $returnValue = $this->transmission->$setterFunction(); - $this->assertInstanceOf('MessageSystems\Transmission', $returnValue); - $this->assertSame($this->transmission, $returnValue); - $this->assertEquals($expected, self::dereference(self::getProperty($this->transmission, 'model'), $dereference)); + try { + Transmission::send(['text'=>'awesome email']); + } catch (\Exception $e) { + $this->assertEquals('["This is a fake error"]', $e->getMessage()); + } finally { + $this->client->getEmitter()->detach($mock); + } } } |