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\EntityErrorEvent; |
9
|
|
|
use Arp\DoctrineEntityRepository\Persistence\PersistServiceInterface; |
10
|
|
|
use Arp\EventDispatcher\Resolver\EventNameAwareInterface; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @covers \Arp\DoctrineEntityRepository\Persistence\Event\EntityErrorEvent |
18
|
|
|
* |
19
|
|
|
* @author Alex Patterson <[email protected]> |
20
|
|
|
* @package ArpTest\DoctrineEntityRepository\Persistence\Event |
21
|
|
|
*/ |
22
|
|
|
final class EntityErrorEventTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private string $eventName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var PersistServiceInterface&MockObject |
31
|
|
|
*/ |
32
|
|
|
private $persistService; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var EntityManagerInterface&MockObject |
36
|
|
|
*/ |
37
|
|
|
private $entityManager; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Throwable |
41
|
|
|
*/ |
42
|
|
|
private \Throwable $exception; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var LoggerInterface&MockObject |
46
|
|
|
*/ |
47
|
|
|
private $logger; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array<mixed> |
51
|
|
|
*/ |
52
|
|
|
private array $params = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Prepare the test case dependencies |
56
|
|
|
*/ |
57
|
|
|
public function setUp(): void |
58
|
|
|
{ |
59
|
|
|
$this->eventName = EntityEventName::CREATE_ERROR; |
60
|
|
|
|
61
|
|
|
$this->entityManager = $this->createMock(EntityManagerInterface::class); |
62
|
|
|
|
63
|
|
|
$this->persistService = $this->createMock(PersistServiceInterface::class); |
64
|
|
|
|
65
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
66
|
|
|
|
67
|
|
|
$this->exception = new \Exception('This is a test exception message'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Assert that the event implement EventNameAwareInterface |
72
|
|
|
*/ |
73
|
|
|
public function testIsInstanceOfEventNameAwareInterface(): void |
74
|
|
|
{ |
75
|
|
|
$event = new EntityErrorEvent( |
76
|
|
|
$this->eventName, |
77
|
|
|
$this->persistService, |
78
|
|
|
$this->entityManager, |
79
|
|
|
$this->logger, |
80
|
|
|
$this->exception, |
81
|
|
|
$this->params |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$this->assertInstanceOf(EventNameAwareInterface::class, $event); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Assert that hasException() will return TRUE when the exception instance is set |
89
|
|
|
*/ |
90
|
|
|
public function testHasExceptionWillReturnTrueIfExceptionIsSet(): void |
91
|
|
|
{ |
92
|
|
|
$event = new EntityErrorEvent( |
93
|
|
|
$this->eventName, |
94
|
|
|
$this->persistService, |
95
|
|
|
$this->entityManager, |
96
|
|
|
$this->logger, |
97
|
|
|
$this->exception, |
98
|
|
|
$this->params |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$this->assertTrue($event->hasException()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Assert that hasException() will return FALSE when the exception instance is NOT set |
106
|
|
|
*/ |
107
|
|
|
public function testHasExceptionWillReturnFalseIFNoExceptionIsSet(): void |
108
|
|
|
{ |
109
|
|
|
$event = new EntityErrorEvent( |
110
|
|
|
$this->eventName, |
111
|
|
|
$this->persistService, |
112
|
|
|
$this->entityManager, |
113
|
|
|
$this->logger, |
114
|
|
|
null, |
115
|
|
|
$this->params |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$this->assertFalse($event->hasException()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Assert the constructor set exception class is returned from getException() |
123
|
|
|
*/ |
124
|
|
|
public function testGetExceptionWillReturnException(): void |
125
|
|
|
{ |
126
|
|
|
$event = new EntityErrorEvent( |
127
|
|
|
$this->eventName, |
128
|
|
|
$this->persistService, |
129
|
|
|
$this->entityManager, |
130
|
|
|
$this->logger, |
131
|
|
|
$this->exception, |
132
|
|
|
$this->params |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$this->assertSame($this->exception, $event->getException()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Assert that the exception can be set and get via setException() and getException() |
140
|
|
|
*/ |
141
|
|
|
public function testSetAndGetException(): void |
142
|
|
|
{ |
143
|
|
|
$event = new EntityErrorEvent( |
144
|
|
|
$this->eventName, |
145
|
|
|
$this->persistService, |
146
|
|
|
$this->entityManager, |
147
|
|
|
$this->logger, |
148
|
|
|
$this->exception, |
149
|
|
|
$this->params |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$newException = new \Exception('This is a new exception message'); |
153
|
|
|
|
154
|
|
|
$event->setException($newException); |
155
|
|
|
|
156
|
|
|
$this->assertSame($newException, $event->getException()); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|