Refactor existing tests

References #74
This commit is contained in:
Oliver Davies 2020-05-31 14:55:50 +01:00
parent 95a3187186
commit 45fda0fed1
2 changed files with 28 additions and 34 deletions

View file

@ -32,16 +32,23 @@ abstract class TalksTestBase extends EntityKernelTestBase {
'custom_test',
];
protected function createEvent(string $eventDateToFormat): ParagraphInterface {
/** @var \Drupal\paragraphs\ParagraphInterface $event */
$event = Paragraph::create([
'field_date' => $eventDateToFormat,
protected function createEvent(array $overrides = []): ParagraphInterface {
$event = Paragraph::create(array_merge([
'type' => 'event',
]);
], $overrides));
return tap($event)->save();
}
protected function createTalk(array $overrides = []): EntityInterface {
$talk = Node::create(array_merge([
'title' => 'Test Driven Drupal',
'type' => 'talk',
], $overrides));
return tap($talk)->save();
}
protected function setUp() {
parent::setUp();
@ -51,21 +58,4 @@ abstract class TalksTestBase extends EntityKernelTestBase {
$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();
}
}

View file

@ -11,27 +11,31 @@ final class UpdatesTalkCreatedDateTest extends TalksTestBase {
public function testCreatingNode() {
$eventDate = Carbon::today()->addWeek();
$eventDateToFormat = $eventDate->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$eventDateToTimestamp = $eventDate->getTimestamp();
$eventDateFormat = $eventDate->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$eventDateTimestamp = $eventDate->getTimestamp();
$talk = $this->createTalk($eventDateToFormat);
$talk = $this->createTalk([
'field_events' => [
$this->createEvent(['field_date' => $eventDateFormat]),
],
]);
$this->assertEqual($eventDateToTimestamp, $talk->get('created')
$this->assertEqual($eventDateTimestamp, $talk->get('created')
->getString());
}
public function testUpdatingNode() {
$talk = $this->createTalk();
$eventDate = Carbon::today()->addWeek();
$eventDateToFormat = $eventDate->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$eventDateToTimestamp = $eventDate->getTimestamp();
$eventDateFormat = $eventDate->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$eventDateTimestamp = $eventDate->getTimestamp();
$event = $this->createEvent($eventDateToFormat);
$talk->set('field_events', [$event]);
$talk->save();
$talk = $this->createTalk([
'field_events' => [
$this->createEvent(['field_date' => $eventDateFormat]),
],
]);
$this->assertEqual($eventDateToTimestamp, $talk->get('created')
$this->assertEqual($eventDateTimestamp, $talk->get('created')
->getString());
}