Skip to content

Commit d39d8ec

Browse files
committed
tests: Add a simple SOAP test server
1 parent 8a10ea4 commit d39d8ec

File tree

3 files changed

+153
-59
lines changed

3 files changed

+153
-59
lines changed

tests/FunctionalTest.php

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
use React\EventLoop\Loop;
1010
use React\Http\Browser;
1111

12-
class BankResponse
12+
class AddResponse
1313
{
14+
public readonly int $additionResult;
1415
}
1516

1617
/**
@@ -28,14 +29,37 @@ class FunctionalTest extends TestCase
2829
*/
2930
private $client;
3031

32+
// set up server once for all test cases
33+
private static $serverProcess;
34+
3135
// download WSDL file only once for all test cases
3236
private static $wsdl;
37+
38+
/**
39+
* @beforeClass
40+
*/
41+
public static function setUpServerBeforeClass()
42+
{
43+
$listenAddress = '127.0.0.1:8000';
44+
$serverRoot = __DIR__;
45+
$serverStartCommand = sprintf('php -S %s -t %s >/dev/null 2>&1 &', $listenAddress, $serverRoot);
46+
self::$serverProcess = \proc_open($serverStartCommand, [], $pipes);
47+
48+
// Briefly wait for the server to settle.
49+
sleep(1);
50+
}
51+
52+
public static function tearDownAfterClass(): void
53+
{
54+
proc_close(self::$serverProcess);
55+
}
56+
3357
/**
3458
* @beforeClass
3559
*/
3660
public static function setUpFileBeforeClass()
3761
{
38-
self::$wsdl = file_get_contents('http://www.thomas-bayer.com/axis2/services/BLZService?wsdl');
62+
self::$wsdl = file_get_contents('http://localhost:8000/SimpleSoapServer.php?wsdl');
3963
}
4064

4165
/**
@@ -46,111 +70,91 @@ public function setUpClient()
4670
$this->client = new Client(null, self::$wsdl);
4771
}
4872

49-
public function testBlzService()
73+
public function testSoapService()
5074
{
51-
$this->assertCount(2, $this->client->getFunctions());
52-
$this->assertCount(3, $this->client->getTypes());
75+
$this->assertCount(1, $this->client->getFunctions());
76+
$this->assertCount(2, $this->client->getTypes());
5377

5478
$api = new Proxy($this->client);
5579

56-
$promise = $api->getBank(array('blz' => '12070000'));
80+
$promise = $api->add(['x' => 21, 'y' => 21]);
5781

5882
$result = Block\await($promise, Loop::get());
5983

6084
$this->assertIsObject($result);
61-
$this->assertTrue(isset($result->details));
62-
$this->assertTrue(isset($result->details->bic));
85+
$this->assertEquals(42, $result->additionResult);
6386
}
6487

65-
public function testBlzServiceWithClassmapReturnsExpectedType()
88+
public function testSoapServiceWithClassmapReturnsExpectedType()
6689
{
6790
$this->client = new Client(null, self::$wsdl, array(
6891
'classmap' => array(
69-
'getBankResponseType' => 'Clue\Tests\React\Soap\BankResponse'
92+
'AddResponse' => AddResponse::class
7093
)
7194
));
7295

73-
$this->assertCount(2, $this->client->getFunctions());
74-
$this->assertCount(3, $this->client->getTypes());
96+
$this->assertCount(1, $this->client->getFunctions());
97+
$this->assertCount(2, $this->client->getTypes());
7598

7699
$api = new Proxy($this->client);
77100

78-
$promise = $api->getBank(array('blz' => '12070000'));
101+
$promise = $api->add(['x' => 21, 'y' => 21]);
79102

80103
$result = Block\await($promise, Loop::get());
81104

82-
$this->assertInstanceOf('Clue\Tests\React\Soap\BankResponse', $result);
83-
$this->assertTrue(isset($result->details));
84-
$this->assertTrue(isset($result->details->bic));
105+
$this->assertInstanceOf(AddResponse::class, $result);
106+
$this->assertEquals(42, $result->additionResult);
85107
}
86108

87-
public function testBlzServiceWithSoapV12()
109+
public function testSoapServiceWithSoapV12()
88110
{
89111
$this->client = new Client(null, self::$wsdl, array(
90112
'soap_version' => SOAP_1_2
91113
));
92114

93-
$this->assertCount(2, $this->client->getFunctions());
94-
$this->assertCount(3, $this->client->getTypes());
115+
$this->assertCount(1, $this->client->getFunctions());
116+
$this->assertCount(2, $this->client->getTypes());
95117

96118
$api = new Proxy($this->client);
97119

98-
$promise = $api->getBank(array('blz' => '12070000'));
120+
$promise = $api->add(['x' => 21, 'y' => 21]);
99121

100122
$result = Block\await($promise, Loop::get());
101123

102124
$this->assertIsObject($result);
103-
$this->assertTrue(isset($result->details));
104-
$this->assertTrue(isset($result->details->bic));
125+
$this->assertEquals(42, $result->additionResult);
105126
}
106127

107-
public function testBlzServiceNonWsdlModeReturnedWithoutOuterResultStructure()
128+
public function testSoapServiceNonWsdlMode()
108129
{
109130
$this->client = new Client(null, null, array(
110-
'location' => 'http://www.thomas-bayer.com/axis2/services/BLZService',
111-
'uri' => 'http://thomas-bayer.com/blz/',
131+
'location' => 'http://localhost:8000/SimpleSoapServer.php',
132+
'uri' => 'http://localhost:8000/SimpleSoapServer.php'
112133
));
113134

114135
$api = new Proxy($this->client);
115-
116-
// try encoding the "blz" parameter with the correct namespace (see uri)
117-
// $promise = $api->getBank(new SoapParam('12070000', 'ns1:blz'));
118-
$promise = $api->getBank(new \SoapVar('12070000', XSD_STRING, null, null, 'blz', 'http://thomas-bayer.com/blz/'));
136+
137+
$promise = $api->add((object)['x' => 21, 'y' => 21]);
119138

120139
$result = Block\await($promise, Loop::get());
121140

122-
$this->assertIsObject($result);
123-
$this->assertFalse(isset($result->details));
124-
$this->assertTrue(isset($result->bic));
141+
$this->assertEquals(42, $result->additionResult);
125142
}
126-
127-
public function testBlzServiceWithRedirectLocationRejectsWithRuntimeException()
143+
public function testSoapServiceWithRedirectLocationRejectsWithRuntimeException()
128144
{
129145
$this->client = new Client(null, null, array(
130-
'location' => 'http://httpbingo.org/redirect-to?url=' . rawurlencode('http://www.thomas-bayer.com/axis2/services/BLZService'),
131-
'uri' => 'http://thomas-bayer.com/blz/',
146+
'location' => 'http://httpbin.org/redirect-to?url=' . rawurlencode('http://localhost:8000/SimpleSoapServer.php/'),
147+
'uri' => 'http://localhost:8000/SimpleSoapServer.php/',
132148
));
133149

134150
$api = new Proxy($this->client);
135-
$promise = $api->getBank('a');
151+
$promise = $api->add(['x' => 21, 'y' => 21]);
136152

137153
$this->expectException(\RuntimeException::class);
138154
$this->expectExceptionMessage('redirects');
139-
Block\await($promise, Loop::get());
140-
}
141-
142-
public function testBlzServiceWithInvalidBlzRejectsWithSoapFault()
143-
{
144-
$api = new Proxy($this->client);
145-
146-
$promise = $api->getBank(array('blz' => 'invalid'));
147-
148-
$this->expectException(\SoapFault::class);
149-
$this->expectExceptionMessage('Keine Bank zur BLZ invalid gefunden!');
150-
Block\await($promise, Loop::get());
155+
Async\await($promise);
151156
}
152-
153-
public function testBlzServiceWithInvalidMethodRejectsWithSoapFault()
157+
public function testSoapServiceWithInvalidMethodRejectsWithSoapFault()
154158
{
155159
$api = new Proxy($this->client);
156160

@@ -165,7 +169,7 @@ public function testCancelMethodRejectsWithRuntimeException()
165169
{
166170
$api = new Proxy($this->client);
167171

168-
$promise = $api->getBank(array('blz' => '12070000'));
172+
$promise = $api->add(['x' => 21, 'y' => 21]);
169173
$promise->cancel();
170174

171175
$this->expectException(\RuntimeException::class);
@@ -181,7 +185,7 @@ public function testTimeoutRejectsWithRuntimeException()
181185
$this->client = new Client($browser, self::$wsdl);
182186
$api = new Proxy($this->client);
183187

184-
$promise = $api->getBank(array('blz' => '12070000'));
188+
$promise = $api->add(['x' => 21, 'y' => 21]);
185189

186190
$this->expectException(\RuntimeException::class);
187191
$this->expectExceptionMessage('timed out');
@@ -190,13 +194,13 @@ public function testTimeoutRejectsWithRuntimeException()
190194

191195
public function testGetLocationForFunctionName()
192196
{
193-
$this->assertEquals('http://www.thomas-bayer.com/axis2/services/BLZService', $this->client->getLocation('getBank'));
194-
$this->assertEquals('http://www.thomas-bayer.com/axis2/services/BLZService', $this->client->getLocation('getBank'));
197+
$this->assertEquals('http://localhost:8000/SimpleSoapServer.php', $this->client->getLocation('add'));
198+
$this->assertEquals('http://localhost:8000/SimpleSoapServer.php', $this->client->getLocation('add'));
195199
}
196200

197201
public function testGetLocationForFunctionNumber()
198202
{
199-
$this->assertEquals('http://www.thomas-bayer.com/axis2/services/BLZService', $this->client->getLocation(0));
203+
$this->assertEquals('http://localhost:8000/SimpleSoapServer.php', $this->client->getLocation(0));
200204
}
201205

202206
public function testGetLocationOfUnknownFunctionNameFails()
@@ -208,7 +212,7 @@ public function testGetLocationOfUnknownFunctionNameFails()
208212
public function testGetLocationForUnknownFunctionNumberFails()
209213
{
210214
$this->expectException(\SoapFault::class);
211-
$this->assertEquals('http://www.thomas-bayer.com/axis2/services/BLZService', $this->client->getLocation(100));
215+
$this->assertEquals('http://localhost:8000/SimpleSoapServer.php', $this->client->getLocation(100));
212216
}
213217

214218
public function testGetLocationWithExplicitLocationOptionReturnsAsIs()
@@ -233,7 +237,7 @@ public function testWithLocationInvalidRejectsWithRuntimeException()
233237
{
234238
$api = new Proxy($this->client->withLocation('http://nonsense.invalid'));
235239

236-
$promise = $api->getBank(array('blz' => '12070000'));
240+
$promise = $api->add(['x' => 21, 'y' => 21]);
237241

238242
$this->expectException(\RuntimeException::class);
239243
Block\await($promise, Loop::get());
@@ -246,9 +250,10 @@ public function testWithLocationRestoredToOriginalResolves()
246250
$client = $client->withLocation($original);
247251
$api = new Proxy($client);
248252

249-
$promise = $api->getBank(array('blz' => '12070000'));
253+
$promise = $api->add(['x' => 21, 'y' => 21]);
250254

251255
$result = Block\await($promise, Loop::get());
252256
$this->assertIsObject($result);
257+
$this->assertEquals(42, $result->additionResult);
253258
}
254259
}

tests/SimpleSoapServer.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
ini_set("soap.wsdl_cache_enabled","0");
4+
5+
class AdditionResult {
6+
public function __construct(public readonly int $additionResult) {}
7+
}
8+
9+
class SoapService {
10+
public function add(object $intsToSum) {
11+
return new AdditionResult($intsToSum->x + $intsToSum->y);
12+
}
13+
}
14+
15+
// Create SOAP server with WSDL
16+
$server = new SoapServer(__DIR__ . '/SimpleSoapServer.wsdl', array(
17+
'soap_version' => SOAP_1_2,
18+
'trace' => true
19+
));
20+
21+
// Set the class that handles the SOAP requests
22+
$server->setClass('SoapService');
23+
24+
// Handle the request
25+
$server->handle();

tests/SimpleSoapServer.wsdl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<definitions name="SimpleSoapServer"
3+
targetNamespace="SimpleSoapServer"
4+
xmlns="http://schemas.xmlsoap.org/wsdl/"
5+
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
6+
xmlns:tns="SimpleSoapServer"
7+
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
8+
9+
<types>
10+
<schema targetNamespace="SimpleSoapServer"
11+
xmlns="http://www.w3.org/2001/XMLSchema">
12+
<element name="Add">
13+
<complexType>
14+
<sequence>
15+
<element name="x" type="xsd:int"/>
16+
<element name="y" type="xsd:int"/>
17+
</sequence>
18+
</complexType>
19+
</element>
20+
<element name="AddResponse">
21+
<complexType>
22+
<sequence>
23+
<element name="additionResult" type="xsd:int"/>
24+
</sequence>
25+
</complexType>
26+
</element>
27+
</schema>
28+
</types>
29+
30+
<message name="AddRequest">
31+
<part name="parameters" element="tns:Add"/>
32+
</message>
33+
34+
<message name="AddResponse">
35+
<part name="parameters" element="tns:AddResponse"/>
36+
</message>
37+
38+
<portType name="SimpleSoapServerPortType">
39+
<operation name="add">
40+
<input message="tns:AddRequest"/>
41+
<output message="tns:AddResponse"/>
42+
</operation>
43+
</portType>
44+
45+
<binding name="SimpleSoapServerBinding" type="tns:SimpleSoapServerPortType">
46+
<soap12:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
47+
<operation name="add">
48+
<soap12:operation soapAction="http://localhost:8000/SimpleSoapServer.php#add"/>
49+
<input>
50+
<soap12:body use="literal" namespace="SimpleSoapServer"/>
51+
</input>
52+
<output>
53+
<soap12:body use="literal" namespace="SimpleSoapServer"/>
54+
</output>
55+
</operation>
56+
</binding>
57+
58+
<service name="SimpleSoapServer">
59+
<port name="SimpleSoapServerPort" binding="tns:SimpleSoapServerBinding">
60+
<soap12:address location="http://localhost:8000/SimpleSoapServer.php"/>
61+
</port>
62+
</service>
63+
64+
</definitions>

0 commit comments

Comments
 (0)