Completed
Push — master ( f9d483...c44aff )
by Alex
15s queued 12s
created

EntityEventTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\DoctrineEntityRepository\Persistence\Event;
6
7
use Arp\DoctrineEntityRepository\Constant\EntityEventName;
8
use Arp\DoctrineEntityRepository\Persistence\Event\EntityEvent;
9
use Arp\Entity\EntityInterface;
10
use Arp\EventDispatcher\Event\ParametersInterface;
11
use Arp\EventDispatcher\Resolver\EventNameAwareInterface;
12
use Doctrine\ORM\EntityManagerInterface;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * @author  Alex Patterson <[email protected]>
18
 * @package ArpTest\DoctrineEntityRepository\Persistence\Event
19
 */
20
class EntityEventTest extends TestCase
21
{
22
    /**
23
     * @var string
24
     */
25
    private string $entityName;
26
27
    /**
28
     * @var string
29
     */
30
    private string $eventName;
31
32
    /**
33
     * @var EntityManagerInterface|MockObject
34
     */
35
    private $entityManager;
36
37
    /**
38
     * Prepare the test case dependencies.
39
     */
40
    public function setUp(): void
41
    {
42
        $this->entityName = EntityInterface::class;
43
44
        $this->eventName = EntityEventName::CREATE;
45
46
        $this->entityManager = $this->getMockForAbstractClass(EntityManagerInterface::class);
47
    }
48
49
    /**
50
     * Assert that the entity event implements the EventNameAwareInterface.
51
     *
52
     * @covers \Arp\DoctrineEntityRepository\Persistence\Event\EntityEvent::__construct
53
     */
54
    public function testEntityEventImplementsEventNameAwareInterface(): void
55
    {
56
        $entityEvent = new EntityEvent($this->eventName, $this->entityName, $this->entityManager);
57
58
        $this->assertInstanceOf(EventNameAwareInterface::class, $entityEvent);
59
    }
60
61
    /**
62
     * Assert that the event name can be returned via getEventName().
63
     *
64
     * @covers \Arp\DoctrineEntityRepository\Persistence\Event\EntityEvent::getEventName
65
     */
66
    public function testGetEventNameWillReturnNamedEvent(): void
67
    {
68
        $entityEvent = new EntityEvent($this->eventName, $this->entityName, $this->entityManager);
69
70
        $this->assertSame($this->eventName, $entityEvent->getEventName());
71
    }
72
73
    /**
74
     * Assert that the event name can be returned via getEntityName().
75
     *
76
     * @covers \Arp\DoctrineEntityRepository\Persistence\Event\EntityEvent::getEntityName
77
     */
78
    public function testGetEntityNameWillReturnEntityFullyQualifiedClassName(): void
79
    {
80
        $entityEvent = new EntityEvent($this->eventName, $this->entityName, $this->entityManager);
81
82
        $this->assertSame($this->entityName, $entityEvent->getEntityName());
83
    }
84
85
    /**
86
     * Assert that the entity manager can be returned via getEntityManager().
87
     *
88
     * @covers \Arp\DoctrineEntityRepository\Persistence\Event\EntityEvent::getEntityManager
89
     */
90
    public function testGetEntityManagerWillReturnEntityManager(): void
91
    {
92
        $entityEvent = new EntityEvent($this->eventName, $this->entityName, $this->entityManager);
93
94
        $this->assertSame($this->entityManager, $entityEvent->getEntityManager());
95
    }
96
97
    /**
98
     * Assert that the event parameters instance can be retrieved via getParameters().
99
     *
100
     * @covers \Arp\DoctrineEntityRepository\Persistence\Event\EntityEvent::getParameters
101
     */
102
    public function testGetParametersReturnsParametersCollection(): void
103
    {
104
        $entityEvent = new EntityEvent($this->eventName, $this->entityName, $this->entityManager);
105
106
        $this->assertInstanceOf(ParametersInterface::class, $entityEvent->getParameters());
107
    }
108
109
    /**
110
     * Assert that hasEntity() will return false if no entity is defined.
111
     *
112
     * @covers \Arp\DoctrineEntityRepository\Persistence\Event\EntityEvent::hasEntity
113
     */
114
    public function testHasEntityWillReturnFalseForNullEntity(): void
115
    {
116
        $entityEvent = new EntityEvent($this->eventName, $this->entityName, $this->entityManager);
117
118
        $this->assertFalse($entityEvent->hasEntity());
119
    }
120
121
    /**
122
     * Assert that hasEntity() will return false if no entity is defined.
123
     *
124
     * @covers \Arp\DoctrineEntityRepository\Persistence\Event\EntityEvent::hasEntity
125
     */
126
    public function testHasEntityWillReturnTrueWhenEntityIsSet(): void
127
    {
128
        $entityEvent = new EntityEvent($this->eventName, $this->entityName, $this->entityManager);
129
130
        /** @var EntityInterface|MockObject $entity */
131
        $entity = $this->getMockForAbstractClass(EntityInterface::class);
132
133
        $entityEvent->setEntity($entity);
134
135
        $this->assertTrue($entityEvent->hasEntity());
136
    }
137
138
    /**
139
     * Asset that an entity can be set/get from setEntity() and getEntity()
140
     *
141
     * @covers \Arp\DoctrineEntityRepository\Persistence\Event\EntityEvent::getEntity
142
     * @covers \Arp\DoctrineEntityRepository\Persistence\Event\EntityEvent::setEntity
143
     */
144
    public function testGetSetEntity(): void
145
    {
146
        $entityEvent = new EntityEvent($this->eventName, $this->entityName, $this->entityManager);
147
148
        /** @var EntityInterface|MockObject $entity */
149
        $entity = $this->getMockForAbstractClass(EntityInterface::class);
150
151
        $entityEvent->setEntity($entity);
152
153
        $this->assertSame($entity, $entityEvent->getEntity());
154
    }
155
}
156