PayPal - The safer, easier way to pay online!

[ Previous | TOC | Next ]

Testing phase

Preamble

After learning how to develop your application you will need some tests. Within this chapter we will focus on PHPUnit tests. However we do not explain unit tests themselves nor do we explain other test frameworks here. There will be a detailed tutorial chapter on testing later on. We won't discuss special options on PHPUnit here.

Setup your pom

Following the conventions about creating your pom you should already have a dependency on PHPUnit for testing. If you do not have this dependency simply edit your pom and add the following snippet:

<project>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>org.phpunit</groupId>
      <artifactId>phpunit5</artifactId>
      <version>3.3.9</version>
      <scope>test</scope>
    </dependency>
    ...
  </dependencies>
  ...
</project>
			
			

Of course you could use other versions of PHPUnit here as long as they are available in the local repository. Note that the php-maven plugin will not be able to detect the PHPUnit by itself. It requires you to set at least something that is similar to PHPUnit and that can be included by the helper scripts php-maven executes for testing. So you are always able to use another patched version as a dependency.

Writing the unit test

Writing the unit test is easy. Put a file ending with suffix "Test.php" within the folder "src/test/php". For example the file "FooTest.php".

<?php
/**
 * The foo test class
 * 
 * @author mepeisen
 */
class FooTest extends PHPUnit_Framework_TestCase
{
	
	/**
	 * tests the bar function
	 */
	public function testBar()
	{
		include "org/sample/app.php";
		$this->fail('we will fail');
	}
	
}
			

During execution of the test cases php-maven will automatically setup the include path. So you can assume that you always can require the php files of corresponding files within the main sources folder. The PHPUnit framework itself will be included too.

Every test class will be executed. That may not be your intention but you will find out how to invoke only some unit tests later on in this tutorial. So what happens here? There is one test method that includes a file from the main sources and simply fails. Of course this is not a good test if it always fails. But let us write such an always-failing test because you should become familiar on how php-maven and maven itself are handling testing.

Executing the test

To execute the test simply execute the following command:

mvn test

As we expect maven will report a test failure. But as you see the include works and the test itself is executed.

How to ignore failing tests

There are multiple ways to ignore failing tests. The simplest way is to simply skip testing. This can be done by either using a command line option or by editing the configuration section in your pom.xml. For historic reason maven and the surefire-plugin (surefire is focused on java) have two options you may use. Php-Maven supports both of them:

mvn -DskipTests test
mvn -Dmaven.test.skip test
				

Try it and you see that php-maven will report to skip testing (look at the info messages) and know the build succeeds.

Another way is to execute testing but ignore failures. What is the difference? It is simple. Skipping tests will skip everything. That means it will skip even the tests that may succeed. If you already know that one of 500 test cases are failing you may want to execute tests to see if the 499 other tests still succeed. And you may want to create a report of that test result. Maybe it is an alpha version where you want to allow failing tests. There is another command line option to do it this way:

mvn -Dmaven.test.failure.ignore test

As you see maven will still complain that there were test failures. But the main difference is that the build itself will succeed. That is really important because there is no other way to deploy your version to repositories without succeeding tests. So either skip testing at all or ensure that failures are ignored if you really want to deploy something that has test failures. However this is not recommended since it is good practice to ensure that all your tests succeed.

As we said these flags may be inserted into your pom:

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.phpmaven</groupId>
        <artifactId>maven-php-plugin</artifactId>
        <version>2.0-SNAPSHOT</version>
        <extensions>true</extensions>
        <configuration>
          <skipTests>true</skipTests>
          <maven.test.skip>true</maven.test.skip>
          <maven.test.failure.ignore>true</maven.test.failure.ignore>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>
				

Execute single test file

To execute a single test case file you may use the following command:

mvn org.phpmaven:maven-php-plugin:test -DtestFile=phpfile.php -DflushPHPOutput=true

The flushPHPOutput flag will ensure that you see the whole test result rather than writing it silently to the file. If you do not specify this flag the php output will always be invisible from command line. Note: If you specify only the file name "MyTest.php" and you have multiple files with this suffix within different directories all of them will be executed. So you should ensure that you specify at least the relative from starting from target/test-classes.

[ Previous | TOC | Next ]