• Development

    Writing Tests for Drush Commands

    There are plenty of examples of these in the wild, but I figured I’d show a stripped down version of an automated Kernel test that successfully tests a drush command. The trick here is making sure you set up a logger and that you stub a few methods (if you happen to use $this->logger()  and dt()  in your Drush commands). Also featured in this example is the use of Faker to generate realistic test data.

    I learned this via the migrate_tools project here.

  • Development

    Using Data Providers in PHPUnit Tests

    This is the before code, where a single test is run, and each scenario I’m testing could be influenced by the previous scenario (not a good thing, unless that was my goal, which it was not).

    This is the after code, where three tests are run. Unfortunately this takes 3x longer to execute. The upside is that each scenario cannot affect the others and it’s perhaps more readable and easier to add additional scenarios.

     

  • Development

    Testing Cookie Modification in Laravel 8

    If there’s a better way to pass a cookie from one request to another, in a phpunit feature test in Laravel, please let me know! Here’s one way to handle it: