Skip to content

Commit cd34fbd

Browse files
committed
chore: add xml example
1 parent 9c30bfb commit cd34fbd

8 files changed

Lines changed: 399 additions & 3 deletions

File tree

composer.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"friendsofphp/php-cs-fixer": "^3.0",
3333
"php-amqplib/php-amqplib": "^3.0",
3434
"phpstan/phpstan": "^1.9",
35-
"phpunit/phpunit": ">=8.5.23 <10"
35+
"phpunit/phpunit": ">=8.5.23 <10",
36+
"tienvx/pact-php-xml": "dev-main"
3637
},
3738
"autoload": {
3839
"psr-4": {
@@ -53,7 +54,11 @@
5354
"BinaryConsumer\\": "example/binary/consumer/src",
5455
"BinaryConsumer\\Tests\\": "example/binary/consumer/tests",
5556
"BinaryProvider\\": "example/binary/provider/src",
56-
"BinaryProvider\\Tests\\": "example/binary/provider/tests"
57+
"BinaryProvider\\Tests\\": "example/binary/provider/tests",
58+
"XmlConsumer\\": "example/xml/consumer/src",
59+
"XmlConsumer\\Tests\\": "example/xml/consumer/tests",
60+
"XmlProvider\\": "example/xml/provider/src",
61+
"XmlProvider\\Tests\\": "example/xml/provider/tests"
5762
}
5863
},
5964
"scripts": {
@@ -101,5 +106,11 @@
101106
"allow-plugins": {
102107
"tienvx/composer-downloads-plugin": true
103108
}
104-
}
109+
},
110+
"repositories": [
111+
{
112+
"type": "vcs",
113+
"url": "https://github.com/tienvx/pact-php-xml.git"
114+
}
115+
]
105116
}

example/xml/consumer/phpunit.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="../../../vendor/autoload.php" colors="true">
3+
<testsuites>
4+
<testsuite name="PhpPact Example Tests">
5+
<directory>./tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<php>
9+
<env name="PACT_LOGLEVEL" value="DEBUG"/>
10+
</php>
11+
</phpunit>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace XmlConsumer\Service;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Psr7\Uri;
7+
8+
class HttpClientService
9+
{
10+
private Client $httpClient;
11+
12+
private string $baseUri;
13+
14+
public function __construct(string $baseUri)
15+
{
16+
$this->httpClient = new Client();
17+
$this->baseUri = $baseUri;
18+
}
19+
20+
public function getMovies(): string
21+
{
22+
$response = $this->httpClient->get(new Uri("{$this->baseUri}/movies"), [
23+
'headers' => ['Accept' => 'text/xml; charset=UTF8']
24+
]);
25+
26+
return $response->getBody();
27+
}
28+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace XmlConsumer\Tests\Service;
4+
5+
use Tienvx\PactPhpXml\Model\Options;
6+
use Tienvx\PactPhpXml\XmlBuilder;
7+
use XmlConsumer\Service\HttpClientService;
8+
use PhpPact\Consumer\InteractionBuilder;
9+
use PhpPact\Consumer\Model\ConsumerRequest;
10+
use PhpPact\Consumer\Model\ProviderResponse;
11+
use PhpPact\Standalone\MockService\MockServerConfig;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class HttpClientServiceTest extends TestCase
15+
{
16+
public function testGetMovies()
17+
{
18+
$request = new ConsumerRequest();
19+
$request
20+
->setMethod('GET')
21+
->setPath('/movies')
22+
->addHeader('Accept', 'text/xml; charset=UTF8');
23+
24+
$xmlBuilder = new XmlBuilder('1.0', 'UTF-8');
25+
$xmlBuilder
26+
->start('movies')
27+
->eachLike('movie')
28+
->start('title')
29+
->contentLike('PHP: Behind the Parser')
30+
->end()
31+
->start('characters')
32+
->eachLike('character', [], new Options(examples: 2))
33+
->start('name')
34+
->contentLike('Ms. Coder')
35+
->end()
36+
->start('actor')
37+
->contentLike('Onlivia Actora')
38+
->end()
39+
->end()
40+
->end()
41+
->start('plot')
42+
->contentLike(
43+
<<<EOF
44+
So, this language. It's like, a programming language. Or is it a
45+
scripting language? All is revealed in this thrilling horror spoof
46+
of a documentary.
47+
EOF
48+
)
49+
->end()
50+
->start('great-lines')
51+
->eachLike('line')
52+
->contentLike('PHP solves all my web problems')
53+
->end()
54+
->end()
55+
->start('rating', ['type' => 'thumbs'])
56+
->contentLike(7)
57+
->end()
58+
->start('rating', ['type' => 'stars'])
59+
->contentLike(5)
60+
->end()
61+
->end()
62+
->end();
63+
64+
$response = new ProviderResponse();
65+
$response
66+
->setStatus(200)
67+
->addHeader('Content-Type', 'text/xml')
68+
->setBody(
69+
json_encode($xmlBuilder->getArray())
70+
);
71+
72+
$config = new MockServerConfig();
73+
$config
74+
->setConsumer('xmlConsumer')
75+
->setProvider('xmlProvider')
76+
->setPactDir(__DIR__.'/../../../pacts');
77+
if ($logLevel = \getenv('PACT_LOGLEVEL')) {
78+
$config->setLogLevel($logLevel);
79+
}
80+
$builder = new InteractionBuilder($config);
81+
$builder
82+
->given('Movies exist')
83+
->uponReceiving('A get request to /movies')
84+
->with($request)
85+
->willRespondWith($response);
86+
87+
$service = new HttpClientService($config->getBaseUri());
88+
$moviesResult = new \SimpleXMLElement($service->getMovies());
89+
$verifyResult = $builder->verify();
90+
91+
$this->assertTrue($verifyResult);
92+
$this->assertCount(1, $moviesResult);
93+
}
94+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"consumer": {
3+
"name": "xmlConsumer"
4+
},
5+
"interactions": [
6+
{
7+
"description": "A get request to /movies",
8+
"providerStates": [
9+
{
10+
"name": "Movies exist"
11+
}
12+
],
13+
"request": {
14+
"headers": {
15+
"Accept": "text/xml; charset=UTF8"
16+
},
17+
"method": "GET",
18+
"path": "/movies"
19+
},
20+
"response": {
21+
"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>",
22+
"headers": {
23+
"Content-Type": "text/xml"
24+
},
25+
"matchingRules": {
26+
"body": {
27+
"$.movies.movie": {
28+
"combine": "AND",
29+
"matchers": [
30+
{
31+
"match": "type"
32+
}
33+
]
34+
},
35+
"$.movies.movie.characters.character": {
36+
"combine": "AND",
37+
"matchers": [
38+
{
39+
"match": "type"
40+
}
41+
]
42+
},
43+
"$.movies.movie.characters.character.actor.#text": {
44+
"combine": "AND",
45+
"matchers": [
46+
{
47+
"match": "type"
48+
}
49+
]
50+
},
51+
"$.movies.movie.characters.character.name.#text": {
52+
"combine": "AND",
53+
"matchers": [
54+
{
55+
"match": "type"
56+
}
57+
]
58+
},
59+
"$.movies.movie.great-lines.line": {
60+
"combine": "AND",
61+
"matchers": [
62+
{
63+
"match": "type"
64+
}
65+
]
66+
},
67+
"$.movies.movie.great-lines.line.#text": {
68+
"combine": "AND",
69+
"matchers": [
70+
{
71+
"match": "type"
72+
}
73+
]
74+
},
75+
"$.movies.movie.plot.#text": {
76+
"combine": "AND",
77+
"matchers": [
78+
{
79+
"match": "type"
80+
}
81+
]
82+
},
83+
"$.movies.movie.rating.#text": {
84+
"combine": "AND",
85+
"matchers": [
86+
{
87+
"match": "type"
88+
},
89+
{
90+
"match": "type"
91+
}
92+
]
93+
},
94+
"$.movies.movie.title.#text": {
95+
"combine": "AND",
96+
"matchers": [
97+
{
98+
"match": "type"
99+
}
100+
]
101+
}
102+
},
103+
"header": {}
104+
},
105+
"status": 200
106+
}
107+
}
108+
],
109+
"metadata": {
110+
"pactRust": {
111+
"ffi": "0.4.7",
112+
"mockserver": "1.2.3",
113+
"models": "1.1.9"
114+
},
115+
"pactSpecification": {
116+
"version": "3.0.0"
117+
}
118+
},
119+
"provider": {
120+
"name": "xmlProvider"
121+
}
122+
}

example/xml/provider/phpunit.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="../../../vendor/autoload.php" colors="true">
3+
<testsuites>
4+
<testsuite name="PhpPact Example Tests">
5+
<directory>./tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<php>
9+
<env name="PACT_LOGLEVEL" value="DEBUG"/>
10+
</php>
11+
</phpunit>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
use AaronDDM\XMLBuilder\Writer\XMLWriterService;
4+
use AaronDDM\XMLBuilder\XMLBuilder;
5+
use Psr\Http\Message\ResponseInterface as Response;
6+
use Psr\Http\Message\ServerRequestInterface as Request;
7+
use Slim\Factory\AppFactory;
8+
9+
require __DIR__ . '/../../../../vendor/autoload.php';
10+
11+
$xmlWriterService = new XMLWriterService();
12+
$xmlBuilder = new XMLBuilder($xmlWriterService);
13+
$xmlBuilder
14+
->createXMLArray()
15+
->start('movies')
16+
->start('movie')
17+
->add('title', 'PHP: Behind the Parser')
18+
->start('characters')
19+
->start('character')
20+
->add('name', 'Ms. Coder')
21+
->add('actor', 'Onlivia Actora')
22+
->end()
23+
->start('character')
24+
->add('name', 'Mr. Coder')
25+
->add('actor', 'El Act&#211;r')
26+
->end()
27+
->end()
28+
->add('plot', <<<PLOT
29+
So, this language. It's like, a programming language. Or is it a
30+
scripting language? All is revealed in this thrilling horror spoof
31+
of a documentary.
32+
PLOT)
33+
->start('great-lines')
34+
->add('line', 'PHP solves all my web problems')
35+
->end()
36+
->add('rating', 7, ['type' => 'thumbs'])
37+
->add('rating', 5, ['type' => 'stars'])
38+
->end()
39+
->end();
40+
41+
$app = AppFactory::create();
42+
43+
$app->get('/movies', function (Request $request, Response $response) use ($xmlBuilder) {
44+
$response->getBody()->write($xmlBuilder->getXml());
45+
46+
return $response->withHeader('Content-Type', 'text/xml');
47+
});
48+
49+
$app->post('/pact-change-state', function (Request $request, Response $response) {
50+
return $response;
51+
});
52+
53+
$app->run();

0 commit comments

Comments
 (0)