From a0bf285b50b04f195e610c6dcb30938ab4b82ad8 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Sun, 22 Apr 2018 08:35:22 -0700 Subject: [PATCH] EmailTemplate endpoint tests; phpcs PSR-2 scan whitespace corrections --- src/API/Management/EmailTemplates.php | 13 +- tests/API/ApiTests.php | 19 ++- tests/API/Management/EmailTemplatesTest.php | 124 ++++++++++++++++++++ 3 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 tests/API/Management/EmailTemplatesTest.php diff --git a/src/API/Management/EmailTemplates.php b/src/API/Management/EmailTemplates.php index eff31f015..74c33179f 100644 --- a/src/API/Management/EmailTemplates.php +++ b/src/API/Management/EmailTemplates.php @@ -14,9 +14,8 @@ class EmailTemplates extends GenericResource { /** * Get an email template by name. - * The email template needs to already exist or the response will be a 404. - * An invalid template name will respond with a 400. * See docs @link below for valid names and fields. + * Requires scope: read:email_templates. * * @param string $templateName - the email template name to get. * @@ -36,7 +35,8 @@ public function get($templateName) /** * Patch an email template by name. * This will update only the email template data fields provided (see HTTP PATCH). - * See docs @link below for valid names and fields. + * See docs @link below for valid names, fields, and possible responses. + * Requires scope: update:email_templates. * * @param string $templateName - the email template name to patch. * @param array $data - an array of data to update. @@ -58,8 +58,11 @@ public function patch($templateName, $data) /** * Create an email template by name. * See docs @link below for valid names and fields. + * Requires scope: create:email_templates. * - * @param array $data - an array of data to use for the new email, including a valid template name. + * @param array $data + * An array of data to use for the new email, including a valid `template`. + * See docs link below for required fields. * * @return mixed|string * @@ -74,4 +77,4 @@ public function create($data) ->withBody(json_encode($data)) ->call(); } -} \ No newline at end of file +} diff --git a/tests/API/ApiTests.php b/tests/API/ApiTests.php index 3592944c7..62a5cce79 100644 --- a/tests/API/ApiTests.php +++ b/tests/API/ApiTests.php @@ -5,15 +5,15 @@ use josegonzalez\Dotenv\Loader; class ApiTests extends \PHPUnit_Framework_TestCase { + protected function getEnv() { - try { + return self::getEnvStatic(); + } + + protected static function getEnvStatic() { $loader = new Loader('.env'); $loader->parse() ->putenv(true); - } - catch (\InvalidArgumentException $e) { - //ignore - } return [ "GLOBAL_CLIENT_ID" => getenv('GLOBAL_CLIENT_ID'), @@ -27,7 +27,14 @@ protected function getEnv() { } protected function getToken($env, $scopes) { - $generator = new TokenGenerator([ 'client_id' => $env['GLOBAL_CLIENT_ID'], 'client_secret' => $env['GLOBAL_CLIENT_SECRET' ] ]); + return self::getTokenStatic($env, $scopes); + } + + protected static function getTokenStatic($env, $scopes) { + $generator = new TokenGenerator([ + 'client_id' => $env['GLOBAL_CLIENT_ID'], + 'client_secret' => $env['GLOBAL_CLIENT_SECRET'] + ]); return $generator->generate($scopes); } } \ No newline at end of file diff --git a/tests/API/Management/EmailTemplatesTest.php b/tests/API/Management/EmailTemplatesTest.php new file mode 100644 index 000000000..b0118a2d9 --- /dev/null +++ b/tests/API/Management/EmailTemplatesTest.php @@ -0,0 +1,124 @@ + [ + 'actions' => ['create', 'read', 'update'] + ] + ]); + + self::$api = new Management(self::$token, self::$domain); + + try { + self::$gotEmail = self::$api->emailTemplates->get(self::EMAIL_TEMPLATE_NAME); + } catch (ClientException $e) { + if (404 === $e->getCode()) { + self::$mustCreate = true; + } + } + } + + /** + * Test fixture for each method + */ + protected function assertPreConditions() + { + $this->assertNotEmpty(self::$token); + $this->assertInstanceOf(Management::class, self::$api); + } + + /** + * @throws \Exception + */ + public function testGotAnEmail() + { + if (self::$mustCreate) { + self::$gotEmail = self::$api->emailTemplates->create([ + 'template' => self::EMAIL_TEMPLATE_NAME, + 'body' => '

Hi!

', + 'from' => 'test@' . self::$domain, + 'subject' => 'Test email', + 'syntax' => 'liquid', + 'urlLifetimeInSeconds' => 0, + 'enabled' => false, + ]); + } + + $this->assertEquals(self::EMAIL_TEMPLATE_NAME, self::$gotEmail['template']); + } + + /** + * Test updating the email template + * + * @throws \Exception + */ + public function testPatch() + { + $new_subject = 'Email subject ' . time(); + self::$gotEmail = self::$api->emailTemplates->patch(self::EMAIL_TEMPLATE_NAME, [ + 'subject' => $new_subject, + ]); + + $this->assertEquals($new_subject, self::$gotEmail['subject']); + } +}