Skip to content

Commit

Permalink
Merge pull request #6 from veewee/driver-implementations
Browse files Browse the repository at this point in the history
Add generic drivers and noop transport
  • Loading branch information
veewee authored Apr 3, 2023
2 parents dd4dd3a + 39b8d07 commit af5e507
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ use Soap\Engine\LazyEngine;
$engine = new LazyEngine(fn () => new SimpleEngine($driver, $transport));
```

## Drivers

This package provides drivers that can be used in a generic way:

### SimpleDriver

The SimpleEngine is a wrapper around a previous defined `Encoder`, `Decoder` and a `Metadata` implementation.

```php
use Soap\Engine\SimpleDriver;

$engine = new SimpleDriver($encoder, $decoder, $metadata);
```

### PartialDriver

The PartialDriver is a wrapper around a previous defined `Encoder`, `Decoder` and a `Metadata` implementation.
It is possible to only provide one of the required items.
When some of the implementations are missing, it will throw a `DriverException` when the driver's method is invoked.

```php
use Soap\Engine\PartialDriver;

$engine = new PartialDriver(metadata: $metadata);
```

## List of available components:

* [ext-soap-engine](https://github.com/php-soap/ext-soap-engine): An engine based on PHP's ext-soap.
Expand Down
14 changes: 14 additions & 0 deletions src/Exception/DriverException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace Soap\Engine\Exception;

final class DriverException extends RuntimeException
{
public static function partialException(string $class): self
{
return new self(
sprintf('The partial driver you are using, does not implement '.$class.'. Please use a different SOAP driver.')
);
}
}
14 changes: 14 additions & 0 deletions src/Exception/TransportException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace Soap\Engine\Exception;

final class TransportException extends RuntimeException
{
public static function noop(): self
{
return new self(
sprintf('The transport you are using is configured not to handle any requests. Please specify a different SOAP transport!')
);
}
}
19 changes: 19 additions & 0 deletions src/NoopTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace Soap\Engine;

use Soap\Engine\Exception\TransportException;
use Soap\Engine\HttpBinding\SoapRequest;
use Soap\Engine\HttpBinding\SoapResponse;

final class NoopTransport implements Transport
{
/**
* @throws TransportException
*/
public function request(SoapRequest $request): SoapResponse
{
throw TransportException::noop();
}
}
55 changes: 55 additions & 0 deletions src/PartialDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);

namespace Soap\Engine;

use Soap\Engine\Exception\DriverException;
use Soap\Engine\HttpBinding\SoapRequest;
use Soap\Engine\HttpBinding\SoapResponse;
use Soap\Engine\Metadata\Metadata;

final class PartialDriver implements Driver
{
public function __construct(
private ?Encoder $encoder = null,
private ?Decoder $decoder = null,
private ?Metadata $metadata = null
) {
}

/**
* @throws DriverException
*/
public function decode(string $method, SoapResponse $response): mixed
{
if (!$this->decoder) {
throw DriverException::partialException(Decoder::class);
}

return $this->decoder->decode($method, $response);
}

/**
* @throws DriverException
*/
public function encode(string $method, array $arguments): SoapRequest
{
if (!$this->encoder) {
throw DriverException::partialException(Encoder::class);
}

return $this->encoder->encode($method, $arguments);
}

/**
* @throws DriverException
*/
public function getMetadata(): Metadata
{
if (!$this->metadata) {
throw DriverException::partialException(Metadata::class);
}

return $this->metadata;
}
}
33 changes: 33 additions & 0 deletions src/SimpleDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);

namespace Soap\Engine;

use Soap\Engine\HttpBinding\SoapRequest;
use Soap\Engine\HttpBinding\SoapResponse;
use Soap\Engine\Metadata\Metadata;

final class SimpleDriver implements Driver
{
public function __construct(
private Encoder $encoder,
private Decoder $decoder,
private Metadata $metadata
) {
}

public function decode(string $method, SoapResponse $response): mixed
{
return $this->decoder->decode($method, $response);
}

public function encode(string $method, array $arguments): SoapRequest
{
return $this->encoder->encode($method, $arguments);
}

public function getMetadata(): Metadata
{
return $this->metadata;
}
}
20 changes: 20 additions & 0 deletions tests/Unit/NoopTransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace SoapTest\Engine;

use PHPUnit\Framework\TestCase;
use Soap\Engine\Exception\TransportException;
use Soap\Engine\HttpBinding\SoapRequest;
use Soap\Engine\NoopTransport;

final class NoopTransportTest extends TestCase
{
public function test_it_doesnt_transport(): void
{
$this->expectException(TransportException::class);

$transport = new NoopTransport();
$transport->request(new SoapRequest('', '', '', SoapRequest::SOAP_1_1));
}
}
68 changes: 68 additions & 0 deletions tests/Unit/PartialDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);

namespace SoapTest\Engine;

use PHPUnit\Framework\TestCase;
use Soap\Engine\Decoder;
use Soap\Engine\Encoder;
use Soap\Engine\Exception\DriverException;
use Soap\Engine\HttpBinding\SoapRequest;
use Soap\Engine\HttpBinding\SoapResponse;
use Soap\Engine\PartialDriver;
use SoapTest\Engine\Fixtures\DummyInMemoryMetadata;

final class PartialDriverTest extends TestCase
{
public function test_it_can_encode(): void
{
$encoder = $this->createStub(Encoder::class);
$encoder->method('encode')->willReturn(
$request = new SoapRequest('', '', '', SoapRequest::SOAP_1_1)
);

$driver = new PartialDriver(encoder: $encoder);
static::assertEquals($request, $driver->encode('method', []));
}

public function test_it_will_not_encode(): void
{
$this->expectException(DriverException::class);

$driver = new PartialDriver();
$driver->encode('method', []);
}

public function test_it_can_decode(): void
{
$decoder = $this->createStub(Decoder::class);
$decoder->method('decode')->willReturn([]);
$driver = new PartialDriver(decoder: $decoder);

static::assertEquals([], $driver->decode('method', new SoapResponse('payload')));
}

public function test_it_will_not_decode(): void
{
$this->expectException(DriverException::class);

$driver = new PartialDriver();
$driver->decode('method', new SoapResponse('payload'));
}

public function test_it_can_provide_metadata(): void
{
$metadata = new DummyInMemoryMetadata();
$driver = new PartialDriver(metadata: $metadata);

static::assertEquals($metadata, $driver->getMetadata());
}

public function test_it_will_not_provide_metadata(): void
{
$this->expectException(DriverException::class);

$driver = new PartialDriver();
$driver->getMetadata();
}
}
46 changes: 46 additions & 0 deletions tests/Unit/SimpleDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace SoapTest\Engine;

use PHPUnit\Framework\TestCase;
use Soap\Engine\Decoder;
use Soap\Engine\Encoder;
use Soap\Engine\HttpBinding\SoapRequest;
use Soap\Engine\HttpBinding\SoapResponse;
use Soap\Engine\SimpleDriver;
use SoapTest\Engine\Fixtures\DummyInMemoryMetadata;

final class SimpleDriverTest extends TestCase
{
private SimpleDriver $driver;
private SoapRequest $request;

protected function setUp(): void
{
$encoder = $this->createStub(Encoder::class);
$encoder->method('encode')->willReturn(
$this->request = new SoapRequest('', '', '', SoapRequest::SOAP_1_1)
);
$decoder = $this->createStub(Decoder::class);
$decoder->method('decode')->willReturn([]);
$metadata = new DummyInMemoryMetadata();

$this->driver = new SimpleDriver($encoder, $decoder, $metadata);
}

public function test_it_can_encode(): void
{
static::assertEquals($this->request, $this->driver->encode('method', []));
}

public function test_it_can_decode(): void
{
static::assertEquals([], $this->driver->decode('method', new SoapResponse('payload')));
}

public function test_it_can_provide_metadata(): void
{
static::assertEquals(new DummyInMemoryMetadata(), $this->driver->getMetadata());
}
}

0 comments on commit af5e507

Please sign in to comment.