|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ArpTest\LaminasDoctrine\Repository\Persistance; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\Entity\EntityInterface; |
|
8
|
|
|
use Arp\LaminasDoctrine\Repository\Persistence\Exception\PersistenceException; |
|
9
|
|
|
use Arp\LaminasDoctrine\Repository\Persistence\PersistService; |
|
10
|
|
|
use Arp\LaminasDoctrine\Repository\Persistence\PersistServiceInterface; |
|
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\LaminasDoctrine\Repository\Persistence\PersistService |
|
18
|
|
|
*/ |
|
19
|
|
|
final class PersistServiceTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var class-string<EntityInterface> |
|
|
|
|
|
|
23
|
|
|
*/ |
|
24
|
|
|
private string $entityName; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var EntityManagerInterface&MockObject |
|
28
|
|
|
*/ |
|
29
|
|
|
private EntityManagerInterface $entityManager; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var LoggerInterface&MockObject |
|
33
|
|
|
*/ |
|
34
|
|
|
private LoggerInterface $logger; |
|
35
|
|
|
|
|
36
|
|
|
public function setUp(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->entityName = EntityInterface::class; |
|
39
|
|
|
$this->entityManager = $this->createMock(EntityManagerInterface::class); |
|
40
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testImplementsPersistServiceInterface(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$persistService = new PersistService($this->entityName, $this->entityManager, $this->logger); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertInstanceOf(PersistServiceInterface::class, $persistService); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testGetEntityName(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$persistService = new PersistService($this->entityName, $this->entityManager, $this->logger); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame($this->entityName, $persistService->getEntityName()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testFlushWillThrowPersistenceExceptionOnError(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$persistService = new PersistService($this->entityName, $this->entityManager, $this->logger); |
|
60
|
|
|
|
|
61
|
|
|
$exceptionMessage = 'This is a test exception message for ' . __FUNCTION__; |
|
62
|
|
|
$exceptionCode = 717; |
|
63
|
|
|
|
|
64
|
|
|
$exception = new \RuntimeException($exceptionMessage, $exceptionCode); |
|
65
|
|
|
|
|
66
|
|
|
$this->entityManager->expects($this->once()) |
|
|
|
|
|
|
67
|
|
|
->method('flush') |
|
68
|
|
|
->willThrowException($exception); |
|
69
|
|
|
|
|
70
|
|
|
$this->logger->expects($this->once()) |
|
|
|
|
|
|
71
|
|
|
->method('error') |
|
72
|
|
|
->with($exceptionMessage, ['exception' => $exception, 'entity_name' => $this->entityName]); |
|
73
|
|
|
|
|
74
|
|
|
$this->expectException(PersistenceException::class); |
|
75
|
|
|
$this->expectExceptionMessage(sprintf('Failed to flush entity of type \'%s\'', $this->entityName)); |
|
76
|
|
|
$this->expectExceptionCode($exceptionCode); |
|
77
|
|
|
|
|
78
|
|
|
$persistService->flush(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @throws PersistenceException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function testFlushWillProxyToEntityManagerFlush(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$persistService = new PersistService($this->entityName, $this->entityManager, $this->logger); |
|
87
|
|
|
|
|
88
|
|
|
$this->entityManager->expects($this->once())->method('flush'); |
|
89
|
|
|
|
|
90
|
|
|
$persistService->flush(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|