Only until March 18th: coupon LARAVEL12 for 40% off Yearly/Lifetime membership!

Read more here
Courses

Build Laravel API for Car Parking App: Step-By-Step

Automated Tests with PHPUnit

Ok, so we've created all the functions, but did you think I will leave you without automated testing? We need to make sure that our API is working now, and also will not break with future changes.

Our goal is to cover all endpoints with tests, some of them with success/failure scenarios.

Notice: if you haven't written any tests before, you can also watch my full 2-hour course Laravel Testing for Beginners.

First, we need to prepare the testing database for our tests. For this simple example, I will use SQLite in-memory database, so in the phpunit.xml that comes by default with Laravel, we need to just un-comment what's already there: the variables of DB_CONNECTION and DB_DATABASE.

phpunit.xml

<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

Now, whatever DB operations will be executed in our tests, they will not touch our main database, but rather will execute in memory, in a temporary database.

Now, let's start writing tests, in roughly the same order as we created this app - from the authentication layer. We create a feature test for auth...

The full lesson is only for Premium Members.
Want to access all 10 lessons of this course? (55 min read)

You also get:

  • 71 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord