Development and testing of web services in PHP / SoapUi

    Web services are a fairly convenient method to organize client-server communication, especially if both parties use different platforms. Formalization of communication allows you to uniquely define a dictionary of messages and apply a huge number of existing libraries and techniques. Enterprise platforms (.net, jee) include many utilities for designing, developing and testing web services, and many of these utilities are quite applicable for small projects.

    This article describes the creation of a simple web service in php and its automated testing.

    The web service will implement three operations: line reverse, summation and echo.


    Our plan: first we plan and announce, then we realize it. Thus, we will go through three stages sequentially:

    1. WSDL first - declare our dictionary, or create a contract
    2. Implement a contract for PHP
    3. Automate the testing process


    All open source tools
    PHP 5+
    NUSOAP - php library for working with WS (
    Eclipse from, version for EE developers - we will use only the WSDL editor.
    SoapUI - Test package for web service oriented applications

    WSDL first

    WSDL is a contract, i.e. the agreement under which the server is obligated to serve the client and indicates the parameters of this process. In essence, it is an XML document that describes methods, data types, and other parameters.

    There are two approaches to writing web services:

    1. The developer writes the program code and the platform tools automatically implement WSDL

    2. The designer or architect describes the interfaces in WSDL and implements the logic for them.

    Many developers go the first way, i.e. they write code (java, c #, etc.) and don’t think about web services. From the point of view of the developer, this is a clear plus.
    But this strategy loses a lot in large projects, with several distributed teams. Ideally, we would like to stabilize the interfaces and, for example, let the writing client team not wait for releases from the writing server command. And QA should also start writing tests as early as possible.
    Thus, we come to the need to write WSDL first.

    Create a WSDL using Eclipse:

    After starting the eclipse, create a project New-> Other-> Web services-> WSDL
    File name specify “ws.wsdl”, namespace: “ sample
    (namespace is not related to WS, as it is common practice work with XML)
    After creating you will get a new WSDL with one operation "NewOperation".
    Delete it and create a new one with the name “reverse”. Clicking on the arrows allows you to view all the parameters, but changing them is not necessary.

    The revers operation itself will receive a string from the client and return it in the reverse order of characters, i.e. you need one IN parameter String and the result String. Add them to your operation.

    You can add ping and sum operations with the necessary parameters yourself (or open wsdl from the archive, see the link at the end)
    Save your changes as 'ws.wsdl'
    Now you have a contract, and you can pass it to the developers of the client, server, QA and td All of them can start work independently of each other.

    We implement WS in PHP

    Create the soap directory in your public_html and copy ws.wsdl (accordingly the file is available as yourserver / soap / ws.wsdl )
    We will put the server code in ws.php, and localhost / soap / ws.php will be the link to your web service.
    Additionally unzip nusoap in 'soap'.


    move on to the immediate implementation of the logic: The first lines of your ws.php: This construction tells the script to implement the specified contract. The call itself will be processed: This is all that is needed to support WS, the rest is your logic - business methods. For each operation from the contract, you need to create a php function with an equivalent name. Here is an example of the entire file: If you open in a browser:
    $server = new soap_server('ws.wsdl');


    $server = new soap_server('ws.xml');

    function reverse($in){
    return strrev($in);

    function ping() {
    return time();

    function sum($a, $b) {
    return ($a + $b);



    localhost / soap / ws.php - you will see all the available operations.

    Now your server is ready to serve clients! But there is no client for you ... and we will not write it, as we will use automated testing ...

    (do not forget that we will test the web service, which is integration testing, and will not cancel writing unit tests for business methods)

    Testing with SoapUI

    SoapUI is a great tool for testing and developing systems based on web services. This utility deserves a separate article, so we will use only one of the options - we will test our server by emulating a client.

    To do this, we need a few clicks ...

    Launch soapUi and create a new project, select ws.wsdl as wsdl. Do not change other parameters.

    This will create a project and you will remove all operations in the tree. Right click on the service and select 'Generate TestSuite'. Set the style to 'Singe suite ...'.

    The result will be a test suit in the tree and a package window (you can close it.)

    The test package itself contains a series of steps, one for each operation. As you may have guessed, these are the elements for checking your service. You can run them all at once, or one at a time, depending on your goals.
    We will try to run them one at a time, pre-configured.
    Click on the reverse step, you will see two panels, the request panel and the response panel. The request content is automatically generated based on the contract with question marks at the data place. In our case, we have one placeholder for the line, enter the value 123456789
    Check that the target url at the top of the panel indicates your service and you can start the execution step using the green triangle in the panel.
    The request will go to the server and the second panel will display the response, which should contain 987654321 - the input parameter in the reverse order.
    Visually, we can make sure that our system works correctly, but there is a minus - we see it visually. Ideally, we would like to automate this for regression testing.

    Pay attention to the icon immediately behind 'Run'. Here are the conditions for checking the answer and you can add the condition the presence of line 987654321 in the answer.

    Now you have an automated test that can be performed both manually and by the build server.


    An example in the archive contains
    ws.php implementation of the web service
    ws.wsdl contract
    WS-soapui-project.xml - soap ui project
    lib nusoap library
    You can download here

    Russian version phpws

    Andrew Romanenco

    Also popular now: