Completed
Push — master ( 03ffe6...2c1c5a )
by Will
11s
created

testHandleWorkerWithThrownException()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
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 Graze\Queue\Message\MessageInterface;
22
use Mockery as m;
23
use Mockery\MockInterface;
24
use PHPUnit_Framework_TestCase as TestCase;
25
use RuntimeException;
26
27
class BatchAcknowledgementHandlerTest 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 BatchAcknowledgementHandler */
40
    private $handler;
41
42 View Code Duplication
    public function setUp()
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...
43
    {
44
        $this->adapter = m::mock(AdapterInterface::class);
45
46
        $this->messageA = $a = m::mock(MessageInterface::class);
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(MessageInterface::class);
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(MessageInterface::class);
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 BatchAcknowledgementHandler(3);
52
    }
53
54
    public function testHandle()
55
    {
56
        $handler = $this->handler;
57
58
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Message\MessageInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
59
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
60
        $this->messageC->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
61
        $this->adapter->shouldReceive('acknowledge')->once()->with(iterator_to_array($this->messages));
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Adapter\AdapterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62
63
        $msgs = [];
64
        $handler($this->messages, $this->adapter, function ($msg, Closure $done) use (&$msgs) {
0 ignored issues
show
Unused Code introduced by
The parameter $done is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
            $msgs[] = $msg;
66
        });
67
68
        assertThat($msgs, is(identicalTo(iterator_to_array($this->messages))));
69
    }
70
71
    public function testHandleInvalidMessage()
72
    {
73
        $handler = $this->handler;
74
75
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Message\MessageInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(false);
77
        $this->messageC->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
78
        $this->adapter->shouldReceive('acknowledge')->once()->with([$this->messageA, $this->messageC]);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Adapter\AdapterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
79
80
        $msgs = [];
81
        $handler($this->messages, $this->adapter, function ($msg) use (&$msgs) {
82
            $msgs[] = $msg;
83
        });
84
85
        assertThat($msgs, is(identicalTo([$this->messageA, $this->messageC])));
86
    }
87
88 View Code Duplication
    public function testHandleWorkerWithThrownException()
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...
89
    {
90
        $handler = $this->handler;
91
92
        $this->messageA->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Message\MessageInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
93
        $this->messageB->shouldReceive('isValid')->once()->withNoArgs()->andReturn(true);
94
95
        $this->adapter->shouldReceive('acknowledge')->once()->with([$this->messageA]);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Queue\Adapter\AdapterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
96
97
        $this->setExpectedException('RuntimeException', 'foo');
98
99
        $handler($this->messages, $this->adapter, function ($msg) {
100
            if ($msg === $this->messageB) {
101
                throw new RuntimeException('foo');
102
            }
103
        });
104
    }
105
}
106