Creating a Simple Testable REST API With Slim Framework — Part 1
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.
- Create the project folder.
mkdir mini-library && cd mini-library
- Create a composer.json file.
- Add the following contents to your composer.json file.
- Install 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
- Create your PHP script, advisable you create a
public/index.phpfile to house your codes.
mkdir public && cd public && touch index.php
- Add the following into the index.php file
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:
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.
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:
Now let’s test our setup, to ensure our LibraryTestCase.php is working properly. So go back to the project directory and run
cd ../ && ./vendor/bin/phpunit ./mytests/LibraryTestCase.php
You should have an output that looks like this:
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
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.
Our new index.php file’s content should now look like this:
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.
Now we can instantiate our route, pass in test requests to simulate the API calls.
Let’s update the content of our LibraryTestCase.php to include the following:
Now let’s run the test
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.
/library endpoint: Let’s update our LibraryTestCase.php to include the following.
The POST, PUT, PATCH
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:
Thank you for reading.
Watch out for Part two, where we would continue with building our library app.