From bcfe01f62c697cd84334ebf250888c97c4bf048d Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 19 May 2020 01:42:44 +0100 Subject: [PATCH] Extract a base test case for talks tests --- .../custom/tests/src/Kernel/TalksTestBase.php | 70 +++++++++++++++++++ .../src/Kernel/UpdatesTalkCreatedDateTest.php | 65 +---------------- 2 files changed, 71 insertions(+), 64 deletions(-) create mode 100644 web/modules/custom/custom/tests/src/Kernel/TalksTestBase.php diff --git a/web/modules/custom/custom/tests/src/Kernel/TalksTestBase.php b/web/modules/custom/custom/tests/src/Kernel/TalksTestBase.php new file mode 100644 index 0000000..cb15c11 --- /dev/null +++ b/web/modules/custom/custom/tests/src/Kernel/TalksTestBase.php @@ -0,0 +1,70 @@ + $eventDateToFormat, + 'type' => 'event', + ]); + + return tap($event)->save(); + } + + protected function setUp() { + parent::setUp(); + + $this->installEntitySchema('paragraph'); + $this->installSchema('node', ['node_access']); + + $this->installConfig(['custom_test']); + } + + protected function createTalk(?string $eventDateToFormat = NULL): EntityInterface { + if ($eventDateToFormat) { + $event = $this->createEvent($eventDateToFormat); + } + + $talk = Node::create([ + 'title' => 'TDD - Test Driven Drupal', + 'type' => 'talk', + ]); + + if (isset($event)) { + $talk->set('field_events', [$event]); + } + + return tap($talk)->save(); + } + +} diff --git a/web/modules/custom/custom/tests/src/Kernel/UpdatesTalkCreatedDateTest.php b/web/modules/custom/custom/tests/src/Kernel/UpdatesTalkCreatedDateTest.php index 9f4e42d..f621567 100644 --- a/web/modules/custom/custom/tests/src/Kernel/UpdatesTalkCreatedDateTest.php +++ b/web/modules/custom/custom/tests/src/Kernel/UpdatesTalkCreatedDateTest.php @@ -5,33 +5,9 @@ declare(strict_types=1); namespace Drupal\Tests\custom\Kernel; use Carbon\Carbon; -use Drupal\Core\Entity\EntityInterface; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; -use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; -use Drupal\node\Entity\Node; -use Drupal\paragraphs\Entity\Paragraph; -use Drupal\paragraphs\ParagraphInterface; -final class UpdatesTalkCreatedDateTest extends EntityKernelTestBase { - - /** - * {@inheritDoc} - */ - public static $modules = [ - // Core. - 'node', - 'file', - 'datetime', - - // Contrib. - 'entity_reference_revisions', - 'paragraphs', - 'hook_event_dispatcher', - - // Custom. - 'custom', - 'custom_test', - ]; +final class UpdatesTalkCreatedDateTest extends TalksTestBase { public function testCreatingNode() { $eventDate = Carbon::today()->addWeek(); @@ -59,43 +35,4 @@ final class UpdatesTalkCreatedDateTest extends EntityKernelTestBase { ->getString()); } - protected function setUp() { - parent::setUp(); - - $this->installEntitySchema('paragraph'); - $this->installSchema('node', ['node_access']); - - $this->installConfig(['custom_test']); - } - - private function createTalk(?string $eventDateToFormat = NULL): EntityInterface { - if ($eventDateToFormat) { - $event = $this->createEvent($eventDateToFormat); - } - - $talk = Node::create([ - 'title' => 'TDD - Test Driven Drupal', - 'type' => 'talk', - ]); - - if (isset($event)) { - $talk->set('field_events', [$event]); - } - - $talk->save(); - - return $talk; - } - - private function createEvent(string $eventDateToFormat): ParagraphInterface { - $event = Paragraph::create([ - 'field_date' => $eventDateToFormat, - 'type' => 'event', - ]); - - $event->save(); - - return $event; - } - }