public function AjaxResponseTest::testCommands in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php \Drupal\Tests\Core\Ajax\AjaxResponseTest::testCommands()
Tests the add and getCommands method.
See also
\Drupal\Core\Ajax\AjaxResponse::addCommand()
\Drupal\Core\Ajax\AjaxResponse::getCommands()
File
- core/tests/ Drupal/ Tests/ Core/ Ajax/ AjaxResponseTest.php, line 40 
- Contains \Drupal\Tests\Core\Ajax\AjaxResponseTest.
Class
- AjaxResponseTest
- @coversDefaultClass \Drupal\Core\Ajax\AjaxResponse @group Ajax
Namespace
Drupal\Tests\Core\AjaxCode
public function testCommands() {
  $command_one = $this
    ->getMock('Drupal\\Core\\Ajax\\CommandInterface');
  $command_one
    ->expects($this
    ->once())
    ->method('render')
    ->will($this
    ->returnValue(array(
    'command' => 'one',
  )));
  $command_two = $this
    ->getMock('Drupal\\Core\\Ajax\\CommandInterface');
  $command_two
    ->expects($this
    ->once())
    ->method('render')
    ->will($this
    ->returnValue(array(
    'command' => 'two',
  )));
  $command_three = $this
    ->getMock('Drupal\\Core\\Ajax\\CommandInterface');
  $command_three
    ->expects($this
    ->once())
    ->method('render')
    ->will($this
    ->returnValue(array(
    'command' => 'three',
  )));
  $this->ajaxResponse
    ->addCommand($command_one);
  $this->ajaxResponse
    ->addCommand($command_two);
  $this->ajaxResponse
    ->addCommand($command_three, TRUE);
  // Ensure that the added commands are in the right order.
  $commands =& $this->ajaxResponse
    ->getCommands();
  $this
    ->assertSame($commands[1], array(
    'command' => 'one',
  ));
  $this
    ->assertSame($commands[2], array(
    'command' => 'two',
  ));
  $this
    ->assertSame($commands[0], array(
    'command' => 'three',
  ));
  // Remove one and change one element from commands and ensure the reference
  // worked as expected.
  unset($commands[2]);
  $commands[0]['class'] = 'test-class';
  $commands = $this->ajaxResponse
    ->getCommands();
  $this
    ->assertSame($commands[1], array(
    'command' => 'one',
  ));
  $this
    ->assertFalse(isset($commands[2]));
  $this
    ->assertSame($commands[0], array(
    'command' => 'three',
    'class' => 'test-class',
  ));
}