32 articles

How to automatically run PHPUnit tests with Laravel and Homestead

During my previous developments, I used to run a custom script with grunt-contrib-watch that reran my PHPUnit tests each time I saved a PHP file. However Laravel comes with Laravel Mix.

Laravel Mix logo

It is a useful tool, but it lacks (and will always lack) PHPUnit support. So I had to look for another solution and the savior was phpunit-watcher. Install, run and... code. That's it! It comes with useful features and customizations that you can tailor to your needs.

I ran these commands in my Homestead box:

$ composer global require spatie/phpunit-watcher

$ phpunit-watcher watch

phpunit-watcher in action

First time using Laravel Dusk? Here are a few errors and blunders and how to fix them

Fiddling with Laravel Dusk for the first time, I had to install the package:

$ composer require --dev laravel/dusk

Error: Your requirements could not be resolved to an installable set of packages.

I was still using Laravel 5.6 but the current dusk package was in version 5 which needed Laravel 5.7+. I checked Packagist and got a previous version. In my case it was:

$ composer require --dev laravel/dusk:4.0.5

Great, now I can start writing awesome tests! Oops, not so fast!

PHP Fatal error:  Class 'Tests\DuskTestCase' not found

Because I RTFM too fast and skipped:

$ php artisan dusk:install                                                               
Dusk scaffolding installed successfully.

Keep calm and RTFM

Great, now I can really start writing awesome tests!


Facebook\WebDriver\Exception\SessionNotCreatedException: session not created: Chrome version must be between 70 and 73   
  (Driver info: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux 4.4.0-101-generic x86_6

So maybe...

$ sudo apt-get update
$ sudo apt-get install chromium-browser
404  Not Found [IP: ...]
E: Failed to fetch

In that case, update Homestead first. Exit the box, $ vagrant box update and finally install chromium.

$ php artisan dusk

Dusk - PHPUnit failures

YES! Never been so happy to see some good old failures!

How to create symbolic links via FTP

Spoiler: you cannot!

In order to use Laravel File Storage utility, I had to create a symlink in the public folder pointing to a subfolder located in the... storage folder. That is nice and easy when you can ssh to your production server. However, it is a whole other story if you only have FTP access!

So the solution was to let PHP do the work for me, by using the handy symlink function:

if (!file_exists('./storage')) {
    symlink("../storage/app/public", "./storage");

Create symbolic links on server without SSH available?

Insert a line in the middle of an existing configuration file

Playing with Testing a new piece of software using a Vagrant box, I created a in which I had to insert the line sql-mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION inside an existing MySQL config file. I had to put it inside a specific section, so I targeted a line starting with datadir.

The following sed command will add the new line above it. It will update the config file by using a temporary intermediate file.

sed -n 'H;${x;s/^\n//;s/datadir.*$/sql-mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION\n&/;p;}' /your/file.cnf > tmp && mv tmp /your/file.cnf

Accessing your application database from Android Studio

Open the Terminal window (Alt + F12) or:

Open the Terminal window

Then list your devices with the adb command. This will display both running emulators and connected devices:

$ adb devices
List of devices attached
emulator-5554   device

Use the device name to open a connection:

$ adb -s emulator-5554 shell
generic_x86:/ $

To avoid a Permission denied error while trying to access your application files, use the run-as command:

generic_x86:/ $ run-as com.your.package.example sqlite3 databases/yourdatabase.db