You are here

public function JsonPathReplacerTest::testReplaceBatch in Subrequests 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Unit/JsonPathReplacerTest.php \Drupal\Tests\subrequests\Unit\JsonPathReplacerTest::testReplaceBatch()

@covers ::replaceBatch

File

tests/src/Unit/JsonPathReplacerTest.php, line 29

Class

JsonPathReplacerTest
@coversDefaultClass \Drupal\subrequests\JsonPathReplacer @group subrequests

Namespace

Drupal\Tests\subrequests\Unit

Code

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);
}