Completed
Push — master ( 52a24b...0631b2 )
by Alex
16s queued 13s
created

testFlushWillProxyToEntityManagerFlush()   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
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
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>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<EntityInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<EntityInterface>.
Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Doctrine\ORM\EntityManagerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        $this->entityManager->/** @scrutinizer ignore-call */ 
67
                              expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            ->method('flush')
68
            ->willThrowException($exception);
69
70
        $this->logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Log\LoggerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $this->logger->/** @scrutinizer ignore-call */ 
71
                       expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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