create account

Testing a Laravel REST API with Sample Data in a CSV File by programarivm

View this thread on: hive.blogpeakd.comecency.com
· @programarivm · (edited)
$0.99
Testing a Laravel REST API with Sample Data in a CSV File
### Repository
https://github.com/programarivm/laravel-api-ddtd-demo

<center>![premier-league-logo.png](https://cdn.steemitimages.com/DQmVpCbs8SJa4KeHyXuDoRWFa24EgQMVkRouoojfc35gP8f/premier-league-logo.png)</center>

So you have to write a brand new API from scratch and don't know how to start off? If that sounds like you, keep reading! 

I've got some good news. There's no need to scratch your head any more. Today I'd like to encourage you to stick with a step-by-step methodology to build your API.

This post is similar to that one entitled [A Data-Driven Test Development (DDTD) Approach with PHPUnit](https://programarivm.com/data-driven-test-development-demo-ddtd-approach-with-phpunit/) which I published on my blog on a different occasion. However, this one is intended to [Laravel](https://laravel.com/) developers rather than to Symfony developers. 

If you're a Python, Ruby, JavaScript, or any other language or framework programmer, my Laravel repo will hopefully help you understand the important general testing idea as well.

<center>![testing.jpg](https://cdn.steemitimages.com/DQmPzDTua9d6pgmGfmqm1buzBwBkBcRTtS3JDtz8iyybidt/testing.jpg)</center>

### Technology Stack

PHP 7.2
Laravel 5.7
MySQL 5.7
NGINX 1.15
Docker 18.06 CE

> Please read the `README.md` file to learn how to get the app running with Docker.

### Let's Take a Data-Driven Test Development Approach

[programarivm/laravel-api-ddtd-demo](https://github.com/programarivm/laravel-api-ddtd-demo) is a handy boilerplate implementing a data-driven test development (DDTD) approach from minute one, ready to be forked. It is a REST API authenticated with [JWT](https://en.wikipedia.org/wiki/JSON_Web_Token) that performs CRUD operations on the UK Premier League.

The repo is, well, still a demo, meaning it is quite basic – contributions are welcome! The important thing though is the methodology being used. 

This is the very first series of commits I made on Oct 19, 2018:

- first commit
- Docker setup
- first Laravel setup
- Frontend scaffolding removed successfully
- team/create/HttpStatus201Test.php fails
- team/create/HttpStatus201Test.php passes

From then on I decided to continue the project by sending PRs instead of doing `git push origin master`.

Note that apart from not being too relevant in terms of the testing methodology, the first four commits are pretty self-explanatory: first commit, Docker setup, first Laravel setup and Frontend scaffolding removed successfully.

So, let's say that in the beginning we want to program the `/team/create` endpoint. We start our API by writing some sample data, and then making the corresponding test fail:

- [team/create/HttpStatus201Test.php fails](https://github.com/programarivm/laravel-api-ddtd-demo/commit/3ae89016e3b3a97a236256ab30a8e7845b909caa)

The next step consists in refactoring the code until the test passes -- and the data in [laravel-api-ddtd-demo/tests/team/create/data/http_status_201.json](https://github.com/programarivm/laravel-api-ddtd-demo/blob/3ae89016e3b3a97a236256ab30a8e7845b909caa/tests/team/create/data/http_status_201.json) is stored into the database:

- [team/create/HttpStatus201Test.php passes](https://github.com/programarivm/laravel-api-ddtd-demo/commit/74ccc78d2afee70dc05a85016ca10a44b2cb239c)

Remember, this is how to run the tests according to the `README.md`:

```
docker exec -it --user 1000:1000 football_php_fpm php artisan migrate
docker exec -it --user 1000:1000 football_php_fpm php artisan db:seed --class=UsersTableSeeder
docker exec -it --user 1000:1000 football_php_fpm php vendor/bin/phpunit
```

<center>![api.jpg](https://cdn.steemitimages.com/DQmctNiaWAnWGfL2h1hHaBf7oC4Vt6id4PLFk7tzUbGyqHN/api.jpg)</center>

### Roadmap

The step-by-step methodology I mentioned in the beginning of this post consists in completing a TDD cycle as the described below.

- Add a test
- Write something in the test
- Let it fail
- Refactor the code
- Make the test pass

As you can see, we are using sample data in CSV format, and we interact with our API through Laravel's [HTTP Tests](https://laravel.com/docs/5.7/http-tests). 

The same cycle goes for developing more API endpoints:

- `/team/{season}`
- `/team/update/{id}`
- `/team/delete/{id}`
- `/auth`

Here are the PRs implementing the endpoints above through a DDTD cycle:

- [Team/{season} #1](https://github.com/programarivm/laravel-api-ddtd-demo/pull/1)
- [Team/update/{id} #2](https://github.com/programarivm/laravel-api-ddtd-demo/pull/2)
- [Team/delete/{id} #3](https://github.com/programarivm/laravel-api-ddtd-demo/pull/3)
- [Auth #4](https://github.com/programarivm/laravel-api-ddtd-demo/pull/4)

It is always the same process taking place, so feel free to add more endpoints or test more HTTP status codes if you like this project.

### How to Contribute

- Feel free to send a pull request
- Drop an email at info@programarivm.com with the subject "Laravel API DDTD Demo"
- Leave me a comment on [Twitter](https://twitter.com/programarivm)
- Say hello on [Google+](https://plus.google.com/+Programarivm)

Thanks so very much.

### GitHub Account

https://github.com/programarivm
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorprogramarivm
permlinktesting-a-laravel-rest-api-with-sample-data-in-a-csv-file
categoryutopian-io
json_metadata{"tags":["utopian-io","development","php","laravel","testing"],"image":["https://cdn.steemitimages.com/DQmVpCbs8SJa4KeHyXuDoRWFa24EgQMVkRouoojfc35gP8f/premier-league-logo.png","https://cdn.steemitimages.com/DQmPzDTua9d6pgmGfmqm1buzBwBkBcRTtS3JDtz8iyybidt/testing.jpg","https://cdn.steemitimages.com/DQmctNiaWAnWGfL2h1hHaBf7oC4Vt6id4PLFk7tzUbGyqHN/api.jpg"],"links":["https://github.com/programarivm/laravel-api-ddtd-demo","https://programarivm.com/data-driven-test-development-demo-ddtd-approach-with-phpunit/","https://laravel.com/","https://en.wikipedia.org/wiki/JSON_Web_Token","https://github.com/programarivm/laravel-api-ddtd-demo/commit/3ae89016e3b3a97a236256ab30a8e7845b909caa","https://github.com/programarivm/laravel-api-ddtd-demo/blob/3ae89016e3b3a97a236256ab30a8e7845b909caa/tests/team/create/data/http_status_201.json","https://github.com/programarivm/laravel-api-ddtd-demo/commit/74ccc78d2afee70dc05a85016ca10a44b2cb239c","https://laravel.com/docs/5.7/http-tests","https://github.com/programarivm/laravel-api-ddtd-demo/pull/1","https://github.com/programarivm/laravel-api-ddtd-demo/pull/2","https://github.com/programarivm/laravel-api-ddtd-demo/pull/3","https://github.com/programarivm/laravel-api-ddtd-demo/pull/4","https://twitter.com/programarivm","https://plus.google.com/+Programarivm","https://github.com/programarivm"],"app":"steemit/0.1","format":"markdown"}
created2018-10-29 23:44:45
last_update2018-10-30 00:14:57
depth0
children5
last_payout2018-11-05 23:44:45
cashout_time1969-12-31 23:59:59
total_payout_value0.780 HBD
curator_payout_value0.206 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,136
author_reputation2,631,258,794,707
root_title"Testing a Laravel REST API with Sample Data in a CSV File"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,306,285
net_rshares904,557,845,891
author_curate_reward""
vote details (32)
@codingdefined ·
$9.56
Thank you for your contribution. A very well written post. Is it only for Lavarel Rest Api or it can be used with any API, because all REST API needs sample data for testing. It would better if you write concise commits, also comments in your code.

----

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/3/1322234).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
👍  , , , , , , , , , ,
properties (23)
authorcodingdefined
permlinkre-programarivm-testing-a-laravel-rest-api-with-sample-data-in-a-csv-file-20181031t070435711z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/3/1322234","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-10-31 07:04:36
last_update2018-10-31 07:04:36
depth1
children2
last_payout2018-11-07 07:04:36
cashout_time1969-12-31 23:59:59
total_payout_value7.231 HBD
curator_payout_value2.333 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length745
author_reputation529,807,736,257,602
root_title"Testing a Laravel REST API with Sample Data in a CSV File"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,390,397
net_rshares8,379,436,481,794
author_curate_reward""
vote details (11)
@programarivm ·
Hi @codingdefined, thanks for the review. 

It can be used with any API. Here is an example for Symfony apps:

https://github.com/programarivm/data-driven-test-development-demo
properties (22)
authorprogramarivm
permlinkre-codingdefined-re-programarivm-testing-a-laravel-rest-api-with-sample-data-in-a-csv-file-20181031t093726970z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["codingdefined"],"links":["https://github.com/programarivm/data-driven-test-development-demo"],"app":"steemit/0.1"}
created2018-10-31 09:37:27
last_update2018-10-31 09:37:27
depth2
children0
last_payout2018-11-07 09:37:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length176
author_reputation2,631,258,794,707
root_title"Testing a Laravel REST API with Sample Data in a CSV File"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,397,229
net_rshares0
@utopian-io ·
Thank you for your review, @codingdefined! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-programarivm-testing-a-laravel-rest-api-with-sample-data-in-a-csv-file-20181031t070435711z-20181103t010914z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-03 01:09:15
last_update2018-11-03 01:09:15
depth2
children0
last_payout2018-11-10 01:09:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation152,955,367,999,756
root_title"Testing a Laravel REST API with Sample Data in a CSV File"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,574,127
net_rshares0
@steem-ua ·
#### Hi @programarivm!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-testing-a-laravel-rest-api-with-sample-data-in-a-csv-file-20181031t072515z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-10-31 07:25:18
last_update2018-10-31 07:25:18
depth1
children0
last_payout2018-11-07 07:25:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length291
author_reputation23,214,230,978,060
root_title"Testing a Laravel REST API with Sample Data in a CSV File"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id74,391,299
net_rshares0
@steemitboard ·
Congratulations @programarivm! You received a personal award!

<table><tr><td>https://steemitimages.com/70x70/http://steemitboard.com/@programarivm/birthday1.png</td><td>Happy Birthday! - You are on the Steem blockchain for 1 year!</td></tr></table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@programarivm) and compare to others on the [Steem Ranking](https://steemitboard.com/ranking/index.php?name=programarivm)_</sub>


> You can upvote this notification to help all Steem users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-programarivm-20190814t181053000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2019-08-14 18:10:54
last_update2019-08-14 18:10:54
depth1
children0
last_payout2019-08-21 18:10:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length618
author_reputation38,975,615,169,260
root_title"Testing a Laravel REST API with Sample Data in a CSV File"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id89,527,708
net_rshares0