Skip to content

Instantly share code, notes, and snippets.

@matthewbaggett
Last active March 23, 2016 01:21
Show Gist options
  • Save matthewbaggett/53cd9d997698a9802125 to your computer and use it in GitHub Desktop.
Save matthewbaggett/53cd9d997698a9802125 to your computer and use it in GitHub Desktop.
<?php
namespace MYEMPLOYERSNAME\Api\Test\Api\Generated;
use MYEMPLOYERSNAME\Api\Test\RoutesTestCase;
use MYEMPLOYERSNAME\Api\TableGateways;
use MYEMPLOYERSNAME\Api\Exceptions\TableGatewayException;
class AddressEndpointTest extends RoutesTestCase
{
/**
* @depends testAddressCreate
*/
public function testAddressGet($id = 123 // or some shit.)
{
$this->waypoint("Begin");
$response = $this->request("GET", "/v1/addresses/{$id}");
$this->waypoint("API REST REQUEST");
$this->assertEquals(200, $response->getStatusCode());
$body = $response->getBody();
$this->waypoint("Get & Parse Response");
$responseJson = json_decode((string)$body, true);
$this->waypoint("JSON DECODE");
$this->assertArrayHasKey('Status', $responseJson);
$this->assertEquals("OKAY", $responseJson['Status'], "Verify that request to GET /v1/addresses/{$id} returns an \"Status: OKAY\" response. This failed. " . (isset($responseJson['Reason']) ? "Reason: " . $responseJson['Reason'] : "No Reason Given"));
$this->assertArrayHasKey('Address', $responseJson);
$this->waypoint("Some assertions");
$this->validateAddressObject($responseJson['Address']);
$this->waypoint("Validate Object Response");
}
}
. > Waypoint 1 - 0.20ms / 0.21ms Begin
> Waypoint 2 - 0.07ms / 0.29ms *** REQUEST *** BEGIN
> Waypoint 3 - 0.17ms / 0.47ms *** REQUEST *** GET APP
> Waypoint 4 - 0.45ms / 0.94ms *** REQUEST *** MOCK ENV
> Waypoint 5 - 0.70ms / 1.65ms *** REQUEST *** Uri::createFromEnvironment
> Waypoint 6 - 0.63ms / 2.29ms *** REQUEST *** Headers::createFromEnvironment
> Waypoint 7 - 0.20ms / 2.50ms *** REQUEST *** Build Body
> Waypoint 8 - 0.79ms / 3.30ms *** REQUEST *** Ready
> Waypoint 9 - 518.37ms / 521.68ms *** REQUEST *** COMPLETE
> Waypoint 10 - 0.03ms / 521.73ms API REST REQUEST
> Waypoint 11 - 0.29ms / 522.03ms Get & Parse Response
> Waypoint 12 - 0.33ms / 522.37ms JSON DECODE
> Waypoint 13 - 0.60ms / 522.98ms Some assertions
> Waypoint 14 - 2.49ms / 525.48ms Validate Object Response
Segura\Api\Test\Api\Generated\AddressEndpointTest:testAddressGet: Took 0.526 seconds
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="bootstrap.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true">
<php>
<ini name="memory_limit" value="3000M"/>
</php>
<testsuites>
<testsuite name="models">
<directory>tests/Models/</directory>
</testsuite>
<testsuite name="api">
<directory>tests/Api/</directory>
</testsuite>
<testsuite name="apiexplorer">
<directory>tests/ApiExplorer/</directory>
</testsuite>
<testsuite name="delay">
<file>tests/Api/Generated/AddressEndpointTest.php</file>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/clover.xml"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory>tests</directory>
<directory>vendor</directory>
<directory>config</directory>
<directory>build</directory>
<directory>generator</directory>
<directory>views</directory>
</exclude>
</whitelist>
</filter>
<!-- <listeners>
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
< ! - - <listener class="PHPUnit\Runner\CleverAndSmart\TestListener">
<arguments>
<object class="PHPUnit\Runner\CleverAndSmart\Storage\Sqlite3Storage"/>
</arguments>
</listener> - - >
</listeners>-->
</phpunit>
<?php
namespace MYEMPLOYERSNAME\Api\Test;
use Slim\Http\Environment;
use Slim\Http\Headers;
use Slim\Http\Request;
use Slim\Http\RequestBody;
use Slim\Http\Response;
use Slim\Http\Uri;
abstract class RoutesTestCase extends BaseTestCase
{
public function setUp(){
parent::setUp();
}
/**
* @param string $method
* @param string $path
* @param array $post
* @param bool $isJsonRequest
* @return Response
*/
public function request(string $method, string $path, array $post = [], $isJsonRequest = true)
{
/**
* @var \Slim\App $app
*/
$this->waypoint("*** REQUEST *** BEGIN");
$app = $this->getContainer()->get("TestAppInstance");
$this->waypoint("*** REQUEST *** GET APP");
$env = Environment::mock(
[
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => $path,
'REQUEST_METHOD' => $method,
]
);
$this->waypoint("*** REQUEST *** MOCK ENV");
$uri = Uri::createFromEnvironment($env);
$this->waypoint("*** REQUEST *** Uri::createFromEnvironment");
$headers = Headers::createFromEnvironment($env);
$this->waypoint("*** REQUEST *** Headers::createFromEnvironment");
$cookies = [];
$serverParams = $env->all();
$body = new RequestBody();
if (count($post) > 0) {
$body->write(json_encode($post));
$body->rewind();
}
$this->waypoint("*** REQUEST *** Build Body");
$request = new Request($method, $uri, $headers, $cookies, $serverParams, $body);
if ($isJsonRequest) {
$request = $request->withHeader("Content-type", "application/json");
}
$response = new Response();
// Invoke app
$this->waypoint("*** REQUEST *** Ready");
$app($request, $response);
$this->waypoint("*** REQUEST *** COMPLETE");
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment