Skip to content
This repository has been archived by the owner on Dec 17, 2022. It is now read-only.

Commit

Permalink
Adds in new assertNodeIsValue assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunty committed Dec 19, 2016
1 parent be59031 commit aae7502
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ The `\Brunty\Response` class contains a list of constants for all HTTP status co
* `assertResponseWasJson()`
* `assertResponseWasXml()`
* `assertResponseHasKey($key)`
* `assertNodeIsValue($xPathQuery, $value)` Runs the xpath query against the result (yes, even for JSON - though that's a bit experimental) and asserts that the value is correct - currently only works with strings.
* `assertRedirectedTo($path)` Path can be absolute, or relative to the root `api_base_url`

## Contributing
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"require": {
"php": "~5.6|~7.0",
"phpunit/phpunit": "~5.7",
"guzzlehttp/guzzle": "~6.2"
"guzzlehttp/guzzle": "~6.2",
"spatie/array-to-xml": "^2.2"
},
"require-dev": {
"satooshi/php-coveralls": "^1.0"
Expand Down
51 changes: 51 additions & 0 deletions src/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use PHPUnit\Framework\TestCase;
use Spatie\ArrayToXml\ArrayToXml;

class ApiTestCase extends TestCase
{
Expand Down Expand Up @@ -34,6 +35,11 @@ class ApiTestCase extends TestCase
*/
private $clientOptions = [];

/**
* @var \SimpleXMLElement
*/
private $bodyAsXml;

public function configureClientOptions(array $options = [])
{
$this->clientOptions = $options;
Expand Down Expand Up @@ -213,6 +219,15 @@ public function assertResponseHasKey($key)
self::assertTrue(isset($content[$key]), sprintf('Response body does not have the key %s', $key));
}

/**
* @param string $query
* @param string|null $value
*/
public function assertNodeIsValue($query, $value)
{
self::assertEquals($value, $this->query($query));
}

/**
* @return string
*/
Expand Down Expand Up @@ -316,4 +331,40 @@ private function baseUrl()
{
return $_ENV['api_base_url'];
}

/**
* @return \SimpleXMLElement
*/
private function getBodyAsXml()
{
if ($this->bodyAsXml === null) {
if ($this->contentTypeIsXml()) {
$this->bodyAsXml = $this->responseBody();
}

if ($this->contentTypeIsJson()) {
$this->bodyAsXml = new \SimpleXMLElement(ArrayToXml::convert($this->responseBody(true)));
}
}

return $this->bodyAsXml;
}

/**
* @param $query
*
* @return null|string
*/
private function query($query)
{
$xml = $this->getBodyAsXml();

$result = $xml->xpath($query);

if (count($result)) {
return (string)$result[0];
}

return null;
}
}
18 changes: 18 additions & 0 deletions tests/ApiTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ public function it_gets_the_headers_from_a_response()
self::assertEquals($headers, $this->getHeader('X-Test-Header'));
}

/**
* @test
*/
public function it_asserts_the_value_of_a_node_in_a_json_response()
{
$this->get('/response-headers?X-Foo=Bar');
self::assertNodeIsValue('//X-Foo[1]', 'Bar');
}

/**
* @test
*/
public function it_asserts_the_value_of_a_node_in_an_xml_response()
{
$this->get('/xml');
self::assertNodeIsValue('//slide[1]/title', 'Wake up to WonderWidgets!');
}

/**
* @test
*/
Expand Down

0 comments on commit aae7502

Please sign in to comment.