DeleteControllerTest::testOnDispatchSuccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Sebaks\CrudTest\Controller\Admin;
4
5
use Sebaks\Crud\Controller\DeleteController;
6
use Sebaks\Crud\View\Model\DeleteViewModel;
7
8 View Code Duplication
class DeleteControllerTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    private $controller;
11
    private $id;
12
    private $deleterMock;
13
    private $view;
14
    private $eventMock;
15
16
    public function setUp()
17
    {
18
        $this->id = 22;
19
20
        $this->deleterMock = $this->getMockBuilder('T4webDomainInterface\Service\DeleterInterface')
21
            ->disableOriginalConstructor()
22
            ->getMock();
23
24
        $this->eventMock = $this->getMock('Zend\Mvc\MvcEvent');
25
26
        $this->view = new DeleteViewModel();
27
28
        $this->controller = new DeleteController(
29
            $this->id,
30
            $this->deleterMock,
31
            $this->view
32
        );
33
    }
34
35
    public function testOnDispatchSuccess()
36
    {
37
        $entityMock = $this->getMock('T4webDomainInterface\EntityInterface');
38
39
        $this->deleterMock->method('delete')
40
            ->with($this->id)
41
            ->willReturn($entityMock);
42
43
        $this->eventMock->method('setResult')
44
            ->with($this->view);
45
46
        $actualViewModel = $this->controller->onDispatch($this->eventMock);
47
48
        $this->assertSame($this->view, $actualViewModel);
49
        $this->assertSame($entityMock, $actualViewModel->getEntity());
0 ignored issues
show
Bug introduced by
The method getEntity does only exist in Sebaks\Crud\View\Model\DeleteViewModelInterface, but not in Zend\Http\Response.

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...
50
    }
51
52
    public function testOnDispatchFail()
53
    {
54
        $this->deleterMock->method('delete')
55
            ->with($this->id)
56
            ->willReturn(null);
57
58
        $this->eventMock->expects($this->never())
59
            ->method('setResult');
60
61
        $this->eventMock->method('getRouteMatch')
62
            ->willReturn($this->getMockBuilder('Zend\Mvc\Router\RouteMatch')
63
                ->setMethods(['setParam'])
64
                ->disableOriginalConstructor()
65
                ->getMock());
66
67
        $this->controller->setEvent($this->eventMock);
68
69
        $actualViewModel = $this->controller->onDispatch($this->eventMock);
70
71
        $this->assertInstanceOf('Zend\View\Model\ViewModel', $actualViewModel);
72
    }
73
74
}