Practical Testing in Laravel

Vahid Azamtarrahian

github repo

Unit Testing

  • Important, but only tests individual classes
  • Laravel Framework already has many
  • Many packages already have them inluded
  • Testing in isolation

Testing individual units of souce code (in as much isolation as possible)

Integration Testing

  • Where do you start?
  • Many different ways to do them
  • What is the correct way to do them?
  • Easy to get lost in the weeds

Testing the code together to make sure it's working as it should

Challenges

Email Testing

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_API_KEY=
MAIL_INBOX_ID=

Email Testing

Mailcatcher 

 https://mailcatcher.me/

sudo apt-get install ruby-dev
gem install mailcatcher

Email Testing

Mail Test Case

API Testing

  • PostMan
  • API Test Case Class

Form Testing

  • Leverage Factories for Forms
  • Setup Request with CSRF Token

Authentication/Authorization Testing

  • $this->be()
  • $this->beRole()
  • Leverage Base Classes to do the same action (DRY)

Authentication/Authorization Testing

  • $this->be()
  • $this->beRole()
  • Leverage Base Classes to do the same action (DRY)

Writing Own Assertions

Make sure to write the positive and negative assertion

Testing Private Methods

Namespacing Tests

Otherwise, you can load them all using 'Classmap'

Thanks

Vahid Azamtarrahian

@vahidazam

azamtav

Practical Testing in Laravel

By vahidazam

Practical Testing in Laravel

Many tutorials for testing in Laravel focus on how to write proper unit tests, how to mock objects, and how to manage test databases. While these tutorials cover basic points and sometimes go for the low hanging fruit in testing, we often find ourselves wanting to do more. This talk will focus on the practical side of testing, and show you ways to leverage Laravel's built-in features to accomplish that. It will be more focused on integration testing rather than unit tests.

  • 1,090