class AjaxRendererTest in Drupal 8
Same name and namespace in other branches
- 9 core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php \Drupal\Tests\Core\Controller\AjaxRendererTest
@coversDefaultClass \Drupal\Core\Render\MainContent\AjaxRenderer @group Ajax
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\Core\Controller\AjaxRendererTest
Expanded class hierarchy of AjaxRendererTest
File
- core/
tests/ Drupal/ Tests/ Core/ Controller/ AjaxRendererTest.php, line 16
Namespace
Drupal\Tests\Core\ControllerView 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
->createMock('Drupal\\Core\\Render\\ElementInfoManagerInterface');
$element_info_manager
->expects($this
->any())
->method('getInfo')
->with('ajax')
->willReturn([
'#header' => TRUE,
'#commands' => [],
'#error' => NULL,
]);
$renderer = $this
->createMock(RendererInterface::class);
$renderer
->expects($this
->any())
->method('renderRoot')
->willReturnCallback(function (&$elements, $is_root_call = FALSE) {
$elements += [
'#attached' => [],
];
if (isset($elements['#markup'])) {
return $elements['#markup'];
}
elseif (isset($elements['#type'])) {
return $elements['#type'];
}
else {
return 'Markup';
}
});
$this->ajaxRenderer = new AjaxRenderer($element_info_manager, $renderer);
}
/**
* Tests the content method.
*
* @covers ::renderResponse
*/
public function testRenderWithFragmentObject() {
$main_content = [
'#markup' => 'example content',
];
$request = new Request();
$route_match = $this
->createMock('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']);
}
/**
* @group legacy
* @expectedDeprecation The renderer service must be passed to Drupal\Core\Render\MainContent\AjaxRenderer::__construct and will be required before Drupal 9.0.0. See https://www.drupal.org/node/3009400
*/
public function testConstructorRendererArgument() {
$element_info_manager = $this
->createMock(ElementInfoManagerInterface::class);
$container = $this
->createMock(ContainerInterface::class);
$container
->expects($this
->once())
->method('get')
->with('renderer')
->willReturn(NULL);
\Drupal::setContainer($container);
new AjaxRenderer($element_info_manager);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxRendererTest:: |
protected | property | The tested ajax controller. | |
AjaxRendererTest:: |
protected | property | The renderer. | |
AjaxRendererTest:: |
protected | function |
Overrides UnitTestCase:: |
|
AjaxRendererTest:: |
public | function | @group legacy @expectedDeprecation The renderer service must be passed to Drupal\Core\Render\MainContent\AjaxRenderer::__construct and will be required before Drupal 9.0.0. See https://www.drupal.org/node/3009400 | |
AjaxRendererTest:: |
public | function | Tests the content method. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |