+ /**
+ * @covers ::build
+ * @dataProvider providerTestBuild
+ */
+ public function testBuild(PromiseInterface $promise, $in_preview, $expected_markup, $log_message = '', $log_arguments = []) {
+ $entity = $this->prophesize(FieldableEntityInterface::class);
+ $field = $this->prophesize(FieldItemListInterface::class);
+ $entity->get('the_field_name')->willReturn($field->reveal());
+ $entity->in_preview = $in_preview;
+ $field->view(Argument::type('array'))->will($promise);
+
+ $field_definition = $this->prophesize(FieldDefinitionInterface::class);
+ $field_definition->getLabel()->willReturn('The Field Label');
+ $this->entityFieldManager->getFieldDefinitions('entity_test', 'entity_test')->willReturn(['the_field_name' => $field_definition]);
+
+ if ($log_message) {
+ $this->logger->warning($log_message, $log_arguments)->shouldBeCalled();
+ }
+ else {
+ $this->logger->warning(Argument::cetera())->shouldNotBeCalled();
+ }
+
+ $block = $this->getTestBlock($entity);
+ $expected = [
+ '#cache' => [
+ 'contexts' => [],
+ 'tags' => [],
+ 'max-age' => 0,
+ ],
+ ];
+ if ($expected_markup) {
+ $expected['content']['#markup'] = $expected_markup;
+ }
+
+ $actual = $block->build();
+ $this->assertEquals($expected, $actual);
+ }
+
+ /**
+ * Provides test data for ::testBuild().
+ */
+ public function providerTestBuild() {
+ $data = [];
+ $data['array, no preview'] = [
+ new ReturnPromise([['content' => ['#markup' => 'The field value']]]),
+ FALSE,
+ 'The field value',
+ ];
+ $data['array, preview'] = [
+ new ReturnPromise([['content' => ['#markup' => 'The field value']]]),
+ TRUE,
+ 'The field value',
+ ];
+ $data['empty array, no preview'] = [
+ new ReturnPromise([[]]),
+ FALSE,
+ '',
+ ];
+ $data['empty array, preview'] = [
+ new ReturnPromise([[]]),
+ TRUE,
+ 'Placeholder for the "The Field Label" field',
+ ];
+ $data['exception, no preview'] = [
+ new ThrowPromise(new \Exception('The exception message')),
+ FALSE,
+ '',
+ 'The field "%field" failed to render with the error of "%error".',
+ ['%field' => 'the_field_name', '%error' => 'The exception message'],
+ ];
+ $data['exception, preview'] = [
+ new ThrowPromise(new \Exception('The exception message')),
+ TRUE,
+ 'Placeholder for the "The Field Label" field',
+ 'The field "%field" failed to render with the error of "%error".',
+ ['%field' => 'the_field_name', '%error' => 'The exception message'],
+ ];
+ return $data;
+ }
+