Skip to content

Commit ac7c845

Browse files
committed
Added Deletor trait.
1 parent 7afa272 commit ac7c845

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

cli-config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
};
1010

1111
$configuration = Setup::createAnnotationMetadataConfiguration(["src/Models"], true);
12-
$configuration->setQuoteStrategy(new \Doctrine\ORM\Mapping\AnsiQuoteStrategy());
12+
$configuration->setQuoteStrategy(new AnsiQuoteStrategy());
1313

1414
$entityManager = EntityManager::create(["url" => "sqlite://:memory:"], $configuration);
1515

src/Models/Character.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace LotGD\Core\Models;
55

6-
use LotGD\Core\Tools\Model\Creator;
6+
use LotGD\Core\Tools\Model\{Creator, Deletor};
77
use Doctrine\ORM\Mapping\Entity;
88

99
/**
@@ -14,6 +14,7 @@
1414
*/
1515
class Character {
1616
use Creator;
17+
use Deletor;
1718

1819
/** @Id @Column(type="integer") @GeneratedValue */
1920
private $id;
@@ -28,7 +29,7 @@ class Character {
2829
private $properties;
2930

3031
/** @var array */
31-
protected static $fillable = [
32+
private static $fillable = [
3233
"name",
3334
"maxhealth",
3435
];
@@ -65,6 +66,10 @@ protected function generateDisplayName() {
6566
$this->displayName = $this->name;
6667
}
6768

69+
/**
70+
* Returns displayName, a combination of title, name and suffix, mixed with colour codes
71+
* @return string The displayName
72+
*/
6873
public function getDisplayName(): string {
6974
return $this->displayName;
7075
}

src/Tools/Model/Creator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
};
1212

1313
/**
14-
* Provides methods for creating new instances.
14+
* Provides methods for creating new entities
1515
*/
1616
trait Creator {
1717
/**

src/Tools/Model/Deletor.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace LotGD\Core\Tools\Model;
5+
6+
use Doctrine\ORM\EntityManagerInterface;
7+
8+
use LotGD\Core\Exceptions\{
9+
AttributeMissingException,
10+
WrongTypeException
11+
};
12+
13+
/**
14+
* Provides methods for deleting entities.
15+
*/
16+
trait Deletor {
17+
/**
18+
* Deletes the entity
19+
* @param EntityManagerInterface $em
20+
*/
21+
public function delete(EntityManagerInterface $em) {
22+
$em->remove($this);
23+
$em->flush();
24+
}
25+
}

tests/Models/CharacterModelTest.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public function testCreation() {
4141

4242
$entities = $this->getEntityManager()->getRepository(Character::class)
4343
->findAll();
44-
$this->assertEquals(count($entities), count($characters));
44+
$this->assertCount(count($characters), $entities);
45+
46+
return $entities;
4547
}
4648

4749
/**
@@ -63,4 +65,20 @@ public function testCreationTypes() {
6365
$char = Character::create($faultyCharacterData);
6466
}
6567
}
68+
69+
/**
70+
* @depends testCreation
71+
*/
72+
public function testDeletion(array $characters) {
73+
foreach($characters as $character) {
74+
$character->save($this->getEntityManager());
75+
}
76+
77+
$character = $this->getEntityManager()->getRepository(Character::class)->find(1);
78+
$character->delete($this->getEntityManager());
79+
80+
$entities = $this->getEntityManager()->getRepository(Character::class)
81+
->findAll();
82+
$this->assertCount(1, $entities);
83+
}
6684
}

0 commit comments

Comments
 (0)