Search by tag: php
6 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.

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
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
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.
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
4)
So maybe...
$ sudo apt-get update
$ sudo apt-get install chromium-browser
[…]
404 Not Found [IP: ...]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/chromium-browser...
In that case, update Homestead first. Exit the box, $ vagrant box update and finally install chromium.
$ php artisan dusk
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");
}
Install PHP CodeSniffer with PEAR
This article should be useful for other tools than CodeSniffer. To put some context, I installed PEAR some time ago for PHPUnit, before I read that PHPUnit had moved away from PEAR and can now be installed using Composer or a simple PHAR (PHp ARchive) file. I had troubles with the default PEAR configuration and the various steps to add the right folders in the system $PATH, PHP configuration, ... and my stubbornness to have everything exactly where I want and behave exactly the way I want!
On my Arch Linux system, PEAR install its packages by default in /usr/bin. I prefer to put them in /usr/share/pear/bin to be more restrictive on what folders I include in my PHP configuration and avoid including the whole /usr/bin in the open_basedir directive. As a reminder, PEAR is installed on Arch Linux with
$ yaourt -S php-pear
and should be available on most distributions through their respective package manager.
First, if, like me, you make 20 mistakes before doing something right, this is the command for uninstalling a package:
$ sudo pear uninstall <packagename></packagename>
To change the PEAR executables directory globally:
$ sudo pear config-set bin_dir /usr/share/pear/bin/
Don't use sudo if you want to apply the changes locally. In this case, it should create, I think automatically, a hidden file named .pearrc in your home directory.
We can check PEAR configuration with
$ sudo pear config-show
We can now install CodeSniffer:
$ sudo pear install PHP_CodeSniffer
and adapt our PATH to find its executable by adding the following in our ~/.bashrc:
PATH=/usr/share/pear/bin:$PATH.
We reload our config and check if the CodeSniffer executable is found in the right place:
$ . ~/.bashrc
$ whereis phpcs
I use the open_basedir directive to add a bit of security. Of course, I don't remember that kind of details but PHP reminds me:
We fix this by editing /etc/php/php.ini or wherever your PHP configuration file is by adding the PEAR folder: open_basedir = /srv/http/:/tmp/:/usr/share/pear/.
We can now start refactoring...
$ phpcs path/to/file.php|js|css or $ phpcs path/to/folder
The following command:
$ phpcs -sp --encoding=utf-8 --standard=PSR2 --tab-width=4 --ignore={External/,Tests/coverage/} -v .
- shows sniff codes, e.g.:
(PSR2.Namespaces.UseDeclaration.SpaceAfterLastUse) - shows progress
- specifies that the encoding of the files is utf-8
- only uses the PSR-2 standard
- specifies the number of spaces I use as tab
- ignores the sub folders External and Tests/coverage
- prints one level of verbosity
- and processes the current folder.








