Skip to content

Commit

Permalink
test: Updat xml example
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Jan 1, 2024
1 parent 6c72b4d commit 0de5470
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
2 changes: 1 addition & 1 deletion example/xml/consumer/src/Service/HttpClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(string $baseUri)
public function getMovies(): string
{
$response = $this->httpClient->get(new Uri("{$this->baseUri}/movies"), [
'headers' => ['Accept' => 'text/xml; charset=UTF8']
'headers' => ['Accept' => 'application/xml']
]);

return $response->getBody();
Expand Down
52 changes: 33 additions & 19 deletions example/xml/consumer/tests/Service/HttpClientServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace XmlConsumer\Tests\Service;

use PhpPact\Consumer\Matcher\Matcher;
use PhpPact\Xml\XmlBuilder;
use XmlConsumer\Service\HttpClientService;
use PhpPact\Consumer\InteractionBuilder;
Expand All @@ -14,11 +15,13 @@ class HttpClientServiceTest extends TestCase
{
public function testGetMovies()
{
$matcher = new Matcher();

$request = new ConsumerRequest();
$request
->setMethod('GET')
->setPath('/movies')
->addHeader('Accept', 'text/xml; charset=UTF8');
->addHeader('Accept', $matcher->regex('application/xml', 'application\/.*xml'));

$xmlBuilder = new XmlBuilder('1.0', 'UTF-8');
$xmlBuilder
Expand All @@ -29,7 +32,7 @@ public function testGetMovies()
$xmlBuilder->name('movie'),
$xmlBuilder->add(
$xmlBuilder->name('title'),
$xmlBuilder->contentLike('PHP: Behind the Parser'),
$xmlBuilder->contentLike('Big Buck Bunny'),
),
$xmlBuilder->add(
$xmlBuilder->name('characters'),
Expand All @@ -38,48 +41,47 @@ public function testGetMovies()
$xmlBuilder->name('character'),
$xmlBuilder->add(
$xmlBuilder->name('name'),
$xmlBuilder->contentLike('Ms. Coder'),
$xmlBuilder->contentLike('Big Buck Bunny'),
),
$xmlBuilder->add(
$xmlBuilder->name('actor'),
$xmlBuilder->contentLike('Onlivia Actora'),
$xmlBuilder->contentLike('Jan Morgenstern'),
),
),
),
$xmlBuilder->add(
$xmlBuilder->name('plot'),
$xmlBuilder->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.
$plot = <<<EOF
The plot follows a day in the life of Big Buck Bunny, during which time he meets three bullying rodents: the leader, Frank the flying squirrel, and his sidekicks Rinky the red squirrel and Gimera the chinchilla.
The rodents amuse themselves by harassing helpless creatures of the forest by throwing fruits, nuts, and rocks at them.
EOF
),
),
$xmlBuilder->add(
$xmlBuilder->name('great-lines'),
$xmlBuilder->eachLike(
$xmlBuilder->name('line'),
$xmlBuilder->contentLike('PHP solves all my web problems'),
$xmlBuilder->contentLike('Open source movie'),
),
),
$xmlBuilder->add(
$xmlBuilder->name('rating'),
$xmlBuilder->attribute('type', 'thumbs'),
$xmlBuilder->contentLike(7),
),
$xmlBuilder->add(
$xmlBuilder->name('rating'),
$xmlBuilder->attribute('type', 'stars'),
$xmlBuilder->contentLike(5),
$xmlBuilder->attribute('type', $matcher->regex('stars', 'stars|thumbs')),
$xmlBuilder->contentLike(6),
),
// TODO: implement XML generators
// $xmlBuilder->add(
// $xmlBuilder->name('release-date'),
// $xmlBuilder->content($matcher->date('dd-MM-yyyy')),
// ),
),
);

$response = new ProviderResponse();
$response
->setStatus(200)
->addHeader('Content-Type', 'text/xml')
->addHeader('Content-Type', $matcher->regex('application/xml', 'application\/.*xml'))
->setBody(
json_encode($xmlBuilder)
);
Expand All @@ -100,10 +102,22 @@ public function testGetMovies()
->willRespondWith($response);

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

$this->assertTrue($verifyResult);
$this->assertCount(1, $moviesResult);
$this->assertCount(1, $movies->movie);
$this->assertEquals('Big Buck Bunny', $movies->movie[0]->title);
$this->assertEquals($plot, $movies->movie[0]->plot);
$this->assertCount(1, $movies->movie[0]->{'great-lines'}->line);
$this->assertEquals('Open source movie', $movies->movie[0]->{'great-lines'}->line[0]);
//$this->assertEquals('6', $movies->movie[0]->rating);
$this->assertCount(2, $movies->movie[0]->characters->character);
$this->assertEquals('Big Buck Bunny', $movies->movie[0]->characters->character[0]->name);
$this->assertEquals('Jan Morgenstern', $movies->movie[0]->characters->character[0]->actor);
// TODO: implement XML generators
$this->assertEquals('', $movies->movie[0]->characters->character[1]->name);
$this->assertEquals('', $movies->movie[0]->characters->character[1]->actor);
//$this->assertEquals('', $movies->movie[0]->{'release-date'}[0]);
}
}
41 changes: 35 additions & 6 deletions example/xml/pacts/xmlConsumer-xmlProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,28 @@
],
"request": {
"headers": {
"Accept": "text/xml; charset=UTF8"
"Accept": "application/xml"
},
"matchingRules": {
"header": {
"$.Accept[0]": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "application\\/.*xml"
}
]
}
}
},
"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>",
"body": "<?xml version='1.0'?><movies><movie><title>Big Buck Bunny</title><characters><character><name>Big Buck Bunny</name><actor>Jan Morgenstern</actor></character><character><name/><actor/></character></characters><plot>The plot follows a day in the life of Big Buck Bunny, during which time he meets three bullying rodents: the leader, Frank the flying squirrel, and his sidekicks Rinky the red squirrel and Gimera the chinchilla.\nThe rodents amuse themselves by harassing helpless creatures of the forest by throwing fruits, nuts, and rocks at them.</plot><great-lines><line>Open source movie</line></great-lines><rating type='stars'>6</rating></movie></movies>",
"headers": {
"Content-Type": "text/xml"
"Content-Type": "application/xml"
},
"matchingRules": {
"body": {
Expand Down Expand Up @@ -85,9 +98,15 @@
"matchers": [
{
"match": "type"
},
}
]
},
"$.movies.movie.rating['@type']": {
"combine": "AND",
"matchers": [
{
"match": "type"
"match": "regex",
"regex": "stars|thumbs"
}
]
},
Expand All @@ -100,7 +119,17 @@
]
}
},
"header": {},
"header": {
"$['Content-Type'][0]": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "application\\/.*xml"
}
]
}
},
"status": {}
},
"status": 200
Expand Down
2 changes: 1 addition & 1 deletion example/xml/provider/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
XML
);

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

$app->post('/pact-change-state', function (Request $request, Response $response) {
Expand Down

0 comments on commit 0de5470

Please sign in to comment.