Issues (59)

unit/Handler/NullAcknowledgementHandlerTest.php (1 issue)

Labels
Severity
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 NullAcknowledgementHandlerTest 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 NullAcknowledgementHandler */
39
    private $handler;
40
41
    public function setUp()
42
    {
43
        $this->adapter = m::mock('Graze\Queue\Adapter\AdapterInterface');
44
45
        $this->messageA = $a = m::mock('Graze\Queue\Message\MessageInterface');
46
        $this->messageB = $b = m::mock('Graze\Queue\Message\MessageInterface');
47
        $this->messageC = $c = m::mock('Graze\Queue\Message\MessageInterface');
48
        $this->messages = new ArrayIterator([$a, $b, $c]);
49
50
        $this->handler = new NullAcknowledgementHandler();
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
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
        $msgs = [];
62
        $handler($this->messages, $this->adapter, function ($msg) use (&$msgs) {
63
            $msgs[] = $msg;
64
        });
65
66
        assertThat($msgs, is(identicalTo(iterator_to_array($this->messages))));
67
    }
68
69
    public function testHandleInvalidMessage()
70
    {
71
        $handler = $this->handler;
72
73
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
74
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(false);
75
        $this->messageC->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
76
77
        $msgs = [];
78
        $handler($this->messages, $this->adapter, function ($msg) use (&$msgs) {
79
            $msgs[] = $msg;
80
        });
81
82
        assertThat($msgs, is(identicalTo([$this->messageA, $this->messageC])));
83
    }
84
85
    /**
86
     * @expectedException \RuntimeException
87
     * @expectedExceptionMessage foo
88
     */
89
    public function testHandleWorkerWithThrownException()
90
    {
91
        $handler = $this->handler;
92
93
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
94
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
95
96
        $handler($this->messages, $this->adapter, function ($msg) {
97
            if ($msg === $this->messageB) {
98
                throw new RuntimeException('foo');
99
            }
100
        });
101
    }
102
}
103