Setting up PHPUnit and Selenium with IntelliJ IDEA/PhpStorm

You are viewing revision #2 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#1)

  1. Introduction
  2. Prerequisites
  3. Install PHPUnit
  4. Install Selenium
  5. Configure IDEA/PhpStorm
  6. Configure WebTestCase
  7. Work around a PHP Warning issue with PHPUnit and Selenium
  8. Run the the tests
  9. Additional resources

Introduction

This article intends to summarize the steps taken to set up PHPUnit and Selenium for a Yii project, with IntelliJ IDEA Ultimate (the IDE). It was done on Mac OS X 10.6.

The article will most likely also be applicable for PhpStorm, since these two products share the same foundation (IDEA uses the same code for PHP support as PhpStorm has in its core). It should at most be a matter of slightly different menu items and stuff.

Prerequisites

We assume that you have the following:

  • PEAR installed, which is used to install PHPUnit
  • Java installed, which is used to run the .jar file for Selenium
  • Created a Yii application (normally using the command yiic webapp ...). Note: In the examples below this app is in the folder /Users/rawtaz/Sites/yii/test in the filesystem, and the URL to it is http://localhost/~rawtaz/yii/test/
  • Have created a project for that Yii application in your IDE, be it IntelliJ IDEA or PhpStorm

Install PHPUnit

This part will first upgrade PEAR so that it has the latest resources, then configure it so that the PEAR channels are discovered automatically, and finally install PHPUnit and its Selenium support.

Run the following commands in your Terminal (use Spotlight to find the Terminal application in case you haven't used it before):

% sudo pear upgrade PEAR
% sudo pear config-set auto_discover 1
% sudo pear install pear.phpunit.de/PHPUnit
% sudo pear install pear.phpunit.de/PHPUnit_Selenium

Install Selenium

Next we need to install the actual Selenium Server. Download the "Selenium Server" .jar file, e.g. selenium-server-standalone-2.25.0.jar, from the Selenium download page. Save the file wherever it makes sense to you; It is an application file that you will run directly.

Once the file is downloaded, start the Selenium Server by running the following commands in your terminal:

Note: For the sake of example, we assume that the file's name is selenium-server-standalone-2.25.0.jar and that it was saved in the folder ~/jar – change these details as appropriate.

% cd ~/jar
% java -jar selenium-server-standalone-2.25.0.jar

Selenium should now start and run in this Terminal window. The output should look like the following:

% java -jar selenium-server-standalone-2.25.0.jar
2012-sep-02 14:58:17 org.openqa.grid.selenium.GridLauncher main
INFO: Launching a standalone server
14:58:19.373 INFO - Java: Apple Inc. 20.8-b03-424
14:58:19.375 INFO - OS: Mac OS X 10.6.8 x86_64
14:58:19.384 INFO - v2.25.0, with Core v2.25.0. Built from revision 17482
14:58:19.527 INFO - RemoteWebDriver instances should connect to: http://127.94.0.1:4444/wd/hub
14:58:19.528 INFO - Version Jetty/5.1.x
14:58:19.529 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
14:58:19.530 INFO - Started HttpContext[/selenium-server,/selenium-server]
14:58:19.531 INFO - Started HttpContext[/,/]
14:58:19.593 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@13f6ba0f
14:58:19.593 INFO - Started HttpContext[/wd,/wd]
14:58:19.601 INFO - Started SocketListener on 0.0.0.0:4444
14:58:19.602 INFO - Started org.openqa.jetty.jetty.Server@65faba46

Configure IDEA/PhpStorm

Note: These instructions might differ slightly if you are using PhpStorm, but in general it's the same.

Assuming that your project in IDEA/PhpStorm only knows about the Yii application's files, we need to add the Yii Framework files and PHPUnit's files to the so called "include path". The purpose of this is to make IDEA/PhpStorm aware of methods and properties on the various classes from Yii and PHPUnit, so that it can provide code completion and inspection for you.

Open [Preferences] -> Project Settings -> PHP -> Include path and click the + button to add the following two items to the Include path list:

  • The path to the Yii framework folder, for example ~/Sites/yii/yii/framework (this can of course be skipped if you already did this or you have added the Yii framework files as a Content Root in a module)
  • The path to the PHPUnit folder, for example /usr/pkg/lib/php/PHPUnit

*Note: Both of the filesystem paths above might very well be different on your system - you'll have to figure out where PHPUnit was installed and where you have the Yii Framework files (the folder named framework).

Configure WebTestCase

We need to make an initial configuration of the class that is used for functional testing, so that it knows which URL to open using Selenium.

Open the file protected/tests/WebTestCase.php in your Yii projec and edit the TEST_BASE_URL constant so that it points to the URL of the index-test.php file in your Yii project

In this example my URL is http://localhost/~rawtaz/yii/test/index-test.php/, so I changed the constant like this:

define('TEST_BASE_URL','http://localhost/~rawtaz/yii/test/index-test.php/');

Work around a PHP Warning issue with PHPUnit and Selenium

A summary of this problem is in the Yii Forum thread at http://www.yiiframework.com/forum/index.php/topic/30511-firefoxphp-faild-to-include-in-yiibase/. There will most likely be a fix for this on the future, but for now we work around the problem like suggested in the aforementioned forum thread.

  • Open the same file as in the previous step, the protected/tests/WebTestCase.php in your Yii project
  • Add $this->setBrowser('*firefox'); as the last thing in the setUp() method

  • Open the file protected/tests/phpunit.xml in your Yii project
  • Comment out all the <browser .../> tags you can find inside the <selenium>...</selenium> tags

Selenium will now use Firefox for the testing.

Run the the tests

Finally, we try to run the default test for your Yii application (we assume it is still the default one generated by yiic webapp ...), to see that they work:

Run the following commands in a new Terminal window:

% cd ~/Sites/yii/test/protected/tests
% phpunit functional/SiteTest.php

Note: In the example above, the path to the project is ~/Sites/yii/test, but yours might be different – adjust accordingly.

What you should see is something like this:

% phpunit functional/SiteTest.php
PHPUnit 3.6.12 by Sebastian Bergmann.

Configuration read from /Users/rawtaz/Sites/yii/test/protected/tests/phpunit.xml

...

Time: 23 seconds, Memory: 5.50Mb

OK (3 tests, 12 assertions)
%

The three dots in the fourth line of text in the above output means that three tests passed. If one of them had failed there would have been an 'F' instead of a dot for that test.

Additional resources

7 0
10 followers
Viewed: 51 761 times
Version: Unknown (update)
Category: How-tos
Written by: rAWTAZ
Last updated by: rAWTAZ
Created on: Sep 2, 2012
Last updated: 12 years ago
Update Article

Revisions

View all history

Related Articles