📜 ⬆️ ⬇️

Development and testing of web services in PHP / SoapUi

Web services is a fairly convenient method to organize client-server communication, especially if both parties use different platforms. The formalization of communication allows you to uniquely identify a dictionary of messages and apply a huge number of existing libraries and techniques. Enterprise platforms (.net, jee) include many utilities for the design, development and testing of web services, and many of these utilities are quite applicable on 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: reverse lines, summation and echo.

Plan


')
Our plan: first plan and announce, then implement. Thus, we will go through three stages one by one:

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

Requirements



All open source tools
PHP 5+
NUSOAP - php library for working with WS (http://sf.net/projects/nusoap)
Eclipse from eclipse.org, version for EE developers - we will use only the WSDL editor.
SoapUI - soapui.org. Package for testing web service oriented applications

WSDL first



WSDL is a contract, i.e. agreement on which the server undertakes to service the client and specifies the parameters of this process. In essence, this 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 implement the WSDL automatically.

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

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

Create a WSDL using Eclipse:

After running 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 a common practice with XML)
After creation you will receive a new WSDL with a single operation “NewOperation”.
Delete it and create a new one called “reverse”. Clicking on the arrows allows you to view all the parameters, but it is not necessary to change them.

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

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

Implement WS in PHP



Create a soap directory in your public_html and copy ws.wsdl (respectively, the file is available as yourserver / soap / ws.wsdl )
We will place the server code in ws.php, and localhost / soap / ws.php will be a link to your web service.
Optionally unpack nusoap in 'soap'.

Let us turn to the direct implementation of logic:

The first lines of your ws.php:
require('lib/nusoap.php');
$server = new soap_server('ws.wsdl');


This construct tells the script to implement the specified contract. The call itself will be processed:

$server->service($HTTP_RAW_POST_DATA);

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:
<?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);

?>


If you open in the browser: 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 him, as we apply automated testing ...

(Do not forget that we will test the web service, which is an integration test, and does not cancel the writing of unit tests for business methods)

We test with SoapUI



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

For this we need a few clicks ...

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

This will create a project and you will lead all operations in the tree. Click the right button on the service and select 'Generate TestSuite'. Set the style to 'Singe suite ...'.

The result will be test suit in the tree and the 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, having previously configured.
Click on the reverse step, you will see two panels, a query panel and a response panel. Request content is automatically generated based on a contract with question marks in place of the data. In our case, we have one placeholder for the string; we will enter the value 123456789
Check that the target url at the top of the panel indicates your service and you can run a step using the green triangle on the panel.
The request will go to the server and the second panel will display the answer, 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 after the 'Run'. Conditions for checking the response are collected here and you can add a condition to the presence of the line 987654321 in the answer.

You now have an automated test that can be performed either manually or by the build server.

Example



An example in the archive contains
ws.php web service implementation
ws.wsdl contract
WS-soapui-project.xml - soap ui project
lib nusoap library
You can download it here romanenco.com/sites/default/files/phpws.zip

romanenco.com/phpws

Andrew Romanenco

Source: https://habr.com/ru/post/61270/


All Articles