Writing testable API apps in Slim framework

Note: Writing testable apps with good test cases is a core requirement at Andela. Your App is not good for review if you have not written tests.

App Setup

  • Make and change our current directory to our Todo API folder
mkdir SlimTodo && cd SlimTodo
  • Create a composer file for the project
  • Install Slim
composer require slim/slim "^3.0"
  • Create public/index.php file
php -S 127.0.0.1:8080 -t public

Routes

Request Type: GETEndpoint: /todoStatus: 200Response: “Hello, Todo”Request Type: GETEndpoint: /todo/:idResponse status: 200 or 404Response: “Todo :id” or “Todo Not Found”Request Type: POST, PUT, PATCHEndpoint: /todo/:idResponse status: 200 or 404Response: “Todo :id, updated successfully” or “Todo Not Found”Request Type: DELETEEndpoint: /todo/:idResponse status: 200 or 404Response: “Todo :id, deleted successfully” or “Todo Not Found”

Setup for Testing

composer update
mkdir test
./vendor/bin/phpunit ./test/TodoTest.php
PHPUnit 5.4.8 by Sebastian Bergmann and contributors..                                                      1 / 1 (100%)Time: 45 ms, Memory: 3.25MBOK (1 test, 1 assertion)

Testing our API endpoints

mkdir src
composer dump-autoload
./vendor/bin/phpunit ./test/TodoTest.php
PHPUnit 5.4.8 by Sebastian Bergmann and contributors..                                                      1 / 1 (100%)Time: 51 ms, Memory: 4.50MBOK (1 test, 2 assertions)

--

--

--

Andela is an elite group of software engineers based in Lagos, Nairobi, & NYC. We work as distributed team members for tech companies worldwide. www.andela.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andela

Andela

Andela is an elite group of software engineers based in Lagos, Nairobi, & NYC. We work as distributed team members for tech companies worldwide. www.andela.com

More from Medium

Unit testing permissions in your Laravel API

Using Laravel events in unit tests

Automatically delete read database notifications in Laravel

Mocking, Spying, or Faking your Facades