-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from veewee/driver-implementations
Add generic drivers and noop transport
- Loading branch information
Showing
9 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |