Completed
Pull Request — master (#40)
by Harry
10:25
created

NullAcknowledgementHandlerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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 Closure;
20
use Graze\Queue\Adapter\AdapterInterface;
21
use GuzzleHttp\Message\MessageInterface;
22
use Mockery as m;
23
use Mockery\MockInterface;
24
use PHPUnit_Framework_TestCase as TestCase;
25
use RuntimeException;
26
27
class NullAcknowledgementHandlerTest extends TestCase
28
{
29
    /** @var AdapterInterface|MockInterface */
30
    private $adapter;
31
    /** @var MessageInterface|MockInterface */
32
    private $messageA;
33
    /** @var MessageInterface|MockInterface */
34
    private $messageB;
35
    /** @var MessageInterface|MockInterface */
36
    private $messageC;
37
    /** @var ArrayIterator */
38
    private $messages;
39
    /** @var NullAcknowledgementHandler */
40
    private $handler;
41
42
    public function setUp()
43
    {
44
        $this->adapter = m::mock('Graze\Queue\Adapter\AdapterInterface');
45
46
        $this->messageA = $a = m::mock('Graze\Queue\Message\MessageInterface');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
47
        $this->messageB = $b = m::mock('Graze\Queue\Message\MessageInterface');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
48
        $this->messageC = $c = m::mock('Graze\Queue\Message\MessageInterface');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $c. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
49
        $this->messages = new ArrayIterator([$a, $b, $c]);
50
51
        $this->handler = new NullAcknowledgementHandler();
52
    }
53
54 View Code Duplication
    public function testHandle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $handler = $this->handler;
57
58
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
59
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
60
        $this->messageC->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
61
62
        $msgs = [];
63
        $handler($this->messages, $this->adapter, function ($msg) use (&$msgs) {
64
            $msgs[] = $msg;
65
        });
66
67
        assertThat($msgs, is(identicalTo(iterator_to_array($this->messages))));
68
    }
69
70 View Code Duplication
    public function testHandleInvalidMessage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $handler = $this->handler;
73
74
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
75
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(false);
76
        $this->messageC->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
77
78
        $msgs = [];
79
        $handler($this->messages, $this->adapter, function ($msg) use (&$msgs) {
80
            $msgs[] = $msg;
81
        });
82
83
        assertThat($msgs, is(identicalTo([$this->messageA, $this->messageC])));
84
    }
85
86
    public function testHandleWorkerWithThrownException()
87
    {
88
        $handler = $this->handler;
89
90
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
91
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
92
93
        $this->setExpectedException('RuntimeException', 'foo');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
94
95
        $handler($this->messages, $this->adapter, function ($msg) {
96
            if ($msg === $this->messageB) {
97
                throw new RuntimeException('foo');
98
            }
99
        });
100
    }
101
}
102