Create a full crud API in Laravel - unit tests part (2)

Agenda
- Introduction
- Steps
- Conclusion
- Youtube suggestion
Introduction
In the previous tutorial, I taught you how to create a fully CRUD API
, let's continue the journey and learn how to use Laragine to test the API
that we created in the previous tutorial.
Steps
We need to do 3 things in phpunit.xml
in the root directory:
- Change the value of the
bootstrap
attribute tovendor/yepwoo/laragine/src/autoload.php
inPHPUnit
tag (it's the same asvendor/autoload.php
but with needed stuff to run the tests correctly in the generated modules and units). - Add the following to the
Unit
test suite:<directory suffix=".php">./core/*/Tests/Feature</directory>
<directory suffix=".php">./plugins/*/Tests/Feature</directory>
- add the following to
Feature
test suite:<directory suffix=".php">./core/*/Tests/Feature</directory>
<directory suffix=".php">./plugins/*/Tests/Feature</directory>
Here’s the full code snippet:

- Let’s go to
PostTest.php
incore/Blog/Tests/Feature
- There’re five main functions in the file:
testItShouldGetListingOfTheResource()
for testing get the lists of the resource.testItShouldStoreNewlyCreatedResource()
for testing create a new resource in the storage.testItShouldGetSpecifiedResource()
for testing get a specified resource.testItShouldUpdateSpecifiedResource()
for testing update a resource in the storagetestItShouldRemoveSpecifiedResource()
for testing remove a resource from the storage
Note
you can edit the unit tests function as you want, we just write the main methods for each CRUD.
Now open your terminal
or git bash
in your project and let's run the test, you can run one of the following commands:
php artisan test
./vendor/bin/phpunit

Conclusion
I hope you found this article helpful. If you need any help please let me know in the comment section.
And that brings us to the end! Thank you so much for reading through this — if this article was helpful in any sense I’d really appreciate a follow to help me reach my 100-follower goal by the end of May 🙂.
Youtube Suggestion
If you want to make videos on Youtube for better clarification please write in a comment, it’ll encourage me to make videos.