Completed
Push — master ( dfcaa1...caabc7 )
by Thomas Mauro
07:54
created

testMiddlewareShouldNotClearEntityManagerIfNotConsumedByWorker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
26
            ->method('getManager')
27
            ->with('default')
28
            ->willReturn($entityManager);
29
        $middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');
0 ignored issues
show
Documentation introduced by
$managerRegistry is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\P...stence\ManagerRegistry>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The class TMV\Laminas\Messenger\Mi...EntityManagerMiddleware has been deprecated with message: This middleware is deprecated and will be removed in 2.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
43
            ->method('getManager')
44
            ->with('default')
45
            ->willReturn($entityManager);
46
        $middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');
0 ignored issues
show
Documentation introduced by
$managerRegistry is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\P...stence\ManagerRegistry>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The class TMV\Laminas\Messenger\Mi...EntityManagerMiddleware has been deprecated with message: This middleware is deprecated and will be removed in 2.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
55
            ->method('getManager')
56
            ->with('unknown_manager')
57
            ->will($this->throwException(new InvalidArgumentException()));
58
        $middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'unknown_manager');
0 ignored issues
show
Documentation introduced by
$managerRegistry is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\P...stence\ManagerRegistry>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The class TMV\Laminas\Messenger\Mi...EntityManagerMiddleware has been deprecated with message: This middleware is deprecated and will be removed in 2.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
70
            ->method('getManager')
71
            ->with('default')
72
            ->willReturn($entityManager);
73
        $middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');
0 ignored issues
show
Documentation introduced by
$managerRegistry is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\P...stence\ManagerRegistry>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The class TMV\Laminas\Messenger\Mi...EntityManagerMiddleware has been deprecated with message: This middleware is deprecated and will be removed in 2.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
74
        $envelope = new Envelope(new stdClass());
75
        $middleware->handle($envelope, $this->getStackMock());
76
    }
77
}
78