Categories
Programming

Testing Laravel In PhpStorm Is Awesome

I’m starting to feel the need for a good test suite now that my codebase is growing larger. I started testing using PHPUnit in the terminal which is fine. However, PhpStorm can make this process much MUCH better!

I thought it would be nice for other Laravel developers to see how to set it up and what the most useful features are.Ā If you are new to testing or you haven’t used the testing features available in PhpStorm continue reading to see why I am so excited about its advanced features.

P.S. There is a video at the bottom of this page if you learn better by screencast. šŸ™‚

The Testing Laravel Setup

On my machine, I use a global Laravel Homestead virtual box with vagrant. If you are using a different development environment on your machine, this setup will likely be different for you. This process was confusing, to say the least. I’m not sure why PhpStorm makes this so difficult. In my opinion, there should be some sort of wizard for this configuration.

Open PhpStorm and do the following.

  • Click Run -> Edit configurations
  • Click the + sign and select PHPUnit to create a new PHPUnit test configuration.
  • Complete the settings here using the directory option.
    • Name it something like All tests
    • Test scope should have “Directory” selected
    • I recommend using the /tests directory in your app. Setting this as the directory will have PhpStorm run all of the tests within the directory when you run tests.
    • You can run specific test classes or specific tests once you are up and running.
    • Check the “Use alternative configuration file” box and add the path to the phpunit.xml file in the root of your Laravel app.
    • Click the wrench icon to the right of the “config file” input to set up your interpreter. (this is where we connect to the Vagrant VM)
      • Select the + icon at the top left of the screen and click the “remote interpreter” option.
        • Select the “Vagrant” option
        • In the “Vagrant Instance Folder” input select your homestead installation folder. (Mine is at /Users/dustin/Homestead) though yours might be different depending on your setup.
        • Once this is done you should be prompted to select an interpreter, select the one you just created and press OK.
      • Under path mappingsĀ input we need to set up the path to the app we are testing. It will automatically point the Homestead directory to the /vagrant directory on your VM but you will need to add your app path too.
        • Click the ellipsisĀ icon at the right of the input and create a link from your host machine code to the virtual machine code.
      • To tell PhpStorm where your PHPUnit library is located you can just point the app to theĀ autoload.php in the /home/vagrant/vendor/autoload.php file in your app with “Use Composer autoloader” selected.

At this point, you should be able to run your tests using PhpStorm.

Why is this better than just using the terminal for testing?

The main things I’m finding that are a huge improvement are as follows.

  1. More visible cues to failed tests. Instead of an output of a stack trace in the terminal, you get a tree view of all of the tests that have run and which ones have failed. You can ignore tests and hide passing tests to just focus on what is broken. Which is really nice.
  2. You can point and click to re-run specific tests that you want instead of running the whole suite of tests or typing in filters to narrow them down.
  3. You can jump to the source code where the test failure is coming from, which really speeds up fixing bugs.
  4. You can run tests with code coverage and get reports and color-coded gutters in the IDE about which lines of code your tests are covering and which lines are not covered.
  5. Maybe the best feature of all is test debugging. Instead of dumping variables within your code or tests you can set break points and step through the test to see all variables at that point.

I have included a quick screencast of the setup process and running a few tests for you to be able to see the process if you are a more visual learner.

If you have any questions or problems comment here or hit me up on twitter twitter.com/dustin_fraker

Leave a Reply

Your email address will not be published. Required fields are marked *