From ac7c845d071adaeb9bdea5a4fca17c870f7f43d7 Mon Sep 17 00:00:00 2001 From: Basilius Sauter Date: Sat, 16 Apr 2016 07:30:03 +0200 Subject: [PATCH] Added Deletor trait. --- cli-config.php | 2 +- src/Models/Character.php | 9 +++++++-- src/Tools/Model/Creator.php | 2 +- src/Tools/Model/Deletor.php | 25 +++++++++++++++++++++++++ tests/Models/CharacterModelTest.php | 20 +++++++++++++++++++- 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/Tools/Model/Deletor.php diff --git a/cli-config.php b/cli-config.php index 4e3ece8..f68f108 100644 --- a/cli-config.php +++ b/cli-config.php @@ -9,7 +9,7 @@ use Doctrine\ORM\{ }; $configuration = Setup::createAnnotationMetadataConfiguration(["src/Models"], true); -$configuration->setQuoteStrategy(new \Doctrine\ORM\Mapping\AnsiQuoteStrategy()); +$configuration->setQuoteStrategy(new AnsiQuoteStrategy()); $entityManager = EntityManager::create(["url" => "sqlite://:memory:"], $configuration); diff --git a/src/Models/Character.php b/src/Models/Character.php index 2521614..f5a73a4 100644 --- a/src/Models/Character.php +++ b/src/Models/Character.php @@ -3,7 +3,7 @@ declare(strict_types=1); namespace LotGD\Core\Models; -use LotGD\Core\Tools\Model\Creator; +use LotGD\Core\Tools\Model\{Creator, Deletor}; use Doctrine\ORM\Mapping\Entity; /** @@ -14,6 +14,7 @@ use Doctrine\ORM\Mapping\Entity; */ class Character { use Creator; + use Deletor; /** @Id @Column(type="integer") @GeneratedValue */ private $id; @@ -28,7 +29,7 @@ class Character { private $properties; /** @var array */ - protected static $fillable = [ + private static $fillable = [ "name", "maxhealth", ]; @@ -65,6 +66,10 @@ class Character { $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 { return $this->displayName; } diff --git a/src/Tools/Model/Creator.php b/src/Tools/Model/Creator.php index 55b2920..d017b57 100644 --- a/src/Tools/Model/Creator.php +++ b/src/Tools/Model/Creator.php @@ -11,7 +11,7 @@ use LotGD\Core\Exceptions\{ }; /** - * Provides methods for creating new instances. + * Provides methods for creating new entities */ trait Creator { /** diff --git a/src/Tools/Model/Deletor.php b/src/Tools/Model/Deletor.php new file mode 100644 index 0000000..29b39b8 --- /dev/null +++ b/src/Tools/Model/Deletor.php @@ -0,0 +1,25 @@ +remove($this); + $em->flush(); + } +} diff --git a/tests/Models/CharacterModelTest.php b/tests/Models/CharacterModelTest.php index e30665d..57511ce 100644 --- a/tests/Models/CharacterModelTest.php +++ b/tests/Models/CharacterModelTest.php @@ -41,7 +41,9 @@ class CharacterModelTest extends ModelTestCase { $entities = $this->getEntityManager()->getRepository(Character::class) ->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); } } + + /** + * @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); + } }