Skip to content

Commit

Permalink
Merge pull request #12 from dmt-software/3.0
Browse files Browse the repository at this point in the history
3.0
  • Loading branch information
proggeler authored Apr 6, 2022
2 parents 195ade5 + e6bbaa1 commit 2d4dea3
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 32 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: php
php:
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
install: composer install
script: ./vendor/bin/phpunit --configuration phpunit.xml.dist
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ use DMT\Soap\Serializer\SoapFaultException;
use DMT\VatServiceEu\ClientBuilder;
use DMT\VatServiceEu\Request\CheckVat;
use DMT\VatServiceEu\Response\CheckVatResponse;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;

try {
$request = new CheckVat();
$request->setCountryCode('NL');
$request->setVatNumber('804888644B01');

$client = ClientBuilder::create()->build();
/** @var ClientInterface $client */
/** @var RequestFactoryInterface $requestFactory */
$client = ClientBuilder::create($client, $requestFactory)->build();

/** @var CheckVatResponse $response */
$response = $client->execute($request);

Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@
}
],
"require": {
"php": ">=7.2",
"php": ">=7.4",
"dmt-software/jms-soap-serializer": "^2.1",
"dmt-software/command-bus-validator": "^1.0",
"guzzlehttp/guzzle": "^7.0"
"dmt-software/http-client-middleware": "^0.1.3"
},
"autoload": {
"psr-4": {
"DMT\\VatServiceEu\\": "src/"
}
},
"require-dev": {
"phpunit/phpunit": "^8.5"
"phpunit/phpunit": "^8.5 | ^9.5",
"guzzlehttp/guzzle": "^7.4"
},
"autoload-dev": {
"psr-4": {
"DMT\\Test\\VatServiceEu\\": "tests/"
}
},
"suggest": {
"guzzlehttp/guzzle": "for PSR-18 client implementation",
"guzzlehttp/psr7": "for PSR-17 message factor implementation"
}
}
38 changes: 35 additions & 3 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DMT\VatServiceEu;

use DMT\CommandBus\Validator\ValidationMiddleware;
use DMT\Http\Client\RequestHandler;
use DMT\Soap\Serializer\SoapDateHandler;
use DMT\Soap\Serializer\SoapDeserializationVisitorFactory;
use DMT\Soap\Serializer\SoapMessageEventSubscriber;
Expand All @@ -19,17 +20,48 @@
use League\Tactician\Handler\Locator\CallableLocator;
use League\Tactician\Handler\MethodNameInflector\HandleClassNameWithoutSuffixInflector;
use League\Tactician\Plugins\LockingMiddleware;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;

class ClientBuilder
{
private ClientInterface $client;
private RequestFactoryInterface $requestFactory;

/**
* Create the client builder
*
* @param ClientInterface $client
* @param RequestFactoryInterface $requestFactory
* @return ClientBuilder
*/
public static function create(ClientInterface $client, RequestFactoryInterface $requestFactory): ClientBuilder
{
return (new static())
->setClient($client)
->setRequestFactory($requestFactory);
}

/**
* @param ClientInterface $client
* @return ClientBuilder
*/
public static function create(): ClientBuilder
public function setClient(ClientInterface $client): self
{
return new static();
$this->client = $client;

return $this;
}

/**
* @param RequestFactoryInterface $requestFactory
* @return ClientBuilder
*/
public function setRequestFactory(RequestFactoryInterface $requestFactory): self
{
$this->requestFactory = $requestFactory;

return $this;
}

/**
Expand Down Expand Up @@ -59,7 +91,7 @@ public function build(): Client
*/
public function getCheckVatHandler(): CheckVatHandler
{
return new CheckVatHandler(new \GuzzleHttp\Client(), $this->getSerializer());
return new CheckVatHandler(new RequestHandler($this->client), $this->getSerializer(), $this->requestFactory);
}

/**
Expand Down
42 changes: 27 additions & 15 deletions src/Handler/CheckVatHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DMT\VatServiceEu\Handler;

use DMT\Http\Client\RequestHandlerInterface;
use DMT\VatServiceEu\Request\CheckVat;
use DMT\VatServiceEu\Request\CheckVatApprox;
use DMT\VatServiceEu\Request\RequestInterface;
Expand All @@ -10,6 +11,7 @@
use DMT\VatServiceEu\Response\ResponseInterface;
use GuzzleHttp\Client;
use JMS\Serializer\SerializerInterface;
use Psr\Http\Message\RequestFactoryInterface;

/**
* Class CheckVatHandler
Expand All @@ -19,25 +21,35 @@
class CheckVatHandler
{
/**
* @var Client
* @var RequestHandlerInterface
*/
protected $httpClient;
protected $requestHandler;

/**
* @var SerializerInterface
*/
protected $serializer;

/**
* @var RequestFactoryInterface
*/
protected $requestFactory;

/**
* CheckVatHandler constructor.
*
* @param Client $httpClient
* @param RequestHandlerInterface $httpClient
* @param SerializerInterface $serializer
*/
public function __construct(Client $httpClient, SerializerInterface $serializer)
public function __construct(
RequestHandlerInterface $httpClient,
SerializerInterface $serializer,
RequestFactoryInterface $requestFactory
)
{
$this->httpClient = $httpClient;
$this->requestHandler = $httpClient;
$this->serializer = $serializer;
$this->requestFactory = $requestFactory;
}

/**
Expand Down Expand Up @@ -71,17 +83,17 @@ public function handleCheckVatApprox(CheckVatApprox $checkVatApprox): CheckVatAp
*/
protected function execute(RequestInterface $request, string $responseClass): ResponseInterface
{
$httpRequest = $this->serializer->serialize($request, 'soap');
$httpRequest = $this->requestFactory
->createRequest(
'POST',
'https://ec.europa.eu/taxation_customs/vies/services/checkVatService'
)
->withHeader('Content-Type', 'text/xml; charset=utf-8')
->withHeader( 'SOAPAction', '""');
$httpRequest->getBody()->write($this->serializer->serialize($request, 'soap'));

$httpResponse = $this->httpClient->post(
'https://ec.europa.eu/taxation_customs/vies/services/checkVatService',
[
'SOAPAction' => '""',
'Content-Type' => 'text/xml; charset=utf-8',
'body' => $httpRequest
]
);
$httpResponse = $this->requestHandler->handle($httpRequest);

return $this->serializer->deserialize($httpResponse->getBody(), $responseClass, 'soap');
}
}
}
8 changes: 5 additions & 3 deletions tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@
use DMT\VatServiceEu\Client;
use DMT\VatServiceEu\ClientBuilder;
use DMT\VatServiceEu\Handler\CheckVatHandler;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Psr7\HttpFactory;
use JMS\Serializer\Serializer;
use PHPUnit\Framework\TestCase;

class ClientBuilderTest extends TestCase
{
public function testBuildClient()
{
$this->assertInstanceOf(Client::class, ClientBuilder::create()->build());
$this->assertInstanceOf(Client::class, ClientBuilder::create(new HttpClient(), new HttpFactory())->build());
}

/**
* Check it the correct handler is returned when requested.
*/
public function testHandlerLocator()
{
$this->assertInstanceOf(CheckVatHandler::class, ClientBuilder::create()->getCheckVatHandler());
$this->assertInstanceOf(CheckVatHandler::class, ClientBuilder::create(new HttpClient(), new HttpFactory())->getCheckVatHandler());
}

/**
* Test if the configured soap serializer is returned.
*/
public function testGetSerializer()
{
$this->assertInstanceOf(Serializer::class, ClientBuilder::create()->getSerializer());
$this->assertInstanceOf(Serializer::class, ClientBuilder::create(new HttpClient(), new HttpFactory())->getSerializer());
}
}
4 changes: 3 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use DMT\VatServiceEu\Request\CheckVatApprox;
use DMT\VatServiceEu\Response\CheckVatApproxResponse;
use DMT\VatServiceEu\Response\CheckVatResponse;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Psr7\HttpFactory;
use League\Tactician\CommandBus;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -57,7 +59,7 @@ public function testClientImplementation()
$request->setCountryCode('NL');
$request->setVatNumber('804888644B01');

$client = ClientBuilder::create()->build();
$client = ClientBuilder::create(new HttpClient(), new HttpFactory())->build();

/** @var CheckVatResponse $response */
$response = $client->execute($request);
Expand Down
26 changes: 22 additions & 4 deletions tests/Handler/CheckVatHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DMT\Test\VatServiceEu\Handler;

use DMT\Http\Client\RequestHandler;
use DMT\Soap\Serializer\SoapFaultException;
use DMT\Test\VatServiceEu\Fixtures\MockedResponseTrait;
use DMT\VatServiceEu\ClientBuilder;
Expand All @@ -10,6 +11,7 @@
use DMT\VatServiceEu\Request\CheckVatApprox;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\HttpFactory;
use PHPUnit\Framework\TestCase;

class CheckVatHandlerTest extends TestCase
Expand All @@ -29,7 +31,11 @@ public function testCheckVat()
)
]);

$handler = new CheckVatHandler($httpClient, ClientBuilder::create()->getSerializer());
$handler = new CheckVatHandler(
new RequestHandler($httpClient),
ClientBuilder::create($httpClient, new HttpFactory())->getSerializer(),
new HttpFactory()
);
$response = $handler->handleCheckVat(new CheckVat());

static::assertSame('NL', $response->getCountryCode());
Expand All @@ -47,7 +53,11 @@ public function testCheckVatSoapFaults(\GuzzleHttp\Client $httpClient, string $m
{
static::expectExceptionObject(new SoapFaultException('Server', $message));

$handler = new CheckVatHandler($httpClient, ClientBuilder::create()->getSerializer());
$handler = new CheckVatHandler(
new RequestHandler($httpClient),
ClientBuilder::create($httpClient, new HttpFactory())->getSerializer(),
new HttpFactory()
);
$handler->handleCheckVat(new CheckVat());
}

Expand All @@ -64,7 +74,11 @@ public function testCheckVatApprox()
)
]);

$handler = new CheckVatHandler($httpClient, ClientBuilder::create()->getSerializer());
$handler = new CheckVatHandler(
new RequestHandler($httpClient),
ClientBuilder::create($httpClient, new HttpFactory())->getSerializer(),
new HttpFactory()
);
$response = $handler->handleCheckVatApprox(new CheckVatApprox());

static::assertSame('GB', $response->getCountryCode());
Expand All @@ -82,7 +96,11 @@ public function testCheckVatApproxSoapFaults(\GuzzleHttp\Client $httpClient, str
{
static::expectExceptionObject(new SoapFaultException('Server', $message));

$handler = new CheckVatHandler($httpClient, ClientBuilder::create()->getSerializer());
$handler = new CheckVatHandler(
new RequestHandler($httpClient),
ClientBuilder::create($httpClient, new HttpFactory())->getSerializer(),
new HttpFactory()
);
$handler->handleCheckVatApprox(new CheckVatApprox());
}

Expand Down

0 comments on commit 2d4dea3

Please sign in to comment.