Run PHPUnit test on vagrant VM using PHPStorm

To run unit tests on virtual machine using PHPStorm 7.1 IDE is not that hard. I’ll show it by creating new project called “vagrant-test”. First you should start VM using vagrant. How to do this is explained in this post: http://phpforus.com/run-first-application-with-vagrant-in-very-fast-way/

Now our VM is running, so let’s do these steps:

  1. Create new project using PHPStorm.
  2. Let’s start our VM:
    cd _vagrant/
    vagrant up
  3. Create unit test using PHPStorm IDE: right mouse click on project directory “New->PHPUnit->PHPUnit Test”. Let’s call it “helloTest”
    PHPStorm create test window
  4. Click “OK”.
    My test will look like this:

    <?php
    class helloTest extends PHPUnit_Framework_TestCase {
    public function testHelloWorld()
    {
    $this->assertEquals( 2, 2 );
    }
    }
  5. Now we need to download PHPUnit lib, with PHPStorm we can do that easily from IDE. Just go to File->Settings->PHP->PHPUnit, than check radio button “Path to phpunit.phar” and click download link.
    Downloading PHPUnit with PHPStorm
  6. Click “OK”. Now in project directory you should see phpunit.phar file.
  7. Now we need to configure PHPStorm with server information. Go to Tools->Deployment->Configuration. Click “+”, then select type- SFTP and enter all needed information which depends on your VM, check picture below what I entered:
    setup server in PHPStorm
  8. Now click “Mappings”:
    setup server in PHPStormand “OK”.

  9. Now go to “Run->Edit Configurations->Defaults->PHPUnit on Server”. Let’s select newly created server:
    Configurating PHPUnit on PHPStorm
  10. “OK”.
  11. If we did not forgot anything we will run our test on VM using PHPStorm successfully. Right click on unit test file and “Run->helloTest on vagrant…”. We will see something like this:
    Successfully ran unit test on PHPStorm