Running Phpunit PHAR under Windows

Installing the Phpunit sometimes is not that easy. Especially when we want to do it quickly and under Windows.
There are a couple of ways doing it, but it is not well documented how to use it with a PHAR under Windows. For those who don’t know what is a PHAR, it is a PHp ARchive, which in practice is a zip file containing the whole structure of a site, and you can run it easily like a simple php file.
Running the Phpunit test can be done in several ways: using just command line, or using an external tool like Ant.
First of all you have to place your phpunit.phar in a path ex. c:mypath. Then configure Phpunit xml file. Example:

<phpunit>
  <testsuites>
    <testsuite name="my test">
      <file>c:/work/tests/SomeTest.php</file>
      <file>c:/work/tests/AnotherTest.php</file>
    </testsuite>
  </testsuites>
</phpunit>

I named it tests.xml and put it in folder c:worktests. You can write your own test files and place it wherever you want (in my build file they are placed in c:worktests). Example:

class SomeTest extends PHPUnit_Framework_TestCase
{
    public function testPushAndPop()
    {
        $stack = array();
        $this->assertEquals(0, count($stack));

        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertEquals(1, count($stack));

        $this->assertEquals('foo', array_pop($stack));
        $this->assertEquals(0, count($stack));
    }
}

Now you can run it with:
PATH_TO_PHPphp.exe c:mypathphpunit.phar -c c:workteststests.xml.

That’s it. Additionally, if you want this to be part of an Ant build, there is a simple way doing it:

1. You must have java to be able to run Ant
2. Create a build.xml for Ant. Example:

<project name="MyProject" basedir="PATH_TO_PHPUNIT_PHAR">
	<description>
		simple example build file
	</description>
	<target name="phpunit" description="Run unit tests with PHPUnit">
		<exec executable="PATH_TO_PHP/php.exe" failonerror="true">
			<arg value="phpunit.phar"/>
			<arg value="-c"/>
			<arg value="c:/work/tests/tests.xml"/>
		</exec>
	</target>
</project>

3. Run: ant.bat phpunit

Leave a Reply

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

Time limit is exhausted. Please reload the CAPTCHA.