PHP Web Services Development&Testing
Introduction
Web services are one of the most optimal way to organize client-server communication, when both sides use different platforms. Enterprise level platforms (java, .net) offers a lot of tools to optimize design, development and testing, and some of these practices are very useful in small and medium size projects. This article demonstrates how to create web service with php and create automated tests.
Web service will publish three operations: reverse, sum and ping.
Revers accepts one string parameter and returns reversed one.
Ping requires no input parameters and returns unix time.
Sum accepts two integers and returns sum.
Plan
In general we will plan our service first, and then implement it. We will make three global steps:
1. WSDL first - create contract for our service.
2. Implementing Web Service with PHP.
3. Automated WS testing with SoapUI.
Requirements
Next tools are used in this article, they are free and open-source:
PHP 5+
NUSOAP - php library for WS (http://sf.net/projects/nusoap)
Eclipse from eclipse.org, version for EE developers - we will use WSDL editor only.
SoapUI - soapui.org. Web services testing tool.
WSDL first
WSDL is contract. This XML document declares methods supported by service. It also describes types used and parameters for each method.
There are two general ways to start.
1. Develop server logic and push methods to some WS framework. Framework will generate WSDL automatically.
2. Write WSDL in some editor and then implement server logic.
A lot of developers choose first way: in this case they just write code and do not think about web services. From developer perspective this is an advantage.
But this fails for team development - every time when any developer adds method or changes parameters, WSDL also changes. Entire development process becomes unstable, every change requires client code checks.
The other way is to declare contract before real codding. So developers can write both server and client side code in parallel. Also it is much more easier to manage contract changes - if architector changes WSDL he notifies team and everybody knows what to check. This way is "WSDL first".
We will use "WSDL-first" practice to declare our operations.
Start Eclipse and create new project. Open dialog New->Other->Web services->WSDL
Set file name to "ws.wsdl", set target namespace: "http://sample"
And press finish.
You will see wsdl template with one operation "NewOperation".
Drop this operation and add new one "reverse". You may click on arrows to look through parameters, but they are ok by default. This operations requires one string parameter "in" and will return string result.
You should also add ping and sum, and modify their parameters. Do this in Designer mode. Use arrows to open request and response definitions and edit parameters and types. You can open wsdl attached to this article for example.
Save your wsdl as 'ws.wsdl'
Now you have contract to implement and can develop server and client at the same time.
Implementing PHP WS server
Create directory 'soap' under your public_html. Copy 'ws.wsdl' to 'soap'.
Our server code will be placed to ws.php, so http://localhost/soap/ws.php is your service url.
You should also unpack nusoap to 'soap'. After unpacking you get two dirs lib and samples - this one is not important, so you can drop it.
First lines of you php file are:
require('lib/nusoap.php');
$server = new soap_server('ws.wsdl');
Parameter of soap_server is path to wsdl file.
The other one required line is:
$server->service($HTTP_RAW_POST_DATA);
This line actually processes soap requests. Other source lines just implement required operations.
For each operation from wsdl you should create one php function. Names in wsdl and php must be equal.
Here is entire file content:
<?php
require('lib/nusoap.php');
$server = new soap_server('ws.xml');
function reverse($in){
return strrev($in);
}
function ping() {
return time();
}
function sum($a, $b) {
return ($a + $b);
}
$server->service($HTTP_RAW_POST_DATA);
?>
You can open this file in browser: http://localhost/soap/ws.php - and you will see all operations as list.
Now your server is ready to process client requests, but you have no client code! But we will not write any client code and will build automated test cases with soapui.
Testing with SoapUI
SoapUI is great tool for web service developers. It can test server code, emulating multiple client requests. It also can emulate server - check soapui's documentation for this feature.
In our case we test php server code with automatic testing. This job can be executed in several clicks.
Open soapUi and create new project (in File menu). Enter project name and browse our wsdl - ws.wsdl. Do not change any checkboxes, just click ok.
This will create new project and you will see all three operations in list. Use right click to open menu on service and choose 'Generate TestSuite'.
Change style to 'Singe suite ...' and leave all other options unchanged. On pressing OK you will see test suite in list and suite window - you can close it.
Open test suite list and you should see three test steps, one for each operation. As you understand these are automated tests for your service. You can run them all to check entire application logic or can start them one by one manually. We will try to use them manually.
Click on 'reverse' step to open. Two panels are request and response. Request is generated automatically. You should see placeholder for in parameter. Replace ? with some string, for example '123456789'.
Also check if target url is correct - take a look at panel at the top. If it contains url other then 'http://localhost/soap/ws.php' then click it to change.
Now everything is configured. Start step by pressing green arrow.
The result is xml with response and this response must contain reversed string. This was manual check, but we are interested in automatic one.
Take a look at icon next to 'Run' one. This allows you to add conditions and checks when system assumes that everything is right.
Click 'add assertion' and choose condition 'contains'. Now you can enter expected value - 987654321.
Now system checks everything automatically, you can change php code to return other result to make test fail.
Sample
All source code is attached to this article.
ws.php is web service implementation.
ws.wsdl contract.
WS-soapui-project.xml - soap ui project.
lib nusoap libraries.
Download sample codes