Adds a test for null-template scenes.

This commit is contained in:
Vassyli
2020-12-28 13:40:19 +01:00
parent c7dbf33f6e
commit 29e5ced0f9
5 changed files with 60 additions and 37 deletions
+11 -7
View File
@@ -139,12 +139,12 @@ class Viewpoint implements CreateableInterface
public function getSnapshot(): ViewpointSnapshot
{
$snapshot = new ViewpointSnapshot(
$this->getTitle(),
$this->getDescription(),
\get_class($this->getTemplate()),
$this->getActionGroups(),
$this->getAttachments(),
$this->getData()
title: $this->getTitle(),
description: $this->getDescription(),
template: $this->getTemplate() === null ? null : \get_class($this->getTemplate()),
actionGroups: $this->getActionGroups(),
attachments: $this->getAttachments(),
data: $this->getData()
);
return $snapshot;
@@ -156,7 +156,11 @@ class Viewpoint implements CreateableInterface
*/
public function changeFromSnapshot(EntityManager $entityManager, ViewpointSnapshot $snapshot)
{
$templateInstance = $entityManager->getRepository(SceneTemplate::class)->find($snapshot->getTemplate());
if ($snapshot->getTemplate() !== null) {
$templateInstance = $entityManager->getRepository(SceneTemplate::class)->find($snapshot->getTemplate());
} else {
$templateInstance = null;
}
$this->setTitle($snapshot->getTitle());
$this->setDescription($snapshot->getDescription());
+9 -22
View File
@@ -8,36 +8,23 @@ namespace LotGD\Core\Models;
*/
class ViewpointSnapshot
{
private $title;
private $description;
private $template;
private $actionGroups;
private $attachments;
private $data;
/**
* ViewpointRestorationPoint constructor.
* @param string $title
* @param string $description
* @param string $template
* @param string|null $template
* @param array $actionGroups
* @param array $attachments
* @param array $data
*/
public function __construct(
string $title,
string $description,
string $template,
array $actionGroups,
array $attachments,
array $data
private string $title,
private string $description,
private ?string $template,
private array $actionGroups,
private array $attachments,
private array $data,
) {
$this->title = $title;
$this->description = $description;
$this->template = $template;
$this->actionGroups = $actionGroups;
$this->attachments = $attachments;
$this->data = $data;
}
/**
@@ -60,9 +47,9 @@ class ViewpointSnapshot
/**
* Template of the viewpoint.
* @return string
* @return string|null
*/
public function getTemplate(): string
public function getTemplate(): ?string
{
return $this->template;
}
+3 -3
View File
@@ -14,15 +14,15 @@ use LotGD\Core\Models\SceneTemplate;
trait SceneBasics
{
/** @Column(type="string", length=255) */
private $title = "{No scene set}";
private string $title = "{No scene set}";
/** @Column(type="text") */
private $description = "{No scene set}";
private string $description = "{No scene set}";
/** @Column(type="string", length=255) */
/**
* @ManyToOne(targetEntity="SceneTemplate", fetch="EAGER")
* @JoinColumn(name="template", referencedColumnName="class", nullable=true)
*/
private $template;
private ?SceneTemplate $template;
/**
* Sets scene title.
+28 -4
View File
@@ -31,9 +31,12 @@ class ViewpointRestorationTest extends CoreModelTestCase
return $actionGroup;
}
protected function getViewpoint()
protected function getViewpoint($useNullTemplate = false)
{
$sceneTemplateMock = $this->createMock(SceneTemplate::class);
$sceneTemplateMock = null;
if ($useNullTemplate === false) {
$sceneTemplateMock = $this->createMock(SceneTemplate::class);
}
$sceneMock = $this->createMock(Scene::class);
$sceneMock->method("getTitle")->willReturn("Scene Mock Title");
@@ -55,9 +58,12 @@ class ViewpointRestorationTest extends CoreModelTestCase
return [$entityManager, $viewpoint];
}
protected function getAlternativeViewpoint()
protected function getAlternativeViewpoint($useNullTemplate = false)
{
$sceneTemplateMock = $this->createMock(SceneTemplate::class);
$sceneTemplateMock = null;
if ($useNullTemplate === false) {
$sceneTemplateMock = $this->createMock(SceneTemplate::class);
}
$sceneMock = $this->createMock(Scene::class);
$sceneMock->method("getTitle")->willReturn("Another Scene Mock Title");
@@ -93,4 +99,22 @@ class ViewpointRestorationTest extends CoreModelTestCase
$this->assertSame($viewpoint->getData(), $newViewpoint->getData());
$this->assertSame($viewpoint->getAttachments(), $newViewpoint->getAttachments());
}
public function testIfViewpointAfterUnserializationIsEqualToBeforeItsSerializationWhenTemplateIsNull()
{
[$entityManager, $viewpoint] = $this->getViewpoint(useNullTemplate: true);
$viewpointRestoration = $viewpoint->getSnapshot();
$serialized = serialize($viewpointRestoration);
$viewpointRestored = unserialize($serialized);
[$entityManager2, $newViewpoint] = $this->getAlternativeViewpoint(useNullTemplate: true);
$newViewpoint->changeFromSnapshot($entityManager, $viewpointRestored);
$this->assertSame($viewpoint->getTitle(), $newViewpoint->getTitle());
$this->assertSame($viewpoint->getDescription(), $newViewpoint->getDescription());
$this->assertSame($viewpoint->getTemplate(), $newViewpoint->getTemplate());
$this->assertEquals($viewpoint->getActionGroups(), $newViewpoint->getActionGroups());;
$this->assertSame($viewpoint->getData(), $newViewpoint->getData());
$this->assertSame($viewpoint->getAttachments(), $newViewpoint->getAttachments());
}
}
+9 -1
View File
@@ -32,10 +32,18 @@ scenes:
title: "The Weaponry"
description: "This is the place where you can buy awesome weapons"
template: "lotgd/tests/weaponry"
-
id: "30000000-0000-0000-0000-000000000004"
title: "Template-less scene"
description: "This scene does not have a template"
scene_connections:
-
outgoingScene: "30000000-0000-0000-0000-000000000001"
incomingScene: "30000000-0000-0000-0000-000000000002"
-
outgoingScene: "30000000-0000-0000-0000-000000000001"
incomingScene: "30000000-0000-0000-0000-000000000003"
incomingScene: "30000000-0000-0000-0000-000000000003"
-
outgoingScene: "30000000-0000-0000-0000-000000000001"
incomingScene: "30000000-0000-0000-0000-000000000004"