Skip to content

Commit 8e809c3

Browse files
authored
Merge pull request #38 from veewee/service-location
Add additional service selection criteria
2 parents 80c3f57 + 1d75648 commit 8e809c3

File tree

5 files changed

+158
-2
lines changed

5 files changed

+158
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ This service will be converted into metadata you, as a human, can understand.
8787
You can apply additional service selection criteria that will be used to find the SOAP service you prefer. By default,
8888

8989
* The selection criteria allows any SOAP service. You can disable e.g. the HTTP-based SOAP services.
90-
* no SOAP version is preferred. The first SOAP service the system detects, will be selected. But you can specify a specific soap version as well.
91-
90+
* No SOAP version is preferred. The first SOAP service the system detects, will be selected. But you can specify a specific soap version as well.
91+
* You can specify a specific service name. If you don't, the first service will be selected.
92+
* You can specify a specific port name. If you don't, the first port will be selected.
9293

9394
### WSDL2
9495

src/Locator/ServiceSelectionCriteria.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ final class ServiceSelectionCriteria
99
{
1010
public ?SoapVersion $preferredSoapVersion;
1111
public bool $allowHttpPorts;
12+
public ?string $serviceName = null;
13+
public ?string $portName = null;
1214

1315
public function __construct()
1416
{
1517
$this->preferredSoapVersion = null;
1618
$this->allowHttpPorts = true;
19+
$this->serviceName = null;
20+
$this->portName = null;
1721
}
1822

1923
public static function defaults(): self
@@ -36,4 +40,20 @@ public function withPreferredSoapVersion(?SoapVersion $preferredSoapVersion = nu
3640

3741
return $new;
3842
}
43+
44+
public function withServiceName(?string $serviceName = null): self
45+
{
46+
$new = clone $this;
47+
$new->serviceName = $serviceName;
48+
49+
return $new;
50+
}
51+
52+
public function withPortName(?string $portName = null): self
53+
{
54+
$new = clone $this;
55+
$new->portName = $portName;
56+
57+
return $new;
58+
}
3959
}

src/Locator/Wsdl1SelectedServiceLocator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ final class Wsdl1SelectedServiceLocator
1818
public function __invoke(Wsdl1 $wsdl, ServiceSelectionCriteria $criteria): Wsdl1SelectedService
1919
{
2020
foreach ($wsdl->services->items as $service) {
21+
if ($criteria->serviceName !== null && $service->name !== $criteria->serviceName) {
22+
continue;
23+
}
24+
2125
$port = $service->ports->lookupByLookupServiceCriteria($criteria);
2226
$binding = $port->andThen(
2327
static fn (Port $port): Option => $wsdl->bindings->lookupByName($port->binding->localName)

src/Model/Definitions/Ports.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function lookupByLookupServiceCriteria(ServiceSelectionCriteria $criteria
3434
{
3535
$preferredVersion = $criteria->preferredSoapVersion;
3636
$allowHttp = $criteria->allowHttpPorts;
37+
$portName = $criteria->portName;
3738

3839
foreach ($this->items as $port) {
3940
if (!$allowHttp && $port->address->type->isHttp()) {
@@ -44,6 +45,10 @@ public function lookupByLookupServiceCriteria(ServiceSelectionCriteria $criteria
4445
continue;
4546
}
4647

48+
if ($portName !== null && $port->name !== $criteria->portName) {
49+
continue;
50+
}
51+
4752
return some($port);
4853
}
4954

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Soap\WsdlReader\Test\Unit\Locator;
4+
5+
use Closure;
6+
use PHPUnit\Framework\TestCase;
7+
use Soap\Wsdl\Loader\StreamWrapperLoader;
8+
use Soap\WsdlReader\Exception\ServiceException;
9+
use Soap\WsdlReader\Locator\ServiceSelectionCriteria;
10+
use Soap\WsdlReader\Locator\Wsdl1SelectedServiceLocator;
11+
use Soap\WsdlReader\Model\Definitions\SoapVersion;
12+
use Soap\WsdlReader\Model\Service\Wsdl1SelectedService;
13+
use Soap\WsdlReader\Wsdl1Reader;
14+
15+
final class Wsdl1SelectedServiceLocatorTest extends TestCase
16+
{
17+
/**
18+
* @dataProvider provideServiceLocations
19+
*/
20+
public function test_it_can_locate_service(
21+
string $wsdl,
22+
ServiceSelectionCriteria $criteria,
23+
Closure $assert
24+
): void {
25+
26+
$locator = new Wsdl1SelectedServiceLocator();
27+
$wsdl1 = (new Wsdl1Reader(new StreamWrapperLoader()))($wsdl);
28+
29+
$service = $locator($wsdl1, $criteria);
30+
$assert($service);
31+
}
32+
33+
/**
34+
* @dataProvider provideNotLocatableServices
35+
*/
36+
public function test_it_can_not_locate_service(
37+
string $wsdl,
38+
ServiceSelectionCriteria $criteria,
39+
): void {
40+
$this->expectException(ServiceException::class);
41+
42+
$locator = new Wsdl1SelectedServiceLocator();
43+
$wsdl1 = (new Wsdl1Reader(new StreamWrapperLoader()))($wsdl);
44+
45+
$locator($wsdl1, $criteria);
46+
}
47+
48+
public static function provideServiceLocations(): iterable
49+
{
50+
$weatherWs = FIXTURE_DIR . '/wsdl/weather-ws.wsdl';
51+
52+
yield 'first-default' => [
53+
$weatherWs,
54+
ServiceSelectionCriteria::defaults(),
55+
self::assertSoapWeather11Service(...),
56+
];
57+
58+
yield 'first-http' => [
59+
$weatherWs,
60+
ServiceSelectionCriteria::defaults()
61+
->withServiceName('Weather')
62+
->withPortName('WeatherHttpGet')
63+
->withAllowHttpPorts(),
64+
self::assertSoapHttpGetService(...),
65+
];
66+
67+
yield 'first-soap12' => [
68+
$weatherWs,
69+
ServiceSelectionCriteria::defaults()
70+
->withPreferredSoapVersion(SoapVersion::SOAP_12),
71+
self::assertSoapWeather12Service(...),
72+
];
73+
}
74+
75+
public static function provideNotLocatableServices(): iterable
76+
{
77+
$weatherWs = FIXTURE_DIR . '/wsdl/weather-ws.wsdl';
78+
79+
yield 'invalid-service-name' => [
80+
$weatherWs,
81+
ServiceSelectionCriteria::defaults()->withServiceName('invalid'),
82+
];
83+
84+
yield 'invalid-port-name' => [
85+
$weatherWs,
86+
ServiceSelectionCriteria::defaults()->withPortName('invalid'),
87+
];
88+
89+
}
90+
91+
private static function assertSoapWeather11Service(Wsdl1SelectedService $service): void
92+
{
93+
static::assertSame('Weather', $service->service->name);
94+
static::assertSame('WeatherSoap', $service->port->name);
95+
static::assertSame('http://wsf.cdyne.com/WeatherWS/Weather.asmx', $service->port->address->location);
96+
static::assertSame(true, $service->port->address->type->isSoap());
97+
static::assertSame(SoapVersion::SOAP_11, $service->port->address->type->soapVersion());
98+
static::assertSame('WeatherSoap', $service->binding->name);
99+
static::assertSame('WeatherSoap', $service->portType->name);
100+
static::assertSame('GetWeatherInformationSoapIn', $service->messages->items[0]->name);
101+
}
102+
103+
private static function assertSoapWeather12Service(Wsdl1SelectedService $service): void
104+
{
105+
static::assertSame('Weather', $service->service->name);
106+
static::assertSame('WeatherSoap12', $service->port->name);
107+
static::assertSame('http://wsf.cdyne.com/WeatherWS/Weather.asmx', $service->port->address->location);
108+
static::assertSame(true, $service->port->address->type->isSoap());
109+
static::assertSame(SoapVersion::SOAP_12, $service->port->address->type->soapVersion());
110+
static::assertSame('WeatherSoap12', $service->binding->name);
111+
static::assertSame('WeatherSoap', $service->portType->name);
112+
static::assertSame('GetWeatherInformationSoapIn', $service->messages->items[0]->name);
113+
}
114+
115+
private static function assertSoapHttpGetService(Wsdl1SelectedService $service): void
116+
{
117+
static::assertSame('Weather', $service->service->name);
118+
static::assertSame('WeatherHttpGet', $service->port->name);
119+
static::assertSame('http://wsf.cdyne.com/WeatherWS/Weather.asmx', $service->port->address->location);
120+
static::assertSame(false, $service->port->address->type->isSoap());
121+
static::assertSame(null, $service->port->address->type->soapVersion());
122+
static::assertSame('WeatherHttpGet', $service->binding->name);
123+
static::assertSame('WeatherHttpGet', $service->portType->name);
124+
static::assertSame('GetWeatherInformationSoapIn', $service->messages->items[0]->name);
125+
}
126+
}

0 commit comments

Comments
 (0)