EagerAcknowledgementHandlerTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testHandleInvalidMessage() 0 18 1
A testHandle() 0 19 1
A setUp() 0 10 1
A testHandleWorkerWithThrownException() 0 13 2
1
<?php
2
3
/**
4
 * This file is part of graze/queue.
5
 *
6
 * Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://github.com/graze/queue/blob/master/LICENSE MIT
12
 *
13
 * @link    https://github.com/graze/queue
14
 */
15
16
namespace Graze\Queue\Handler;
17
18
use ArrayIterator;
19
use Graze\Queue\Adapter\AdapterInterface;
20
use Graze\Queue\Message\MessageInterface;
21
use Mockery as m;
22
use Mockery\MockInterface;
23
use Graze\Queue\Test\TestCase;
24
use RuntimeException;
25
26
class EagerAcknowledgementHandlerTest extends TestCase
27
{
28
    /** @var AdapterInterface|MockInterface */
29
    private $adapter;
30
    /** @var MessageInterface|MockInterface */
31
    private $messageA;
32
    /** @var MessageInterface|MockInterface */
33
    private $messageB;
34
    /** @var MessageInterface|MockInterface */
35
    private $messageC;
36
    /** @var ArrayIterator */
37
    private $messages;
38
    /** @var EagerAcknowledgementHandler */
39
    private $handler;
40
41
    public function setUp()
42
    {
43
        $this->adapter = m::mock(AdapterInterface::class);
44
45
        $this->messageA = $a = m::mock(MessageInterface::class);
46
        $this->messageB = $b = m::mock(MessageInterface::class);
47
        $this->messageC = $c = m::mock(MessageInterface::class);
48
        $this->messages = new ArrayIterator([$a, $b, $c]);
49
50
        $this->handler = new EagerAcknowledgementHandler();
51
    }
52
53
    public function testHandle()
54
    {
55
        $handler = $this->handler;
56
57
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on Graze\Queue\Message\MessageInterface. ( Ignorable by Annotation )

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

57
        $this->messageA->/** @scrutinizer ignore-call */ 
58
                         shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);

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...
58
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
59
        $this->messageC->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
60
61
        // @see https://github.com/padraic/mockery/issues/331
62
        $this->adapter->shouldReceive('acknowledge')->once()->with(m::mustBe([$this->messageA]));
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on Graze\Queue\Adapter\AdapterInterface. ( Ignorable by Annotation )

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

62
        $this->adapter->/** @scrutinizer ignore-call */ 
63
                        shouldReceive('acknowledge')->once()->with(m::mustBe([$this->messageA]));

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...
63
        $this->adapter->shouldReceive('acknowledge')->once()->with(m::mustBe([$this->messageB]));
64
        $this->adapter->shouldReceive('acknowledge')->once()->with(m::mustBe([$this->messageC]));
65
66
        $msgs = [];
67
        $handler($this->messages, $this->adapter, function ($msg) use (&$msgs) {
68
            $msgs[] = $msg;
69
        });
70
71
        assertThat($msgs, is(identicalTo(iterator_to_array($this->messages))));
72
    }
73
74
    public function testHandleInvalidMessage()
75
    {
76
        $handler = $this->handler;
77
78
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
79
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(false);
80
        $this->messageC->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
81
82
        // @see https://github.com/padraic/mockery/issues/331
83
        $this->adapter->shouldReceive('acknowledge')->once()->with(m::mustBe([$this->messageA]));
84
        $this->adapter->shouldReceive('acknowledge')->once()->with(m::mustBe([$this->messageC]));
85
86
        $msgs = [];
87
        $handler($this->messages, $this->adapter, function ($msg) use (&$msgs) {
88
            $msgs[] = $msg;
89
        });
90
91
        assertThat($msgs, is(identicalTo([$this->messageA, $this->messageC])));
92
    }
93
94
    /**
95
     * @expectedException \RuntimeException
96
     * @expectedExceptionMessage foo
97
     */
98
    public function testHandleWorkerWithThrownException()
99
    {
100
        $handler = $this->handler;
101
102
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
103
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
104
105
        // @see https://github.com/padraic/mockery/issues/331
106
        $this->adapter->shouldReceive('acknowledge')->once()->with(m::mustBe([$this->messageA]));
107
108
        $handler($this->messages, $this->adapter, function ($msg) {
109
            if ($msg === $this->messageB) {
110
                throw new RuntimeException('foo');
111
            }
112
        });
113
    }
114
}
115