You are here

public function JsonPathReplacerTest::testReplaceBatchSplit in Subrequests 8.2

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

@covers ::replaceBatch

File

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

Class

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

Namespace

Drupal\Tests\subrequests\Unit

Code

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