Dashboard > GreenPepperOpen > Documentation > Executing a Specification in PHP
  GreenPepperOpen Log In View a printable version of the current page.  
  Executing a Specification in PHP
Added by Bertrand Paquet, last edited by Bertrand Paquet on Dec 04, 2008  (view change)
Labels: 
(None)

PHPSud (PHP System Under Developpement)

Introduction

PHPSud is a free add-on for GreenPepper which allow you to use executables specifications on PHP Code.

PHPSud is very easy to use :

  • write yours executables specifications in GreenPepper
  • write yours fixtures in PHP
  • write yours business code in PHP
  • run your executable specification on your code :
    • from GreenPepper
    • from command line with local specifications
    • from command line with remote specifications

Sorry, it doesn't work with PHP4. It's tested with PHP5.

How it's work ?

PHPSud is see by GreenPepper as a Java program, but PHPSud runs your PHP program. A small part of mixed PHP/Java code make the glue between the two languages.

Concretely, for each GreenPepperized page, PHPSud launch a PHP interperter, load your PHP program, forward fixtures request from GreenPepper to your PHP code, and give your PHP code response to GreenPepper. Communication between PHP interpreter and java code is done throuh a socket.

Examples

See the GreenPepper's calulator demo.

You can implement that in PHP like that

<?php

class Calculator {
	private $x;
	
	private $y;
	
	function setX($x) {
		$this->x = $x;
	}
	
	function setY($y) {
		$this->y = $y;
	}
	
	function Sum() {
		return $this->x + $this->y;
	}
	
	function Product() {
		return $this->x * $this->y;
	}
	
	function Quotient() {
		if ($this->y == 0) {
			throw new Exception("Divide by zero");
		}
		return $this->x / $this->y;
	}
}

Compare this code with the corresponding java code. It's very similar !

The PHP files to implements GreenPepper's bank example are in attachements. Look at them !

User guide

Getting PHPSud

If you want to use PHPSud from Maven, you have nothing to do. Maven will download PHPSud for you.
On other case, you have to download it from GreenPepper Maven repo, from here. Take the version with all dependencies packaged in.
Example : greenpepper-extensions-php-1.0-jar-with-dependencies.jar

PHPSud constructor parameters

To use PHPSud, you have to specify in GreenPepper configuration that you want to use it. (See below to see where you have to do that).

PHPSud class is com.greenpepper.phpsud.PHPSud.

PHPSud take three arguments in command line :

  • complete path and filename of PHP interpreter. For example : c:\php5\php.exe. On unix system, you can use default value, PHPSud will use /usr/bin/php.
  • working directory for your php program. For example : c:\myprojet
  • your main PHP file. Generally, this file include all others. This file is loaded by PHPSud with PHP command include_once from specified working directory. For exampe : init.php.

With the GreenPepper SUD syntax :

com.greenpepper.phpsud.PHPSud;c:\php5\php.exe;c:\myprojet;init.php

You can not use import command in your PHP GreenPepperized pages, all PHP script have to be included by your main PHP file.

Using PHPSud from GreenPepper in Confluence

  • Put PHPSud jar (jar with all dependencies) in confluence classpath
  • Create a new System Under Test, use Java Runner, and use PHPSud as described above. No more configuration is necessary
  • Thats'all !!

Using PHPSud from command line on local specifications

On windows, you can use a command like that :

java -cp greenpepper-extensions-php-1.0-SNAPSHOT-jar-with-dependencies.jar com.greenpepper.phpsud.Runner c:\php5\php.exe c:\myproject init.php -o target -s specs

The first three agruments are the three PHPSud constructor argument (see above).
The next argument is GreenPepper Command Line runner arguments. See Executing a Specification in Java for complete arguments description.

You can use this method to run local specifications during Phing builds.

Remark : your GreenPepperized pages have to be 'Set as implemented'.

Using PHPSud on remote specifications

You can use GreenPepper maven plugin to run executable specification on PHP.

To do that :

  • have maven installed and configured
  • have a Confluence / GreenPepper instance ready, hosting your executable specifications
  • create a pom.xml file in the root directory of your project. This file will only be used to run your executable specification. See an example below. greenPepperUid and greenPepperSut parameter can be found on GreenPepper Space Registration config panel. Other properties are self descritptives
  • run command mvn greenpepper:run
  • maven gives you result : failure or success
  • reports are in directory target/greenpepper-reports

Remark : this method can also be used to run executable specifications on local specifications. For example on local specifications retrieven by mvn greenpepper:freeze command.

Remark : your GreenPepperized pages have to be 'Set as implemented'.

Remark : more documentation on GreenPepper maven plugin here.

Pom example :

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>

	<groupId>my.project</groupId>
	<artifactId>php</artifactId>
	<version>1.0-SNAPSHOT</version>
	
	<properties>
		<php>c:\php5\php.exe</php>
		<phpMainFile>init.php</phpMainFile>
		<confluenceUrl><![CDATA[http://confluence:8080/]]></confluenceUrl>
		<greenPepperUid>GreenPepper - Bank</greenPepperUid>
		<greenPepperSut>Bank</greenPepperSut>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>greenpepper-open</groupId>
			<artifactId>greenpepper-core</artifactId>
			<version>2.1</version>
		</dependency>
		<dependency>
			<groupId>greenpepper-open</groupId>
			<artifactId>greenpepper-extensions-php</artifactId>
			<version>1.0</version>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>greenpepper</groupId>
				<artifactId>greenpepper-maven-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
					<reportsDirectory>target/greenpepper-reports</reportsDirectory>
					<systemUnderDevelopment>com.greenpepper.phpsud.PHPSud;${php};${basedir};${phpMainFile}</systemUnderDevelopment>
					<repositories>
						<repository>
							<name>greenpepper</name>
							<type>com.greenpepper.runner.repository.GreenPepperRepository</type>
							<root>
		<![CDATA[${confluenceUrl}/rpc/xmlrpc?handler=greenpepper1&sut=${greenPepperSut}&includeStyle=true]]>
							</root>
							<suites>
								<suite>${greenPepperUid}</suite>
							</suites>
						</repository>
					<!-- 
						<repository>
							<name>greenpepper</name>
							<type>com.greenpepper.repository.FileSystemRepository</type>
							<root>my/path/to/specs</root>
							<tests>
								<test>MyPage.html</test>
							</tests>
						</repository>
					-->					
					</repositories>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>GreenPepper Proximity</id>
			<name>GreenPepper Proximity</name>
			<url>
				http://www.greenpeppersoftware.com/proximity/repository/public
			</url>
		</repository>
	</repositories>

	<pluginRepositories>
		<pluginRepository>
			<id>GreenPepper Proximity</id>
			<name>GreenPepper Proximity</name>
			<url>
				http://www.greenpeppersoftware.com/proximity/repository/public
			</url>
		</pluginRepository>
	</pluginRepositories>
	
</project>

Bugs & Features Request

Please use GreenPepper Jira, use GreenPepper project, Open Source component.

DEMONSTRATION LICENSE - This Confluence site is for demonstration purposes only. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.4.3 Build:#705 Mar 21, 2007) - Bug/feature request - Contact Administrators