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

Creating a Simple Testable REST API With Slim Framework — Part 1Ogundiji Bolade AdioBlockedUnblockFollowFollowingJan 19Slim 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.

SetupCreate the project folder.

mkdir mini-library && cd mini-libraryCreate a composer.

json file.

touch composer.

jsonAdd the following contents to your composer.

json file.

composer.

jsonInstall Slim; go to http://www.

slimframework.

com/docs/v3/start/installation.

html to get slim installed into the current directory.

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.

phpAdd the following into the index.

php fileindex.

phpThe 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.

htmlLet’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 publiccd .

/ && php -S 127.

0.

0.

1:3000 -t publicNow visit http://127.

0.

0.

1:3000/You should see this:result in the browserTestingTo 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 runcomposer updateNow we would be creating our test folder and the test files (LibraryTestCase.

php).

mkdir mytests && cd mytests && touch LibraryTestCase.

phpWe would update the content of our LibraryTestCase.

php to include the following:LibraryTestCase.

phpNow 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.

phpcd .

/ && .

/vendor/bin/phpunit .

/mytests/LibraryTestCase.

phpYou should have an output that looks like this:test setup resultWoo!.Our setup looks great.

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

Route : /library; Route : /library/:idNow let’s add our routes and response into the index.

php fileupdated index.

phpNow 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.

phpLet’s add move the following to the libraryRoute.

php file from our index.

php file.

libraryRoute.

phpOur new index.

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

phpNow 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.

jsonNow 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 SlimHttpEnvironment class to mock HTTP headers and build up the Request object from the headers using SlimHttpRequest.

Let’s update the content of our LibraryTestCase.

php to include the following:libraryTestCase.

phpNow let’s run the test.

/vendor/bin/phpunit .

/mytests/LibraryTestCase.

phpCheers!test resultIf 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.

phpThe POST, PUT, PATCH/library/{id}libraryTestCase.

phpThe 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 outputThank you for reading.

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

.. More details

Leave a Reply