Test Failed
Pull Request — master (#8)
by Alex
04:30
created

EntityErrorEventTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetAndGetException() 0 16 1
A setUp() 0 11 1
A testIsInstanceOfEventNameAwareInterface() 0 12 1
A testGetExceptionWillReturnException() 0 12 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\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
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 the constructor set exception class is returned from getException()
89
     */
90
    public function testGetExceptionWillReturnException(): 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->assertSame($this->exception, $event->getException());
102
    }
103
104
    /**
105
     * Assert that the exception can be set and get via setException() and getException()
106
     */
107
    public function testSetAndGetException(): void
108
    {
109
        $event = new EntityErrorEvent(
110
            $this->eventName,
111
            $this->persistService,
112
            $this->entityManager,
113
            $this->logger,
114
            $this->exception,
115
            $this->params
116
        );
117
118
        $newException = new \Exception('This is a new exception message');
119
120
        $event->setException($newException);
121
122
        $this->assertSame($newException, $event->getException());
123
    }
124
}
125