### Repository https://github.com/programarivm/laravel-api-ddtd-demo <center></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></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></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
author | programarivm |
---|---|
permlink | testing-a-laravel-rest-api-with-sample-data-in-a-csv-file |
category | utopian-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"} |
created | 2018-10-29 23:44:45 |
last_update | 2018-10-30 00:14:57 |
depth | 0 |
children | 5 |
last_payout | 2018-11-05 23:44:45 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.780 HBD |
curator_payout_value | 0.206 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 5,136 |
author_reputation | 2,631,258,794,707 |
root_title | "Testing a Laravel REST API with Sample Data in a CSV File" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 74,306,285 |
net_rshares | 904,557,845,891 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
eforucom | 0 | 3,066,577,434 | 1.06% | ||
catchawhale | 0 | 5,772,463,270 | 2% | ||
pinoy | 0 | 4,592,787,603 | 100% | ||
doughtaker | 0 | 22,207,669,163 | 100% | ||
codingdefined | 0 | 4,383,687,100 | 5% | ||
jadabug | 0 | 645,537,269 | 2.5% | ||
jaff8 | 0 | 68,221,280,197 | 100% | ||
steemtaker | 0 | 769,515,857 | 1% | ||
amosbastian | 0 | 44,472,786,248 | 25.18% | ||
indianteam | 0 | 657,635,340 | 100% | ||
simplymike | 0 | 84,616,900,228 | 60% | ||
hakancelik | 0 | 13,815,280,149 | 20% | ||
acknowledgement | 0 | 753,729,335 | 10% | ||
statsexpert | 0 | 1,298,455,349 | 20% | ||
angelinafx | 0 | 1,281,925,775 | 1% | ||
mythosacademy | 0 | 644,744,690 | 6.5% | ||
coinsandchains | 0 | 3,611,145,907 | 8.15% | ||
walker5 | 0 | 491,664,925 | 100% | ||
lordofreward | 0 | 571,427,738 | 20% | ||
mightypanda | 0 | 60,053,879,930 | 60% | ||
jacekw.dev | 0 | 2,586,901,954 | 50% | ||
bullinachinashop | 0 | 3,504,198,381 | 100% | ||
steem-ua | 0 | 538,889,449,893 | 4.52% | ||
depaldelta | 0 | 252,879,126 | 66% | ||
jackmoksha | 0 | 492,597,335 | 100% | ||
thevoteproject | 0 | 494,567,584 | 100% | ||
nfc | 0 | 11,611,600,213 | 1% | ||
hdu | 0 | 644,785,859 | 5% | ||
curbot | 0 | 3,910,588,972 | 10% | ||
warningapple | 0 | 477,535,861 | 100% | ||
linknotfound | 0 | 490,267,608 | 100% | ||
whitebot | 0 | 19,273,379,598 | 1% |
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/)
author | codingdefined |
---|---|
permlink | re-programarivm-testing-a-laravel-rest-api-with-sample-data-in-a-csv-file-20181031t070435711z |
category | utopian-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"} |
created | 2018-10-31 07:04:36 |
last_update | 2018-10-31 07:04:36 |
depth | 1 |
children | 2 |
last_payout | 2018-11-07 07:04:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 7.231 HBD |
curator_payout_value | 2.333 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 745 |
author_reputation | 529,807,736,257,602 |
root_title | "Testing a Laravel REST API with Sample Data in a CSV File" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 74,390,397 |
net_rshares | 8,379,436,481,794 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
mys | 0 | 4,400,444,167 | 3.45% | ||
utopian-io | 0 | 8,276,633,643,538 | 5.83% | ||
emrebeyler | 0 | 15,108,407,957 | 1% | ||
aydant | 0 | 4,801,159,149 | 50% | ||
amosbastian | 0 | 17,257,545,544 | 8.88% | ||
organicgardener | 0 | 4,860,106,451 | 25% | ||
reazuliqbal | 0 | 4,965,569,750 | 8% | ||
hakancelik | 0 | 13,534,272,531 | 20% | ||
statsexpert | 0 | 6,443,712,879 | 100% | ||
mightypanda | 0 | 29,519,446,130 | 30% | ||
fastandcurious | 0 | 1,912,173,698 | 55% |
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
author | programarivm |
---|---|
permlink | re-codingdefined-re-programarivm-testing-a-laravel-rest-api-with-sample-data-in-a-csv-file-20181031t093726970z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"users":["codingdefined"],"links":["https://github.com/programarivm/data-driven-test-development-demo"],"app":"steemit/0.1"} |
created | 2018-10-31 09:37:27 |
last_update | 2018-10-31 09:37:27 |
depth | 2 |
children | 0 |
last_payout | 2018-11-07 09:37:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 176 |
author_reputation | 2,631,258,794,707 |
root_title | "Testing a Laravel REST API with Sample Data in a CSV File" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 74,397,229 |
net_rshares | 0 |
Thank you for your review, @codingdefined! Keep up the good work!
author | utopian-io |
---|---|
permlink | re-re-programarivm-testing-a-laravel-rest-api-with-sample-data-in-a-csv-file-20181031t070435711z-20181103t010914z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.9"}" |
created | 2018-11-03 01:09:15 |
last_update | 2018-11-03 01:09:15 |
depth | 2 |
children | 0 |
last_payout | 2018-11-10 01:09:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 65 |
author_reputation | 152,955,367,999,756 |
root_title | "Testing a Laravel REST API with Sample Data in a CSV File" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 74,574,127 |
net_rshares | 0 |
#### 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)**
author | steem-ua |
---|---|
permlink | re-testing-a-laravel-rest-api-with-sample-data-in-a-csv-file-20181031t072515z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.9"}" |
created | 2018-10-31 07:25:18 |
last_update | 2018-10-31 07:25:18 |
depth | 1 |
children | 0 |
last_payout | 2018-11-07 07:25:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 291 |
author_reputation | 23,214,230,978,060 |
root_title | "Testing a Laravel REST API with Sample Data in a CSV File" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 74,391,299 |
net_rshares | 0 |
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)!
author | steemitboard |
---|---|
permlink | steemitboard-notify-programarivm-20190814t181053000z |
category | utopian-io |
json_metadata | {"image":["https://steemitboard.com/img/notify.png"]} |
created | 2019-08-14 18:10:54 |
last_update | 2019-08-14 18:10:54 |
depth | 1 |
children | 0 |
last_payout | 2019-08-21 18:10:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 618 |
author_reputation | 38,975,615,169,260 |
root_title | "Testing a Laravel REST API with Sample Data in a CSV File" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 89,527,708 |
net_rshares | 0 |