DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Testing Slim Framework Actions

Testing Slim Framework Actions

All in all, it's not too complicated to test a Slim action, and Environment::mock() makes it easy to set up particular test cases. Let me show you how it's done.

Rob Allen user avatar by
Rob Allen
·
Mar. 20, 16 · Web Dev Zone · Tutorial
Like (5)
Save
Tweet
3.78K Views

Join the DZone community and get the full member experience.

Join For Free

To test a Slim Framework action, you need a request and a response object, and mock whatever is in the action. This is one way to do this.

Consider this simple echo action that returns the query parameters to you as a JSON encoded string:

$ curl "http://localhost:8888/echo?foo=bar&this=that"
{"foo":"bar","this":"that"}

This is one of those useful API endpoints that your users can use to check that everything is working as expected.

The Action Under Test

The code for this endpoint is a class called EchoAction which looks like this:

namespace App\Action;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class EchoAction
{
    public function __invoke(Request $request, Response $response, $args = [])
    {
        return $response->withJson($request->getQueryParams());
    }
}

and it is registered with the Slim App like so:

$app->get('/echo', App\Action\EchoAction::class);

Testing

Testing it isn't too complex as there are no dependencies on the EchoAction class itself, so we just have to instantiate the class, invoke it, and write a test.

For a URL of the form /echo?foo=bar, the core test code is:

$action = new \App\Action\EchoAction();
$response = $action($request, $response);
$this->assertSame((string)$response->getBody(), json_encode(['foo' => 'bar']));

Creating the Request and Response

Creating the $response is easy as the constructor parameters all have defaults:

$response = new \Slim\Http\Response();

The $request is a little more complex as it's constructor signature looks like this:

public function __construct(
        $method,
        UriInterface $uri,
        HeadersInterface $headers,
        array $cookies,
        array $serverParams,
        StreamInterface $body,
        array $uploadedFiles = []
    )

However, Slim actually creates a Slim\Http\Request using the static createFromEnvironment() factory method, which takes a Slim\Http\Environment instance. Roughly, it does this:

$request = Request::createFromEnvironment(new Environment($_SERVER));

Setting up a $_SERVER array with the relevant elements can be a little tiresome in testing though. Fortunately, Joshneeded to test Slim itself, so the Environment object has a handy static method called mock() that does this for us.

We use it like this:

$environment = \Slim\Http\Environment::mock([
    'REQUEST_METHOD' => 'GET',
    'REQUEST_URI' => '/echo',
    'QUERY_STRING'=>'foo=bar'
]);

As you can see, mock() takes an array which contains $_SERVER keys that we wish to set up for our particular test. Usually we set the REQUEST_METHOD, REQUEST_URI and, if we need it, QUERY_STRING. We need QUERY_STRING as this is the key in $_SERVER that Request uses to determine the query parameters for the request.

Putting it All Together

Hence, our completed test looks like this:

class EchoActionTest extends \PHPUnit_Framework_TestCase
{
    public function testGetRequestReturnsEcho()
    {
        // instantiate action
        $action = new \App\Action\EchoAction();

        // We need a request and response object to invoke the action
        $environment = \Slim\Http\Environment::mock([
            'REQUEST_METHOD' => 'GET',
            'REQUEST_URI' => '/echo',
            'QUERY_STRING'=>'foo=bar']
        );
        $request = \Slim\Http\Request::createFromEnvironment($environment);
        $response = new \Slim\Http\Response();

        // run the controller action and test it
        $response = $action($request, $response, []);
        $this->assertSame((string)$response->getBody(), '{"foo":"bar"}');
    }
}

All in all, it's not too complicated to test a Slim action, and Environment::mock() makes it easy to set up particular test cases.

Framework Testing

Published at DZone with permission of Rob Allen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Image Classification Using SingleStore DB, Keras, and Tensorflow
  • Pre-Commit Hooks DevOps Engineer Should Know To Control Kubernetes
  • Flutter vs React Native. How to Cover All Mobile Platforms in 2022 With No Hassle
  • How to Perform Visual Regression Testing Using Cypress

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo