You are here

class AjaxRendererTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php \Drupal\Tests\Core\Controller\AjaxRendererTest

@coversDefaultClass \Drupal\Core\Render\MainContent\AjaxRenderer @group Ajax

Hierarchy

Expanded class hierarchy of AjaxRendererTest

File

core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php, line 19
Contains \Drupal\Tests\Core\Controller\AjaxRendererTest.

Namespace

Drupal\Tests\Core\Controller
View source
class AjaxRendererTest extends UnitTestCase {

  /**
   * The tested ajax controller.
   *
   * @var \Drupal\Core\Render\MainContent\AjaxRenderer
   */
  protected $ajaxRenderer;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $renderer;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $element_info_manager = $this
      ->getMock('Drupal\\Core\\Render\\ElementInfoManagerInterface');
    $element_info_manager
      ->expects($this
      ->any())
      ->method('getInfo')
      ->with('ajax')
      ->willReturn([
      '#header' => TRUE,
      '#commands' => array(),
      '#error' => NULL,
    ]);
    $this->ajaxRenderer = new TestAjaxRenderer($element_info_manager);
    $this->renderer = $this
      ->getMockBuilder('Drupal\\Core\\Render\\Renderer')
      ->disableOriginalConstructor()
      ->setMethods(NULL)
      ->getMock();
    $container = new ContainerBuilder();
    $container
      ->set('renderer', $this->renderer);
    \Drupal::setContainer($container);
  }

  /**
   * Tests the content method.
   *
   * @covers ::renderResponse
   */
  public function testRenderWithFragmentObject() {
    $main_content = [
      '#markup' => 'example content',
    ];
    $request = new Request();
    $route_match = $this
      ->getMock('Drupal\\Core\\Routing\\RouteMatchInterface');

    /** @var \Drupal\Core\Ajax\AjaxResponse $result */
    $result = $this->ajaxRenderer
      ->renderResponse($main_content, $request, $route_match);
    $this
      ->assertInstanceOf('Drupal\\Core\\Ajax\\AjaxResponse', $result);
    $commands = $result
      ->getCommands();
    $this
      ->assertEquals('insert', $commands[0]['command']);
    $this
      ->assertEquals('example content', $commands[0]['data']);
    $this
      ->assertEquals('insert', $commands[1]['command']);
    $this
      ->assertEquals('status_messages', $commands[1]['data']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxRendererTest::$ajaxRenderer protected property The tested ajax controller.
AjaxRendererTest::$renderer protected property The renderer.
AjaxRendererTest::setUp protected function Overrides UnitTestCase::setUp
AjaxRendererTest::testRenderWithFragmentObject public function Tests the content method.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.