Skip to content

Commit

Permalink
#8 - To add support for the request data and headers to get passed to…
Browse files Browse the repository at this point in the history
… the tests, Need to still deal with the GET params though. Making another ticket for that.
  • Loading branch information
etiennemarais committed Feb 4, 2016
1 parent 650ca29 commit 71e4c25
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 36 deletions.
22 changes: 17 additions & 5 deletions example/example.apib
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ buying new credits if the threshold is below a certain point.

+ Headers

X-Auth-Token: <ADMIN_API_KEY>
Authorization: Token ApiKey1234

+ Response 200 (application/json)

Expand All @@ -33,6 +33,12 @@ buying new credits if the threshold is below a certain point.
}
}

+ Request

+ Headers

Authorization: Token ApiKey1234

+ Response 402 (application/json)

{
Expand All @@ -44,6 +50,12 @@ buying new credits if the threshold is below a certain point.
}
}

+ Request

+ Headers

Authorization: Token WrongApiKey1234

+ Response 401 (application/json)

{
Expand All @@ -60,7 +72,7 @@ deals with retries and responding to failures in sending, errors and network pro

+ Headers

X-Auth-Token: <API_KEY>
Authorization: Token ApiKey1234

+ Body

Expand Down Expand Up @@ -89,7 +101,7 @@ deals with retries and responding to failures in sending, errors and network pro

+ Headers

X-Auth-Token: <API_KEY>
Authorization: Token ApiKey1234

+ Body

Expand All @@ -108,7 +120,7 @@ deals with retries and responding to failures in sending, errors and network pro

+ Headers

token: badToken
Authorization: Token WrongApiKey1234

+ Response 401 (application/json)

Expand All @@ -121,7 +133,7 @@ deals with retries and responding to failures in sending, errors and network pro

+ Headers

X-Auth-Token: <API_KEY>
Authorization: Token ApiKey1234

+ Body

Expand Down
16 changes: 8 additions & 8 deletions example/generated_tests/FeaturesTest.php
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
<?php

/**
* Generated by OutlineTestGenerator on 2016-02-04 at 10:04:43.
* Generated by OutlineTestGenerator on 2016-02-04 at 15:36:31.
*/
class FeaturesTest extends TestCase
{

public function testGet_Fetching_credits_available_Returns_200()
{
$this->get("/status/credits")
$this->get("/status/credits", [], array ( 'Authorization' => 'Token ApiKey1234', ))
->seeJsonStructure(array ( 'status', 'message', 'data', array ( 'credits_available', ), ))
->assertResponseStatus(200);
}

public function testGet_Fetching_credits_available_Returns_402()
{
$this->get("/status/credits")
$this->get("/status/credits", [], array ( 'Authorization' => 'Token ApiKey1234', ))
->seeJsonStructure(array ( 'status', 'message', 'data', array ( 'credits_available', 'min_threshold', ), ))
->assertResponseStatus(402);
}

public function testGet_Fetching_credits_available_Returns_401()
{
$this->get("/status/credits")
$this->get("/status/credits", [], array ( 'Authorization' => 'Token WrongApiKey1234', ))
->seeJsonStructure(array ( 'status', 'message', ))
->assertResponseStatus(401);
}

public function testPost_Resending_a_code_Returns_200()
{
$this->post("/code/resend")
$this->post("/code/resend", array ( 'client_user_id' => '12345', 'phone_number' => '27848118111', ), array ( 'Content-Type' => 'application/json', 'Authorization' => 'Token ApiKey1234', ))
->seeJsonStructure(array ( 'status', 'message', 'data', array ( 'verification_status', 'expires_at', array ( 'date', 'timezone_type', 'timezone', ), ), ))
->assertResponseStatus(200);
}

public function testPost_Resending_a_code_Returns_400()
{
$this->post("/code/resend")
$this->post("/code/resend", array ( 'phone_number' => '27848118111', ), array ( 'Content-Type' => 'application/json', 'Authorization' => 'Token ApiKey1234', ))
->seeJsonStructure(array ( 'status', 'message', ))
->assertResponseStatus(400);
}

public function testPost_Resending_a_code_Returns_401()
{
$this->post("/code/resend")
$this->post("/code/resend", [], array ( 'Content-Type' => 'application/json', 'Authorization' => 'Token WrongApiKey1234', ))
->seeJsonStructure(array ( 'status', 'message', ))
->assertResponseStatus(401);
}

public function testPost_Resending_a_code_Returns_406()
{
$this->post("/code/resend")
$this->post("/code/resend", array ( 'client_user_id' => '12345', 'phone_number' => 'someBadNumber', ), array ( 'Content-Type' => 'application/json', 'Authorization' => 'Token ApiKey1234', ))
->seeJsonStructure(array ( 'status', 'message', 'data', array ( 'required_format', ), ))
->assertResponseStatus(406);
}
Expand Down
27 changes: 21 additions & 6 deletions src/Outline/Resource/ResourceAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,57 @@ class ResourceAction
{
private $originalExampleActions;
private $testCaseTemplate;
private $params;
private $actionParams;

/**
* @param array $example
* @param array $params
* @param array $actionParams
*/
public function __construct(array $example, array $params)
public function __construct(array $example, array $actionParams)
{
$this->originalExampleActions = $example;
$this->params = $params;
$this->actionParams = $actionParams;
}

/**
* @param Text_Template $testCaseTemplate
* @return ResourceAction
*/
public function withTestCaseTemplate(Text_Template $testCaseTemplate)
{
$this->testCaseTemplate = $testCaseTemplate;
return $this;
}

/**
* @return string
*/
public function getTestCases()
{
$testCases = '';
$actionParams = $this->params;
$actionParams = $this->actionParams;

/* Asuming that each response has only one request */
$request = $this->originalExampleActions['requests'][0];

array_map(function($responses) use ($actionParams, &$testCases) {
array_map(function($responses) use ($actionParams, $request, &$testCases) {
$seeJsonStructure = Arr::replaceWithArrayStringRepresentation(
"->seeJsonStructure(%)",
json_decode($responses['body'], true)
);

$requestData = Arr::getRequestBody($request);
$requestHeaders = Arr::getRequestHeaders($request);

$testCaseVars = [
'methodName' => $actionParams['methodName'] . '_Returns_' . $responses['name'],
'method' => $actionParams['method'],
'methodLabel' => $actionParams['methodLabel'],
'statusCode' => $responses['name'],
'endpoint' => $actionParams['endpoint'],
'seeJsonStructure' => $seeJsonStructure,
'requestData' => $requestData,
'requestHeaders' => $requestHeaders,
];

$this->testCaseTemplate->setVar($testCaseVars);
Expand Down
4 changes: 4 additions & 0 deletions src/Outline/Test/Template/Lumen/LumenTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ public function renderTo($outputTestsPath)
$methodLabel = ucfirst($method);
$endpoint = $resourceAction['attributes']['uriTemplate'];
$methodName = str_replace(' ', '_', $resourceAction['name']);
$getRequestQueryParams = ($method === 'GET')
? $resourceAction['parameters']
: [];

foreach ($resourceAction['examples'] as $example) {
$actionParams = [
'method' => $method,
'methodLabel' => $methodLabel,
'methodName' => $methodName,
'endpoint' => $endpoint,
'queryParams' => $getRequestQueryParams,
];

$testCases .= (new ResourceAction($example, $actionParams))
Expand Down
2 changes: 1 addition & 1 deletion src/Outline/Test/Template/Lumen/stubs/TestCase.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

public function test{methodLabel}_{methodName}()
{
$this->{method}("{endpoint}")
$this->{method}("{endpoint}", {requestData}, {requestHeaders})
{seeJsonStructure}
->assertResponseStatus({statusCode});
}
46 changes: 43 additions & 3 deletions src/Outline/Utilities/Arr/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,51 @@ public static function getKeyStructure(array $array)
*/
public static function replaceWithArrayStringRepresentation($string, array $array)
{
$arrayString = var_export(Arr::getKeyStructure($array), true);
$arrayString = Arr::getKeyStructure($array);

$arrayString = preg_replace("/(\\d+\\s=>)/i", '', $arrayString);
$arrayString = trim(preg_replace('/\s+/', ' ', $arrayString));
$arrayString = self::cleanAndStringifyArray($arrayString);

return str_replace("%", $arrayString, $string);
}

/**
* @param array $request
* @return string
*/
public static function getRequestBody(array $request)
{
$requestBody = json_decode($request['body'], true);

if (is_null($requestBody)) {
return "[]";
}

$requestBody = self::cleanAndStringifyArray($requestBody);

return $requestBody;
}

/**
* @param array $request
* @return string
*/
public static function getRequestHeaders($request)
{
$requestHeaders = array_pluck($request['headers'], 'value', 'name');

$requestHeaders = self::cleanAndStringifyArray($requestHeaders);

return $requestHeaders;
}

/**
* @param $arrayString
* @return mixed|string
*/
private static function cleanAndStringifyArray($arrayString)
{
$arrayString = preg_replace("/(\\d+\\s=>)/i", '', var_export($arrayString, true));
$arrayString = trim(preg_replace('/\s+/', ' ', $arrayString));
return $arrayString;
}
}
22 changes: 17 additions & 5 deletions tests/fixture/fixture.apib
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ buying new credits if the threshold is below a certain point.

+ Headers

X-Auth-Token: <ADMIN_API_KEY>
Authorization: Token ApiKey1234

+ Response 200 (application/json)

Expand All @@ -33,6 +33,12 @@ buying new credits if the threshold is below a certain point.
}
}

+ Request

+ Headers

Authorization: Token ApiKey1234

+ Response 402 (application/json)

{
Expand All @@ -44,6 +50,12 @@ buying new credits if the threshold is below a certain point.
}
}

+ Request

+ Headers

Authorization: Token WrongApiKey1234

+ Response 401 (application/json)

{
Expand All @@ -60,7 +72,7 @@ deals with retries and responding to failures in sending, errors and network pro

+ Headers

X-Auth-Token: <API_KEY>
Authorization: Token ApiKey1234

+ Body

Expand Down Expand Up @@ -89,7 +101,7 @@ deals with retries and responding to failures in sending, errors and network pro

+ Headers

X-Auth-Token: <API_KEY>
Authorization: Token ApiKey1234

+ Body

Expand All @@ -108,7 +120,7 @@ deals with retries and responding to failures in sending, errors and network pro

+ Headers

token: badToken
Authorization: Token WrongApiKey1234

+ Response 401 (application/json)

Expand All @@ -121,7 +133,7 @@ deals with retries and responding to failures in sending, errors and network pro

+ Headers

X-Auth-Token: <API_KEY>
Authorization: Token ApiKey1234

+ Body

Expand Down
Loading

0 comments on commit 71e4c25

Please sign in to comment.