Creating a Simple Testable REST API With Slim Framework — Part 1

Ogundiji Bolade Adio

Slim is a super lightweight, and fast framework for simple yet powerful web app development and building APIs in PHP. Slim, aside supporting all HTTP methods, it also contains very handy URL structure with routers, middlewares, bodyparser along with page templates, encrypted cookies and lots more.

Test Driven Development (TDD)

Test Driven Development is a process for writing software that provably satisfies requirements. In practice, the developer start the development process by writing unit-test before writing actual code implementation. By doing this, we ensure that however the code is implemented, it is not negatively impacting the final goal. Below, I’ll demonstrate how you can test your Slim php APIs using a very simple example of a mini library app demo.

Setup

  • Create the project folder.
mkdir mini-library && cd mini-library
  • Create a composer.json file.
touch composer.json
  • Add the following contents to your composer.json file.
composer.json
composer require slim/slim "^3.0"

This command downloads the Slim Framework and its third-party dependencies into your project’s vendor/directory.

  • Create your PHP script, advisable you create a public/index.php file to house your codes.
mkdir public && cd public && touch index.php
  • Add the following into the index.php file
index.php

The above code is the route definition, and we have successfully created our first route/endpoint, to fully understand how the request and response works in Slim Framework, check : http://www.slimframework.com/docs/v3/objects/request.html and http://www.slimframework.com/docs/v3/objects/response.html

Let’s take a look at our created route. First we need to navigate out of the public directory and back into our project root directory and then run php -S 127.0.0.1:8080 -t public

cd ../ && php -S 127.0.0.1:3000 -t public

Now visit http://127.0.0.1:3000/

You should see this:

result in the browser

Testing

To test our created route, first we would need to add PHPUnit into our project dependencies as a development dependency. To do that let’s update our composer.json file.

Then run

composer update

Now we would be creating our test folder and the test files (LibraryTestCase.php).

mkdir mytests && cd mytests && touch LibraryTestCase.php

We would update the content of our LibraryTestCase.php to include the following:

LibraryTestCase.php

Now let’s test our setup, to ensure our LibraryTestCase.php is working properly. So go back to the project directory and run .vendor/bin/phpunit ./mytests/LibraryTestCase.php

cd ../ && ./vendor/bin/phpunit ./mytests/LibraryTestCase.php

You should have an output that looks like this:

test setup result

Woo! Our setup looks great.

The App Routes

Now here are the list of routes, type, and expected responses, we would consider for our mini app.

Route : /library; 
Route : /library/:id

Now let’s add our routes and response into the index.php file

updated index.php

Now that we have PHPUnit test suit setup and running and all our routes have been put in place, how do we test our app APIs?

For our app to be testable we need to get the instance of the app before we call the run method which bootstraps the app for web requests.

Let’s add some changes to our index.php file.

We can create a new folder routes, to house our route class.

mkdir routes && cd routes && touch libraryRoute.php

Let’s add move the following to the libraryRoute.php file from our index.php file.

libraryRoute.php

Our new index.php file’s content should now look like this:

updated index.php

Now note the new namespace in the libraryRoute.php file, we would need to update the composer.json file to tell composer how to autoload files in the routes directory. Let’s add the following code to the composer.json file.

updated composer.json

Now we can instantiate our route, pass in test requests to simulate the API calls.

Let’s test GET request to the app. To do this, we make use of the Slim\Http\Environment class to mock HTTP headers and build up the Request object from the headers using Slim\Http\Request.

Let’s update the content of our LibraryTestCase.php to include the following:

libraryTestCase.php

Now let’s run the test

./vendor/bin/phpunit ./mytests/LibraryTestCase.php

Cheers!

test result

If you get a similar result, congratulations, you just landed your first successful API test using the Slim Framework, if you don’t take your tme to review the steps again, you might have skipped a step.

Now lets continue with other endpoints.

The GET/library endpoint: Let’s update our LibraryTestCase.php to include the following.

libraryTestCase.php

The POST, PUT, PATCH/library/{id}

libraryTestCase.php

The DELETE /library/{id}

Now their you have all your endpoints TDD, note for PUT and PATCH you can recreate the POST for that.

Output should look like this:

final test output

Thank you for reading.

Watch out for Part two, where we would continue with building our library app.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade