Skip to content

Commit

Permalink
chore: add xml example
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Aug 29, 2023
1 parent 9c30bfb commit 7ee934b
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 2 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"friendsofphp/php-cs-fixer": "^3.0",
"php-amqplib/php-amqplib": "^3.0",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": ">=8.5.23 <10"
"phpunit/phpunit": ">=8.5.23 <10",
"tienvx/pact-php-xml": "dev-main"
},
"autoload": {
"psr-4": {
Expand All @@ -53,7 +54,11 @@
"BinaryConsumer\\": "example/binary/consumer/src",
"BinaryConsumer\\Tests\\": "example/binary/consumer/tests",
"BinaryProvider\\": "example/binary/provider/src",
"BinaryProvider\\Tests\\": "example/binary/provider/tests"
"BinaryProvider\\Tests\\": "example/binary/provider/tests",
"XmlConsumer\\": "example/xml/consumer/src",
"XmlConsumer\\Tests\\": "example/xml/consumer/tests",
"XmlProvider\\": "example/xml/provider/src",
"XmlProvider\\Tests\\": "example/xml/provider/tests"
}
},
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions example/xml/consumer/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../../../vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="PhpPact Example Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="PACT_LOGLEVEL" value="DEBUG"/>
</php>
</phpunit>
28 changes: 28 additions & 0 deletions example/xml/consumer/src/Service/HttpClientService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace XmlConsumer\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;

class HttpClientService
{
private Client $httpClient;

private string $baseUri;

public function __construct(string $baseUri)
{
$this->httpClient = new Client();
$this->baseUri = $baseUri;
}

public function getMovies(): string
{
$response = $this->httpClient->get(new Uri("{$this->baseUri}/movies"), [
'headers' => ['Accept' => 'text/xml; charset=UTF8']
]);

return $response->getBody();
}
}
94 changes: 94 additions & 0 deletions example/xml/consumer/tests/Service/HttpClientServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace XmlConsumer\Tests\Service;

use Tienvx\PactPhpXml\Model\Options;
use Tienvx\PactPhpXml\XmlBuilder;
use XmlConsumer\Service\HttpClientService;
use PhpPact\Consumer\InteractionBuilder;
use PhpPact\Consumer\Model\ConsumerRequest;
use PhpPact\Consumer\Model\ProviderResponse;
use PhpPact\Standalone\MockService\MockServerConfig;
use PHPUnit\Framework\TestCase;

class HttpClientServiceTest extends TestCase
{
public function testGetMovies()
{
$request = new ConsumerRequest();
$request
->setMethod('GET')
->setPath('/movies')
->addHeader('Accept', 'text/xml; charset=UTF8');

$xmlBuilder = new XmlBuilder('1.0', 'UTF-8');
$xmlBuilder
->start('movies')
->eachLike('movie')
->start('title')
->contentLike('PHP: Behind the Parser')
->end()
->start('characters')
->eachLike('character', [], new Options(examples: 2))
->start('name')
->contentLike('Ms. Coder')
->end()
->start('actor')
->contentLike('Onlivia Actora')
->end()
->end()
->end()
->start('plot')
->contentLike(
<<<EOF
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
EOF
)
->end()
->start('great-lines')
->eachLike('line')
->contentLike('PHP solves all my web problems')
->end()
->end()
->start('rating', ['type' => 'thumbs'])
->contentLike(7)
->end()
->start('rating', ['type' => 'stars'])
->contentLike(5)
->end()
->end()
->end();

$response = new ProviderResponse();
$response
->setStatus(200)
->addHeader('Content-Type', 'text/xml')
->setBody(
json_encode($xmlBuilder->getArray())
);

$config = new MockServerConfig();
$config
->setConsumer('xmlConsumer')
->setProvider('xmlProvider')
->setPactDir(__DIR__.'/../../../pacts');
if ($logLevel = \getenv('PACT_LOGLEVEL')) {
$config->setLogLevel($logLevel);
}
$builder = new InteractionBuilder($config);
$builder
->given('Movies exist')
->uponReceiving('A get request to /movies')
->with($request)
->willRespondWith($response);

$service = new HttpClientService($config->getBaseUri());
$moviesResult = new \SimpleXMLElement($service->getMovies());
$verifyResult = $builder->verify();

$this->assertTrue($verifyResult);
$this->assertCount(1, $moviesResult);
}
}
122 changes: 122 additions & 0 deletions example/xml/pacts/xmlConsumer-xmlProvider.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"consumer": {
"name": "xmlConsumer"
},
"interactions": [
{
"description": "A get request to /movies",
"providerStates": [
{
"name": "Movies exist"
}
],
"request": {
"headers": {
"Accept": "text/xml; charset=UTF8"
},
"method": "GET",
"path": "/movies"
},
"response": {
"body": "<?xml version='1.0'?><movies><movie><title>PHP: Behind the Parser</title><characters><character><name>Ms. Coder</name><actor>Onlivia Actora</actor></character><character><name/><actor/></character></characters><plot>So, this language. It's like, a programming language. Or is it a\nscripting language? All is revealed in this thrilling horror spoof\nof a documentary.</plot><great-lines><line>PHP solves all my web problems</line></great-lines><rating type='thumbs'>7</rating><rating type='stars'>5</rating></movie></movies>",
"headers": {
"Content-Type": "text/xml"
},
"matchingRules": {
"body": {
"$.movies.movie": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.movies.movie.characters.character": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.movies.movie.characters.character.actor.#text": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.movies.movie.characters.character.name.#text": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.movies.movie.great-lines.line": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.movies.movie.great-lines.line.#text": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.movies.movie.plot.#text": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
},
"$.movies.movie.rating.#text": {
"combine": "AND",
"matchers": [
{
"match": "type"
},
{
"match": "type"
}
]
},
"$.movies.movie.title.#text": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
}
},
"header": {}
},
"status": 200
}
}
],
"metadata": {
"pactRust": {
"ffi": "0.4.7",
"mockserver": "1.2.3",
"models": "1.1.9"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "xmlProvider"
}
}
11 changes: 11 additions & 0 deletions example/xml/provider/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../../../vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="PhpPact Example Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="PACT_LOGLEVEL" value="DEBUG"/>
</php>
</phpunit>
53 changes: 53 additions & 0 deletions example/xml/provider/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

use AaronDDM\XMLBuilder\Writer\XMLWriterService;
use AaronDDM\XMLBuilder\XMLBuilder;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/../../../../vendor/autoload.php';

$xmlWriterService = new XMLWriterService();
$xmlBuilder = new XMLBuilder($xmlWriterService);
$xmlBuilder
->createXMLArray()
->start('movies')
->start('movie')
->add('title', 'PHP: Behind the Parser')
->start('characters')
->start('character')
->add('name', 'Ms. Coder')
->add('actor', 'Onlivia Actora')
->end()
->start('character')
->add('name', 'Mr. Coder')
->add('actor', 'El Act&#211;r')
->end()
->end()
->add('plot', <<<PLOT
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
PLOT)
->start('great-lines')
->add('line', 'PHP solves all my web problems')
->end()
->add('rating', 7, ['type' => 'thumbs'])
->add('rating', 5, ['type' => 'stars'])
->end()
->end();

$app = AppFactory::create();

$app->get('/movies', function (Request $request, Response $response) use ($xmlBuilder) {
$response->getBody()->write($xmlBuilder->getXml());

return $response->withHeader('Content-Type', 'text/xml');
});

$app->post('/pact-change-state', function (Request $request, Response $response) {
return $response;
});

$app->run();
Loading

0 comments on commit 7ee934b

Please sign in to comment.