Added Deletor trait.
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@ use Doctrine\ORM\{
|
|||||||
};
|
};
|
||||||
|
|
||||||
$configuration = Setup::createAnnotationMetadataConfiguration(["src/Models"], true);
|
$configuration = Setup::createAnnotationMetadataConfiguration(["src/Models"], true);
|
||||||
$configuration->setQuoteStrategy(new \Doctrine\ORM\Mapping\AnsiQuoteStrategy());
|
$configuration->setQuoteStrategy(new AnsiQuoteStrategy());
|
||||||
|
|
||||||
$entityManager = EntityManager::create(["url" => "sqlite://:memory:"], $configuration);
|
$entityManager = EntityManager::create(["url" => "sqlite://:memory:"], $configuration);
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace LotGD\Core\Models;
|
namespace LotGD\Core\Models;
|
||||||
|
|
||||||
use LotGD\Core\Tools\Model\Creator;
|
use LotGD\Core\Tools\Model\{Creator, Deletor};
|
||||||
use Doctrine\ORM\Mapping\Entity;
|
use Doctrine\ORM\Mapping\Entity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -14,6 +14,7 @@ use Doctrine\ORM\Mapping\Entity;
|
|||||||
*/
|
*/
|
||||||
class Character {
|
class Character {
|
||||||
use Creator;
|
use Creator;
|
||||||
|
use Deletor;
|
||||||
|
|
||||||
/** @Id @Column(type="integer") @GeneratedValue */
|
/** @Id @Column(type="integer") @GeneratedValue */
|
||||||
private $id;
|
private $id;
|
||||||
@@ -28,7 +29,7 @@ class Character {
|
|||||||
private $properties;
|
private $properties;
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
protected static $fillable = [
|
private static $fillable = [
|
||||||
"name",
|
"name",
|
||||||
"maxhealth",
|
"maxhealth",
|
||||||
];
|
];
|
||||||
@@ -65,6 +66,10 @@ class Character {
|
|||||||
$this->displayName = $this->name;
|
$this->displayName = $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns displayName, a combination of title, name and suffix, mixed with colour codes
|
||||||
|
* @return string The displayName
|
||||||
|
*/
|
||||||
public function getDisplayName(): string {
|
public function getDisplayName(): string {
|
||||||
return $this->displayName;
|
return $this->displayName;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ use LotGD\Core\Exceptions\{
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides methods for creating new instances.
|
* Provides methods for creating new entities
|
||||||
*/
|
*/
|
||||||
trait Creator {
|
trait Creator {
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace LotGD\Core\Tools\Model;
|
||||||
|
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
use LotGD\Core\Exceptions\{
|
||||||
|
AttributeMissingException,
|
||||||
|
WrongTypeException
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides methods for deleting entities.
|
||||||
|
*/
|
||||||
|
trait Deletor {
|
||||||
|
/**
|
||||||
|
* Deletes the entity
|
||||||
|
* @param EntityManagerInterface $em
|
||||||
|
*/
|
||||||
|
public function delete(EntityManagerInterface $em) {
|
||||||
|
$em->remove($this);
|
||||||
|
$em->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,7 +41,9 @@ class CharacterModelTest extends ModelTestCase {
|
|||||||
|
|
||||||
$entities = $this->getEntityManager()->getRepository(Character::class)
|
$entities = $this->getEntityManager()->getRepository(Character::class)
|
||||||
->findAll();
|
->findAll();
|
||||||
$this->assertEquals(count($entities), count($characters));
|
$this->assertCount(count($characters), $entities);
|
||||||
|
|
||||||
|
return $entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,4 +65,20 @@ class CharacterModelTest extends ModelTestCase {
|
|||||||
$char = Character::create($faultyCharacterData);
|
$char = Character::create($faultyCharacterData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testCreation
|
||||||
|
*/
|
||||||
|
public function testDeletion(array $characters) {
|
||||||
|
foreach($characters as $character) {
|
||||||
|
$character->save($this->getEntityManager());
|
||||||
|
}
|
||||||
|
|
||||||
|
$character = $this->getEntityManager()->getRepository(Character::class)->find(1);
|
||||||
|
$character->delete($this->getEntityManager());
|
||||||
|
|
||||||
|
$entities = $this->getEntityManager()->getRepository(Character::class)
|
||||||
|
->findAll();
|
||||||
|
$this->assertCount(1, $entities);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user