class FormAjaxResponseBuilderTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Form/FormAjaxResponseBuilderTest.php \Drupal\Tests\Core\Form\FormAjaxResponseBuilderTest
- 8.9.x core/tests/Drupal/Tests/Core/Form/FormAjaxResponseBuilderTest.php \Drupal\Tests\Core\Form\FormAjaxResponseBuilderTest
- 11.x 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 \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Form\FormAjaxResponseBuilderTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of FormAjaxResponseBuilderTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Form/ FormAjaxResponseBuilderTest.php, line 20
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() : void {
parent::setUp();
$this->renderer = $this->createMock('Drupal\\Core\\Render\\MainContent\\MainContentRendererInterface');
$this->routeMatch = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
$this->formAjaxResponseBuilder = new FormAjaxResponseBuilder($this->renderer, $this->routeMatch);
}
/**
* @covers ::buildResponse
*/
public function testBuildResponseNoTriggeringElement() : void {
$this->renderer
->expects($this->never())
->method('renderResponse');
$request = new Request();
$form = [];
$form_state = new FormState();
$commands = [];
$this->expectException(HttpException::class);
$this->formAjaxResponseBuilder
->buildResponse($request, $form, $form_state, $commands);
}
/**
* @covers ::buildResponse
*/
public function testBuildResponseNoCallable() : void {
$this->renderer
->expects($this->never())
->method('renderResponse');
$request = new Request();
$form = [];
$form_state = new FormState();
$triggering_element = [];
$form_state->setTriggeringElement($triggering_element);
$commands = [];
$this->expectException(HttpException::class);
$this->formAjaxResponseBuilder
->buildResponse($request, $form, $form_state, $commands);
}
/**
* @covers ::buildResponse
*/
public function testBuildResponseRenderArray() : void {
$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() : void {
$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() : void {
$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() : void {
$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
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
FormAjaxResponseBuilderTest::$formAjaxResponseBuilder | protected | property | ||||
FormAjaxResponseBuilderTest::$renderer | protected | property | ||||
FormAjaxResponseBuilderTest::$routeMatch | protected | property | ||||
FormAjaxResponseBuilderTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
FormAjaxResponseBuilderTest::testBuildResponseNoCallable | public | function | @covers ::buildResponse | |||
FormAjaxResponseBuilderTest::testBuildResponseNoTriggeringElement | public | function | @covers ::buildResponse | |||
FormAjaxResponseBuilderTest::testBuildResponseRenderArray | public | function | @covers ::buildResponse | |||
FormAjaxResponseBuilderTest::testBuildResponseResponse | public | function | @covers ::buildResponse | |||
FormAjaxResponseBuilderTest::testBuildResponseWithCommands | public | function | @covers ::buildResponse | |||
FormAjaxResponseBuilderTest::testBuildResponseWithUpdateCommand | public | function | @covers ::buildResponse | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed 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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.