|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace TMV\Laminas\Messenger\Test\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use stdClass; |
|
11
|
|
|
use Symfony\Component\Messenger\Envelope; |
|
12
|
|
|
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; |
|
13
|
|
|
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp; |
|
14
|
|
|
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase; |
|
15
|
|
|
use TMV\Laminas\Messenger\Middleware\DoctrineClearEntityManagerMiddleware; |
|
16
|
|
|
|
|
17
|
|
|
class DoctrineClearEntityManagerMiddlewareTest extends MiddlewareTestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testMiddlewareClearEntityManager(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
|
22
|
|
|
$entityManager->expects($this->once()) |
|
23
|
|
|
->method('clear'); |
|
24
|
|
|
$managerRegistry = $this->createMock(ManagerRegistry::class); |
|
25
|
|
|
$managerRegistry |
|
|
|
|
|
|
26
|
|
|
->method('getManager') |
|
27
|
|
|
->with('default') |
|
28
|
|
|
->willReturn($entityManager); |
|
29
|
|
|
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default'); |
|
|
|
|
|
|
30
|
|
|
$envelope = new Envelope(new stdClass(), [ |
|
31
|
|
|
new ConsumedByWorkerStamp(), |
|
32
|
|
|
]); |
|
33
|
|
|
$middleware->handle($envelope, $this->getStackMock()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testMiddlewareShouldNotClearEntityManagerIfNotConsumedByWorker(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
|
39
|
|
|
$entityManager->expects($this->never()) |
|
40
|
|
|
->method('clear'); |
|
41
|
|
|
$managerRegistry = $this->createMock(ManagerRegistry::class); |
|
42
|
|
|
$managerRegistry |
|
|
|
|
|
|
43
|
|
|
->method('getManager') |
|
44
|
|
|
->with('default') |
|
45
|
|
|
->willReturn($entityManager); |
|
46
|
|
|
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default'); |
|
|
|
|
|
|
47
|
|
|
$envelope = new Envelope(new stdClass()); |
|
48
|
|
|
$middleware->handle($envelope, $this->getStackMock()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testInvalidEntityManagerThrowsException(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$managerRegistry = $this->createMock(ManagerRegistry::class); |
|
54
|
|
|
$managerRegistry |
|
|
|
|
|
|
55
|
|
|
->method('getManager') |
|
56
|
|
|
->with('unknown_manager') |
|
57
|
|
|
->will($this->throwException(new InvalidArgumentException())); |
|
58
|
|
|
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'unknown_manager'); |
|
|
|
|
|
|
59
|
|
|
$this->expectException(UnrecoverableMessageHandlingException::class); |
|
60
|
|
|
$middleware->handle(new Envelope(new stdClass()), $this->getStackMock(false)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testMiddlewareDoesNotClearInNonWorkerContext(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
|
66
|
|
|
$entityManager->expects($this->never()) |
|
67
|
|
|
->method('clear'); |
|
68
|
|
|
$managerRegistry = $this->createMock(ManagerRegistry::class); |
|
69
|
|
|
$managerRegistry |
|
|
|
|
|
|
70
|
|
|
->method('getManager') |
|
71
|
|
|
->with('default') |
|
72
|
|
|
->willReturn($entityManager); |
|
73
|
|
|
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default'); |
|
|
|
|
|
|
74
|
|
|
$envelope = new Envelope(new stdClass()); |
|
75
|
|
|
$middleware->handle($envelope, $this->getStackMock()); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
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.