function AjaxResponseTest::testMergeWithOtherAjaxResponse

Tests the mergeWith() method.

Throws

\PHPUnit\Framework\MockObject\Exception

See also

\Drupal\Core\Ajax\AjaxResponse::mergeWith()

File

core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php, line 109

Class

AjaxResponseTest
@coversDefaultClass \Drupal\Core\Ajax\AjaxResponse[[api-linebreak]] @group Ajax

Namespace

Drupal\Tests\Core\Ajax

Code

public function testMergeWithOtherAjaxResponse() : void {
  $response = new AjaxResponse([]);
  $command_one = $this->createCommandMock('one');
  $command_two = $this->createCommandMockWithSettingsAndLibrariesAttachments('Drupal\\Core\\Ajax\\HtmlCommand', [
    'setting1' => 'value1',
    'setting2' => 'value2',
  ], [
    'jquery',
    'drupal',
  ], 'two');
  $command_three = $this->createCommandMockWithSettingsAndLibrariesAttachments('Drupal\\Core\\Ajax\\InsertCommand', [
    'setting1' => 'overridden',
    'setting3' => 'value3',
  ], [
    'jquery',
    'ajax',
  ], 'three');
  $response->addCommand($command_one);
  $response->addCommand($command_two);
  $response2 = new AjaxResponse([]);
  $response2->addCommand($command_three);
  $response->mergeWith($response2);
  self::assertEquals([
    'library' => [
      'jquery',
      'drupal',
      'jquery',
      'ajax',
    ],
    'drupalSettings' => [
      'setting1' => 'overridden',
      'setting2' => 'value2',
      'setting3' => 'value3',
    ],
  ], $response->getAttachments());
  self::assertEquals([
    [
      'command' => 'one',
    ],
    [
      'command' => 'two',
    ],
    [
      'command' => 'three',
    ],
  ], $response->getCommands());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.