Skip to content

2. Debugging and Unit testing

Andrew Hanichkovsky edited this page Feb 15, 2018 · 4 revisions

PHPunit

First time you run PHPunit (right-click phpunit.xml and click run) it will try to scare you with a big dialog box. If you already setup a PHP cli interpreter then just click the Fix button and you should be done. Otherwise follow the PHP interpreter section and select the new interpreter.

Tests are probably going to run but they will mostly fail at this point. There are 2 fixes that need to be made before you can move on :

  1. For PHP to execute database commands properly it our testing environment we need to enable the sqlite driver that comes with it. Enable pdo_sqlite in Laragon's Menu > PHP > Extensions. If you're not planning to run tests locally you're done here.
  2. To run local tests, more specifically the UI ones, you need to swap a variable in your .env file. Change DB_CONNECTION to dusk when you're performing dusk unit tests locally. Otherwise don't touch this and expect only the Feature and Unit test suites to pass, Dusk tests will fail. NOTE : ONLY KEEP THIS CHANGE FOR THE TEST RUNS, YOU SHOULD KEEP IT SET TO MYSQL DATABASE IN ALL OTHER CASES

Writing unit tests

I'll probably hook you up with examples later, just remind me.

For now you can start by taking a look at files in your {path to project root}\tests\ folder

Setting up XDebug with PHPStorm

We'll be mostly using the laravel debugbar and PHPunit because we need CI on our code but its nice to be able to quickly debug things with breakpoints like any type-safe language, so we'll be installing xDebug do deal with it.

Getting the library

  1. Download the appropriate version of xDebug (Yours should be 7.2 64bit VC15 TS) click here for direct link
  2. Move it into your php path inside the ext folder : {path to laragon}\bin\php\{some_php_version}\ext\
  3. Rename it to simply php_xdebug.dll

Changes to php.ini

  1. Open Laragon's menu > PHP and choose php.ini
  2. Copy & Paste these lines to the bottom of the file
       [XDebug]
       xdebug.remote_enable = 1
       xdebug.remote_handler = dbgp
       xdebug.remote_host = localhost
       xdebug.remote_port = 9000
    
  3. Finally Restart Laragon and enable xdebug in Menu > PHP > Extensions.

Quick test : open the window you saw in the PHP interpreter section and refresh your interpreter, it should now say you have xdebug 2.6.0rc1 installed as your debugger. debugger

Configure PHPStorm to work with xDebug --the proper way

  1. Open Run > Edit Configurations
  2. Click +, choose PHP Web Application and write the start url as http://soen341.oo
  3. Click on the 3 dots near server
    • Add a new entry as [name wtvr] with host localhost on port 80 with XDebug
    • Ignore path mapping, we're local
  4. Back in PHP Web App config choose your new server
  5. You can now run debug which will open a new XDebug session and send you to the index page.

Add breakpoints as you wish, dig through objects and step through as much as your heart desires.

Setup autostart xDebug with PHPStorm --the easy way

You can tell PHPStorm to listen for incoming PHP Debug connections so you won't have to always run the file manually and navigate to the correct page.

  1. Copy & paste these extra lines to the bottom of your php.ini
       xdebug.remote_autostart = 1
       xdebug.idekey = "PHPSTORM"
  1. In PHPStorm, find the Tools dropdown > DBGp proxy > Configure
  2. Use the ide key PHPSTORM, host localhost, and port 9000.
  3. In the Run dropdown toogle the Start Listening to PHP Debug Connections at the bottom of the menu

You can now set your breakpoints then switch to the browser and navigate to the pages you want, PHPStorm will pop up on its own when the server hits your breakpoints.