class FormAjaxResponseBuilderTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/Form/FormAjaxResponseBuilderTest.php \Drupal\Tests\Core\Form\FormAjaxResponseBuilderTest
@coversDefaultClass \Drupal\Core\Form\FormAjaxResponseBuilder @group Form
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Core\Form\FormAjaxResponseBuilderTest
Expanded class hierarchy of FormAjaxResponseBuilderTest
File
- core/
tests/ Drupal/ Tests/ Core/ Form/ FormAjaxResponseBuilderTest.php, line 22 - Contains \Drupal\Tests\Core\Form\FormAjaxResponseBuilderTest.
Namespace
Drupal\Tests\Core\FormView source
class FormAjaxResponseBuilderTest extends UnitTestCase {
/**
* @var \Drupal\Core\Render\MainContent\MainContentRendererInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $renderer;
/**
* @var \Drupal\Core\Routing\RouteMatchInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $routeMatch;
/**
* @var \Drupal\Core\Form\FormAjaxResponseBuilder
*/
protected $formAjaxResponseBuilder;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->renderer = $this
->getMock('Drupal\\Core\\Render\\MainContent\\MainContentRendererInterface');
$this->routeMatch = $this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface');
$this->formAjaxResponseBuilder = new FormAjaxResponseBuilder($this->renderer, $this->routeMatch);
}
/**
* @covers ::buildResponse
*
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testBuildResponseNoTriggeringElement() {
$this->renderer
->expects($this
->never())
->method('renderResponse');
$request = new Request();
$form = [];
$form_state = new FormState();
$commands = [];
$expected = [];
$this
->assertSame($expected, $this->formAjaxResponseBuilder
->buildResponse($request, $form, $form_state, $commands));
}
/**
* @covers ::buildResponse
*
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testBuildResponseNoCallable() {
$this->renderer
->expects($this
->never())
->method('renderResponse');
$request = new Request();
$form = [];
$form_state = new FormState();
$triggering_element = [];
$form_state
->setTriggeringElement($triggering_element);
$commands = [];
$expected = [];
$this
->assertSame($expected, $this->formAjaxResponseBuilder
->buildResponse($request, $form, $form_state, $commands));
}
/**
* @covers ::buildResponse
*/
public function testBuildResponseRenderArray() {
$triggering_element = [
'#ajax' => [
'callback' => function (array $form, FormStateInterface $form_state) {
return $form['test'];
},
],
];
$request = new Request();
$form = [
'test' => [
'#type' => 'textfield',
],
];
$form_state = new FormState();
$form_state
->setTriggeringElement($triggering_element);
$commands = [];
$this->renderer
->expects($this
->once())
->method('renderResponse')
->with($form['test'], $request, $this->routeMatch)
->willReturn(new AjaxResponse([]));
$result = $this->formAjaxResponseBuilder
->buildResponse($request, $form, $form_state, $commands);
$this
->assertInstanceOf('\\Drupal\\Core\\Ajax\\AjaxResponse', $result);
$this
->assertSame($commands, $result
->getCommands());
}
/**
* @covers ::buildResponse
*/
public function testBuildResponseResponse() {
$triggering_element = [
'#ajax' => [
'callback' => function (array $form, FormStateInterface $form_state) {
return new AjaxResponse([]);
},
],
];
$request = new Request();
$form = [];
$form_state = new FormState();
$form_state
->setTriggeringElement($triggering_element);
$commands = [];
$this->renderer
->expects($this
->never())
->method('renderResponse');
$result = $this->formAjaxResponseBuilder
->buildResponse($request, $form, $form_state, $commands);
$this
->assertInstanceOf('\\Drupal\\Core\\Ajax\\AjaxResponse', $result);
$this
->assertSame($commands, $result
->getCommands());
}
/**
* @covers ::buildResponse
*/
public function testBuildResponseWithCommands() {
$triggering_element = [
'#ajax' => [
'callback' => function (array $form, FormStateInterface $form_state) {
return new AjaxResponse([]);
},
],
];
$request = new Request();
$form = [
'test' => [
'#type' => 'textfield',
],
];
$form_state = new FormState();
$form_state
->setTriggeringElement($triggering_element);
$commands = [
new AlertCommand('alert!'),
];
$commands_expected = [];
$commands_expected[] = [
'command' => 'alert',
'text' => 'alert!',
];
$this->renderer
->expects($this
->never())
->method('renderResponse');
$result = $this->formAjaxResponseBuilder
->buildResponse($request, $form, $form_state, $commands);
$this
->assertInstanceOf('\\Drupal\\Core\\Ajax\\AjaxResponse', $result);
$this
->assertSame($commands_expected, $result
->getCommands());
}
/**
* @covers ::buildResponse
*/
public function testBuildResponseWithUpdateCommand() {
$triggering_element = [
'#ajax' => [
'callback' => function (array $form, FormStateInterface $form_state) {
return new AjaxResponse([]);
},
],
];
$request = new Request();
$form = [
'#build_id' => 'the_build_id',
'#build_id_old' => 'a_new_build_id',
'test' => [
'#type' => 'textfield',
],
];
$form_state = new FormState();
$form_state
->setTriggeringElement($triggering_element);
$commands = [
new AlertCommand('alert!'),
];
$commands_expected = [];
$commands_expected[] = [
'command' => 'update_build_id',
'old' => 'a_new_build_id',
'new' => 'the_build_id',
];
$commands_expected[] = [
'command' => 'alert',
'text' => 'alert!',
];
$this->renderer
->expects($this
->never())
->method('renderResponse');
$result = $this->formAjaxResponseBuilder
->buildResponse($request, $form, $form_state, $commands);
$this
->assertInstanceOf('\\Drupal\\Core\\Ajax\\AjaxResponse', $result);
$this
->assertSame($commands_expected, $result
->getCommands());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormAjaxResponseBuilderTest:: |
protected | property | ||
FormAjaxResponseBuilderTest:: |
protected | property | ||
FormAjaxResponseBuilderTest:: |
protected | property | ||
FormAjaxResponseBuilderTest:: |
protected | function |
Overrides UnitTestCase:: |
|
FormAjaxResponseBuilderTest:: |
public | function | @covers ::buildResponse | |
FormAjaxResponseBuilderTest:: |
public | function | @covers ::buildResponse | |
FormAjaxResponseBuilderTest:: |
public | function | @covers ::buildResponse | |
FormAjaxResponseBuilderTest:: |
public | function | @covers ::buildResponse | |
FormAjaxResponseBuilderTest:: |
public | function | @covers ::buildResponse | |
FormAjaxResponseBuilderTest:: |
public | function | @covers ::buildResponse | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in 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. |