class JsonPathReplacerTest in Subrequests 8.2
Same name and namespace in other branches
- 3.x tests/src/Unit/JsonPathReplacerTest.php \Drupal\Tests\subrequests\Unit\JsonPathReplacerTest
@coversDefaultClass \Drupal\subrequests\JsonPathReplacer @group subrequests
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\subrequests\Unit\JsonPathReplacerTest
Expanded class hierarchy of JsonPathReplacerTest
File
- tests/
src/ Unit/ JsonPathReplacerTest.php, line 14
Namespace
Drupal\Tests\subrequests\UnitView source
class JsonPathReplacerTest extends UnitTestCase {
/**
* @var \Drupal\subrequests\JsonPathReplacer
*/
protected $sut;
protected function setUp() {
parent::setUp();
$this->sut = new JsonPathReplacer();
}
/**
* @covers ::replaceBatch
*/
public function testReplaceBatch() {
$batch = $responses = [];
$batch[] = new Subrequest([
'uri' => '/ipsum/{{foo.body@$.things[*]}}/{{bar.body@$.things[*]}}/{{foo.body@$.stuff}}',
'action' => 'sing',
'requestId' => 'oop',
'headers' => [],
'_resolved' => FALSE,
'body' => [
'answer' => '{{foo.body@$.stuff}}',
],
'waitFor' => [
'foo',
],
]);
$batch[] = new Subrequest([
'uri' => '/dolor/{{foo.body@$.stuff}}',
'action' => 'create',
'requestId' => 'oof',
'headers' => [],
'_resolved' => FALSE,
'body' => 'bar',
'waitFor' => [
'foo',
],
]);
$response = Response::create('{"things":["what","keep","talking"],"stuff":42}');
$response->headers
->set('Content-ID', '<foo>');
$responses[] = $response;
$response = Response::create('{"things":["the","plane","is"],"stuff":"delayed"}');
$response->headers
->set('Content-ID', '<bar>');
$responses[] = $response;
$actual = $this->sut
->replaceBatch($batch, $responses);
$this
->assertCount(10, $actual);
$paths = array_map(function (Subrequest $subrequest) {
return [
$subrequest->uri,
$subrequest->body,
];
}, $actual);
$expected_paths = [
[
'/ipsum/what/the/42',
[
'answer' => '42',
],
],
[
'/ipsum/what/plane/42',
[
'answer' => '42',
],
],
[
'/ipsum/what/is/42',
[
'answer' => '42',
],
],
[
'/ipsum/keep/the/42',
[
'answer' => '42',
],
],
[
'/ipsum/keep/plane/42',
[
'answer' => '42',
],
],
[
'/ipsum/keep/is/42',
[
'answer' => '42',
],
],
[
'/ipsum/talking/the/42',
[
'answer' => '42',
],
],
[
'/ipsum/talking/plane/42',
[
'answer' => '42',
],
],
[
'/ipsum/talking/is/42',
[
'answer' => '42',
],
],
[
'/dolor/42',
'bar',
],
];
$this
->assertEquals($expected_paths, $paths);
$this
->assertEquals([
'answer' => 42,
], $actual[0]->body);
}
/**
* @covers ::replaceBatch
*/
public function testReplaceBatchSplit() {
$batch = $responses = [];
$batch[] = new Subrequest([
'uri' => 'test://{{foo.body@$.things[*].id}}/{{foo.body@$.things[*].id}}',
'action' => 'sing',
'requestId' => 'oop',
'headers' => [],
'_resolved' => FALSE,
'body' => [
'answer' => '{{foo.body@$.stuff}}',
],
'waitFor' => [
'foo',
],
]);
$response = Response::create('{"things":[{"id":"what"},{"id":"keep"},{"id":"talking"}],"stuff":42}');
$response->headers
->set('Content-ID', '<foo#0>');
$responses[] = $response;
$response = Response::create('{"things":[{"id":"the"},{"id":"plane"}],"stuff":"delayed"}');
$response->headers
->set('Content-ID', '<foo#1>');
$responses[] = $response;
$actual = $this->sut
->replaceBatch($batch, $responses);
$this
->assertCount(10, $actual);
$paths = array_map(function (Subrequest $subrequest) {
return [
$subrequest->uri,
$subrequest->body,
];
}, $actual);
$expected_paths = [
[
'test://what/what',
[
'answer' => '42',
],
],
[
'test://what/what',
[
'answer' => 'delayed',
],
],
[
'test://keep/keep',
[
'answer' => '42',
],
],
[
'test://keep/keep',
[
'answer' => 'delayed',
],
],
[
'test://talking/talking',
[
'answer' => '42',
],
],
[
'test://talking/talking',
[
'answer' => 'delayed',
],
],
[
'test://the/the',
[
'answer' => '42',
],
],
[
'test://the/the',
[
'answer' => 'delayed',
],
],
[
'test://plane/plane',
[
'answer' => '42',
],
],
[
'test://plane/plane',
[
'answer' => 'delayed',
],
],
];
$this
->assertEquals($expected_paths, $paths);
}
/**
* @covers ::replaceBatch
*/
public function testReplaceBatchTypes() {
$batch = $responses = [];
$batch[] = new Subrequest([
'uri' => '/test/types',
'action' => 'create',
'requestId' => 'xyz',
'headers' => [],
'_resolved' => FALSE,
'body' => [
'You are number' => '{{foo.body@$.Number}}',
'Where am I' => '{{foo.body@$.Location}}',
'World of number two' => '{{foo.body@$.Two}}',
'Question' => 'Who is number {{foo.body@$.Who}}?',
'Michael' => '{{foo.body@$.Feigenbaum}}',
],
'waitFor' => [
'foo',
],
]);
$response = Response::create('{"Number":6, "Location":"In the village", "Two":false, "Who":1, "Feigenbaum":4.6692}');
$response->headers
->set('Content-ID', '<foo>');
$responses[] = $response;
$actual = $this->sut
->replaceBatch($batch, $responses);
$this
->assertInternalType('int', $actual[0]->body['You are number']);
$this
->assertInternalType('string', $actual[0]->body['Where am I']);
$this
->assertInternalType('boolean', $actual[0]->body['World of number two']);
$this
->assertInternalType('string', $actual[0]->body['Question']);
$this
->assertInternalType('float', $actual[0]->body['Michael']);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
JsonPathReplacerTest:: |
protected | property | ||
JsonPathReplacerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
JsonPathReplacerTest:: |
public | function | @covers ::replaceBatch | |
JsonPathReplacerTest:: |
public | function | @covers ::replaceBatch | |
JsonPathReplacerTest:: |
public | function | @covers ::replaceBatch | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |