Test Failed
Pull Request — master (#8)
by Alex
02:49
created

EntityErrorEventTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 32
c 1
b 0
f 0
dl 0
loc 89
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetAndGetException() 0 15 1
A setUp() 0 7 1
A testIsInstanceOfEventNameAwareInterface() 0 11 1
A testGetExceptionWillReturnException() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\DoctrineEntityRepository\Persistence;
6
7
use Arp\DoctrineEntityRepository\Constant\EntityEventName;
8
use Arp\DoctrineEntityRepository\Persistence\Event\EntityErrorEvent;
9
use Arp\Entity\EntityInterface;
10
use Arp\EventDispatcher\Resolver\EventNameAwareInterface;
11
use Doctrine\ORM\EntityManagerInterface;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
15
/**
16
 * @covers  \Arp\DoctrineEntityRepository\Persistence\Event\EntityErrorEvent
17
 *
18
 * @author  Alex Patterson <[email protected]>
19
 * @package ArpTest\DoctrineEntityRepository\Persistence
20
 */
21
final class EntityErrorEventTest extends TestCase
22
{
23
    /**
24
     * @var string
25
     */
26
    private string $eventName;
27
28
    /**
29
     * @var string
30
     */
31
    private string $entityName = EntityInterface::class;
32
33
    /**
34
     * @var EntityManagerInterface&MockObject
35
     */
36
    private $entityManager;
37
38
    /**
39
     * @var \Throwable
40
     */
41
    private \Throwable $exception;
42
43
    /**
44
     * @var array<mixed>
45
     */
46
    private array $params = [];
47
48
    /**
49
     * Prepare the test case dependencies
50
     */
51
    public function setUp(): void
52
    {
53
        $this->eventName = EntityEventName::CREATE_ERROR;
54
55
        $this->entityManager = $this->createMock(EntityManagerInterface::class);
56
57
        $this->exception = new \Exception('This is a test exception message');
58
    }
59
60
    /**
61
     * Assert that the event implement EventNameAwareInterface
62
     */
63
    public function testIsInstanceOfEventNameAwareInterface(): void
64
    {
65
        $event = new EntityErrorEvent(
66
            $this->eventName,
67
            $this->entityName,
68
            $this->entityManager,
69
            $this->exception,
70
            $this->params
71
        );
72
73
        $this->assertInstanceOf(EventNameAwareInterface::class, $event);
74
    }
75
76
    /**
77
     * Assert the constructor set exception class is returned from getException()
78
     */
79
    public function testGetExceptionWillReturnException(): void
80
    {
81
        $event = new EntityErrorEvent(
82
            $this->eventName,
83
            $this->entityName,
84
            $this->entityManager,
85
            $this->exception,
86
            $this->params
87
        );
88
89
        $this->assertSame($this->exception, $event->getException());
90
    }
91
92
    /**
93
     * Assert that the exception can be set and get via setException() and getException()
94
     */
95
    public function testSetAndGetException(): void
96
    {
97
        $event = new EntityErrorEvent(
98
            $this->eventName,
99
            $this->entityName,
100
            $this->entityManager,
101
            $this->exception,
102
            $this->params
103
        );
104
105
        $newException = new \Exception('This is a new exception message');
106
107
        $event->setException($newException);
108
109
        $this->assertSame($newException, $event->getException());
110
    }
111
}
112