Skip to content

Commit

Permalink
EmailTemplate endpoint tests; phpcs PSR-2 scan whitespace corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Apr 24, 2018
1 parent e368f5b commit a0bf285
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 11 deletions.
13 changes: 8 additions & 5 deletions src/API/Management/EmailTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand All @@ -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
*
Expand All @@ -74,4 +77,4 @@ public function create($data)
->withBody(json_encode($data))
->call();
}
}
}
19 changes: 13 additions & 6 deletions tests/API/ApiTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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);
}
}
124 changes: 124 additions & 0 deletions tests/API/Management/EmailTemplatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Auth0\Tests\API\Management;

use Auth0\SDK\API\Management;
use Auth0\Tests\API\ApiTests;
use GuzzleHttp\Exception\ClientException;

/**
* Class EmailTemplateTest
*
* @package Auth0\Tests\API\Management
*/
class EmailTemplateTest extends ApiTests
{

const EMAIL_TEMPLATE_NAME = 'enrollment_email';

/**
* Management API token with scopes read:email_templates, create:email_templates, update:email_templates
*
* @var string
*/
protected static $token;

/**
* Valid tenant domain
*
* @var string
*/
protected static $domain;

/**
* Auth0 v2 Management API accessor
*
* @var Management
*/
protected static $api;

/**
* Email template retrieved during class setup, tested later
*
* @var array
*/
protected static $gotEmail;

/**
* Can this email template be created?
*
* @var bool
*/
protected static $mustCreate = false;

/**
* Test fixture for class
*
* @throws \Exception
*/
public static function setUpBeforeClass()
{
$env = self::getEnvStatic();

self::$domain = $env['DOMAIN'];
self::$token = self::getTokenStatic($env, [
'email_templates' => [
'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' => '<!doctype html><html><body><h1>Hi!</h1></body></html>',
'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']);
}
}

0 comments on commit a0bf285

Please sign in to comment.