Adds fixes and tests for cascade=persist, remove for scene entities.
It still looks like doctrine doesn't "know" about the column names in a cascade=remove relationship and assumes the property name to be also the column name - which is usually not true (by default, it's propertyname_id). This update changes the column name so that doctrine's assumptions are correct again and adds tests so any changes which invalidates this relationship can be gecocnized easily.
This commit is contained in:
@@ -35,12 +35,12 @@ class Scene implements CreateableInterface, SceneConnectable
|
||||
private $connectionGroups = null;
|
||||
|
||||
/**
|
||||
* @OneToMany(targetEntity="SceneConnection", mappedBy="outgoingScene", cascade={"persist"})
|
||||
* @OneToMany(targetEntity="SceneConnection", mappedBy="outgoingScene", cascade={"persist", "remove"})
|
||||
*/
|
||||
private $outgoingConnections = null;
|
||||
|
||||
/**
|
||||
* @OneToMany(targetEntity="SceneConnection", mappedBy="incomingScene", cascade={"persist"})
|
||||
* @OneToMany(targetEntity="SceneConnection", mappedBy="incomingScene", cascade={"persist", "remove"})
|
||||
*/
|
||||
private $incomingConnections = null;
|
||||
|
||||
@@ -50,7 +50,6 @@ class Scene implements CreateableInterface, SceneConnectable
|
||||
private static $fillable = [
|
||||
"title",
|
||||
"description",
|
||||
"parents",
|
||||
"template"
|
||||
];
|
||||
|
||||
|
||||
@@ -15,15 +15,15 @@ class SceneConnection
|
||||
{
|
||||
/**
|
||||
* @Id
|
||||
* @ManyToOne(targetEntity="Scene", inversedBy="outgoingConnections", cascade={"persist"})
|
||||
* @JoinColumn(name="outgoing_scene_id", referencedColumnName="id")
|
||||
* @ManyToOne(targetEntity="Scene")
|
||||
* @JoinColumn(name="outgoingScene", referencedColumnName="id")
|
||||
*/
|
||||
private $outgoingScene;
|
||||
|
||||
/**
|
||||
* @Id
|
||||
* @ManyToOne(targetEntity="Scene", inversedBy="incomingConnections", cascade={"persist"})
|
||||
* @JoinColumn(name="incoming_scene_id", referencedColumnName="id")
|
||||
* @ManyToOne(targetEntity="Scene")
|
||||
* @JoinColumn(name="incomingScene", referencedColumnName="id")
|
||||
*/
|
||||
private $incomingScene;
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class SceneConnectionGroup implements SceneConnectable
|
||||
/**
|
||||
* @Id
|
||||
* @ManyToOne(targetEntity="Scene", inversedBy="outgoingConnections", cascade={"persist"})
|
||||
* @JoinColumn(name="scene_id", referencedColumnName="id")
|
||||
* @JoinColumn(name="scene", referencedColumnName="id")
|
||||
*/
|
||||
private $scene;
|
||||
|
||||
|
||||
@@ -82,4 +82,10 @@ abstract class ModelTestCase extends \PHPUnit_Extensions_Database_TestCase
|
||||
// Clear out the cache so tests don't get confused.
|
||||
$this->getEntityManager()->clear();
|
||||
}
|
||||
|
||||
protected function flushAndClear()
|
||||
{
|
||||
$this->getEntityManager()->flush();
|
||||
$this->getEntityManager()->clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,7 @@ namespace LotGD\Core\Tests\Models;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
|
||||
use LotGD\Core\Exceptions\ArgumentException;
|
||||
use LotGD\Core\Models\Scene;
|
||||
use LotGD\Core\Models\SceneConnectionGroup;
|
||||
use LotGD\Core\Models\SceneConnection;
|
||||
use LotGD\Core\Models\{Scene, SceneConnection, SceneConnectionGroup};
|
||||
use LotGD\Core\Tests\CoreModelTestCase;
|
||||
|
||||
/**
|
||||
@@ -19,9 +17,125 @@ class SceneModelTest extends CoreModelTestCase
|
||||
/** @var string default data set */
|
||||
protected $dataset = "scene";
|
||||
|
||||
public function testCreate()
|
||||
protected function getNumberOfScenes(): int
|
||||
{
|
||||
$scene = new Scene();
|
||||
$results = $this->getEntityManager()->getRepository(Scene::class)->findAll();
|
||||
return count($results);
|
||||
}
|
||||
|
||||
protected function getNumberOfSceneConnections(): int
|
||||
{
|
||||
$results = $this->getEntityManager()->getRepository(SceneConnection::class)->findAll();
|
||||
return count($results);
|
||||
}
|
||||
|
||||
protected function getNumberOfSceneGroups(): int
|
||||
{
|
||||
$results = $this->getEntityManager()->getRepository(SceneConnectionGroup::class)->findAll();
|
||||
return count($results);
|
||||
}
|
||||
|
||||
protected function getTestSceneData(): array
|
||||
{
|
||||
return [
|
||||
"title" => "A new scene",
|
||||
"description" => "This is a new scene",
|
||||
"template" => "lotgd/test/new-scene"
|
||||
];
|
||||
}
|
||||
|
||||
public function testIfSceneCanGetCreatedAndDeleted()
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
|
||||
// Count number of scenes
|
||||
$n1 = $this->getNumberOfScenes();
|
||||
$this->assertGreaterThan(0, $n1);
|
||||
|
||||
// create new scene, flush and clear. Number of scenes in db should be +1
|
||||
$newScene = Scene::create($this->getTestSceneData());
|
||||
$newScene->save($em);
|
||||
$this->flushAndClear();
|
||||
unset($newScene);
|
||||
|
||||
// recount and assert that n1 + 1 === n2
|
||||
$n2 = $this->getNumberOfScenes();
|
||||
$this->assertSame($n1 + 1, $n2);
|
||||
|
||||
// fetch new scene, delete, flush and clear.
|
||||
$newScene = $em->getRepository(Scene::class)->findOneBy($this->getTestSceneData());
|
||||
$newScene->delete($em);
|
||||
$this->flushAndClear();
|
||||
|
||||
// recount and assert that n3 == n1
|
||||
$n3 = $this->getNumberOfScenes();
|
||||
$this->assertSame($n1, $n3);
|
||||
}
|
||||
|
||||
public function testIfSceneWithConnectionsCanGetCreatedAndDeleted()
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
|
||||
// Count number of scenes
|
||||
$n1 = $this->getNumberOfScenes();
|
||||
$this->assertGreaterThan(0, $n1);
|
||||
|
||||
// Count number of connections
|
||||
$c1 = $this->getNumberOfSceneConnections();
|
||||
$this->assertGreaterTHan(0, $c1);
|
||||
|
||||
// create new scene, connect to another one. Number of scenes must be +1, number of connections must be +1
|
||||
// this tests for cascade=persist
|
||||
$scene = Scene::create($this->getTestSceneData());
|
||||
$scene->connect($em->getRepository(Scene::class)->find(1));
|
||||
$scene->save($em);
|
||||
$this->flushAndClear();
|
||||
unset($scene);
|
||||
|
||||
// recount and assert that this is the case
|
||||
$this->assertSame($n1 + 1, $this->getNumberOfScenes());
|
||||
$this->assertSame($c1 + 1, $this->getNumberOfSceneConnections());
|
||||
|
||||
// delete scene again. Number of scenes and number of connections must be what it was at the beginning
|
||||
// this tests for cascade=remove
|
||||
$scene = $em->getRepository(Scene::class)->findOneBy($this->getTestSceneData());
|
||||
$scene->delete($em);
|
||||
$this->flushAndClear();
|
||||
unset($scene);
|
||||
|
||||
// recount and assert that this is the case
|
||||
$this->assertSame($n1, $this->getNumberOfScenes());
|
||||
$this->assertSame($c1, $this->getNumberOfSceneConnections());
|
||||
}
|
||||
|
||||
public function testIfSceneWithConnectionGroupsCanGetCreatedAndDeleted()
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
|
||||
// count number of scenes
|
||||
$n1 = $this->getNumberOfScenes();
|
||||
$g1 = $this->getNumberOfSceneGroups();
|
||||
|
||||
// create new scene, add scene group. Number of scenes must be +1, number of scene connection groups must be +1
|
||||
// this tests for cascade=persist
|
||||
$scene = Scene::create($this->getTestSceneData());
|
||||
$scene->addConnectionGroup(new SceneConnectionGroup("test", "test"));
|
||||
$scene->save($em);
|
||||
$this->flushAndClear();
|
||||
|
||||
// recount and assert that this is the case
|
||||
$this->assertSame($n1 + 1, $this->getNumberOfScenes());
|
||||
$this->assertSame($g1 + 1, $this->getNumberOfSceneGroups());
|
||||
|
||||
// delete scene again. Number of scenes and number of connection groups must be what it was at the beginning
|
||||
$scene = $em->getRepository(Scene::class)->findOneBy($this->getTestSceneData());
|
||||
$scene->delete($em);
|
||||
$this->flushAndClear();
|
||||
unset($scene);
|
||||
|
||||
// recount and assert that this is the case
|
||||
$this->assertSame($n1, $this->getNumberOfScenes());
|
||||
$this->assertSame($g1, $this->getNumberOfSceneGroups());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+14
-14
@@ -53,38 +53,38 @@ scenes:
|
||||
template: "lotgd/tests/none"
|
||||
scene_connection_groups:
|
||||
-
|
||||
scene_id: 4
|
||||
scene: 4
|
||||
name: "lotgd/tests/none/child1"
|
||||
title: "Child 1"
|
||||
-
|
||||
scene_id: 4
|
||||
scene: 4
|
||||
name: "lotgd/tests/none/child2"
|
||||
title: "Child 2"
|
||||
scene_connections:
|
||||
-
|
||||
outgoing_scene_id: 1
|
||||
incoming_scene_id: 2
|
||||
outgoingScene: 1
|
||||
incomingScene: 2
|
||||
directionality: 0
|
||||
-
|
||||
outgoing_scene_id: 1
|
||||
incoming_scene_id: 3
|
||||
outgoingScene: 1
|
||||
incomingScene: 3
|
||||
directionality: 0
|
||||
-
|
||||
outgoing_scene_id: 1
|
||||
incoming_scene_id: 4
|
||||
outgoingScene: 1
|
||||
incomingScene: 4
|
||||
directionality: 0
|
||||
-
|
||||
outgoing_scene_id: 4
|
||||
incoming_scene_id: 5
|
||||
outgoingScene: 4
|
||||
incomingScene: 5
|
||||
outgoingConnectionGroupName: "lotgd/tests/none/child1"
|
||||
directionality: 0
|
||||
-
|
||||
outgoing_scene_id: 4
|
||||
incoming_scene_id: 6
|
||||
outgoingScene: 4
|
||||
incomingScene: 6
|
||||
outgoingConnectionGroupName: "lotgd/tests/none/child2"
|
||||
directionality: 0
|
||||
-
|
||||
outgoing_scene_id: 5
|
||||
incoming_scene_id: 6
|
||||
outgoingScene: 5
|
||||
incomingScene: 6
|
||||
directionality: 1
|
||||
|
||||
|
||||
+10
-10
@@ -26,28 +26,28 @@ scenes:
|
||||
template: "lotgd/tests/orphan"
|
||||
scene_connection_groups:
|
||||
-
|
||||
scene_id: 1
|
||||
scene: 1
|
||||
name: "lotgd/tests/village/outside"
|
||||
title: "Outside"
|
||||
-
|
||||
scene_id: 1
|
||||
scene: 1
|
||||
name: "lotgd/tests/village/market"
|
||||
title: "Market"
|
||||
-
|
||||
scene_id: 1
|
||||
scene: 1
|
||||
name: "lotgd/tests/village/empty"
|
||||
title: "Empty"
|
||||
-
|
||||
scene_id: 2
|
||||
scene: 2
|
||||
name: "lotgd/tests/forest/category"
|
||||
title: "Empty"
|
||||
scene_connections:
|
||||
-
|
||||
outgoing_scene_id: 1
|
||||
incoming_scene_id: 2
|
||||
outgoingScene: 1
|
||||
incomingScene: 2
|
||||
-
|
||||
outgoing_scene_id: 1
|
||||
incoming_scene_id: 3
|
||||
outgoingScene: 1
|
||||
incomingScene: 3
|
||||
-
|
||||
outgoing_scene_id: 1
|
||||
incoming_scene_id: 4
|
||||
outgoingScene: 1
|
||||
incomingScene: 4
|
||||
@@ -34,8 +34,8 @@ scenes:
|
||||
template: "lotgd/tests/weaponry"
|
||||
scene_connections:
|
||||
-
|
||||
outgoing_scene_id: 1
|
||||
incoming_scene_id: 2
|
||||
outgoingScene: 1
|
||||
incomingScene: 2
|
||||
-
|
||||
outgoing_scene_id: 1
|
||||
incoming_scene_id: 3
|
||||
outgoingScene: 1
|
||||
incomingScene: 3
|
||||
Reference in New Issue
Block a user