CreateControllerTest::testOnDispatchFail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace Sebaks\CrudTest\Controller\Admin;
4
5
use Sebaks\Crud\Controller\CreateController;
6
use Sebaks\Crud\View\Model\CreateViewModel;
7
8
class CreateControllerTest extends \PHPUnit_Framework_TestCase
9
{
10
    private $controller;
11
    private $data;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
12
    private $creatorMock;
13
    private $view;
14
    private $eventMock;
15
16
    public function setUp()
17
    {
18
        $this->data = [
19
            'foo' => 'bar'
20
        ];
21
22
        $this->creatorMock = $this->getMockBuilder('T4webDomainInterface\Service\CreatorInterface')
23
            ->disableOriginalConstructor()
24
            ->getMock();
25
26
        $this->eventMock = $this->getMock('Zend\Mvc\MvcEvent');
27
28
        $this->view = new CreateViewModel();
29
30
        $this->controller = new CreateController(
31
            $this->data,
32
            $this->creatorMock,
33
            $this->view
34
        );
35
    }
36
37
    public function testOnDispatchSuccess()
38
    {
39
        $entityMock = $this->getMock('T4webDomainInterface\EntityInterface');
40
41
        $this->creatorMock->method('create')
42
            ->with($this->data)
43
            ->willReturn($entityMock);
44
45
        $this->eventMock->method('setResult')
46
            ->with($this->view);
47
48
        $actualViewModel = $this->controller->onDispatch($this->eventMock);
49
50
        $this->assertSame($this->view, $actualViewModel);
51
        $this->assertSame($entityMock, $actualViewModel->getEntity());
0 ignored issues
show
Bug introduced by
The method getEntity does only exist in Sebaks\Crud\View\Model\CreateViewModelInterface, 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...
52
    }
53
54
    public function testOnDispatchFail()
55
    {
56
        $this->creatorMock->method('create')
57
            ->with($this->data)
58
            ->willReturn(null);
59
60
        $this->creatorMock->method('getErrors')
61
            ->willReturn(['errorField' => 'error message']);
62
63
        $this->eventMock->method('setResult')
64
            ->with($this->view);
65
66
        $actualViewModel = $this->controller->onDispatch($this->eventMock);
67
68
        $this->assertSame($this->view, $actualViewModel);
69
        $this->assertNull($actualViewModel->getEntity());
0 ignored issues
show
Bug introduced by
The method getEntity does only exist in Sebaks\Crud\View\Model\CreateViewModelInterface, 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...
70
        $this->assertEquals(['errorField' => 'error message'], $actualViewModel->getErrors());
0 ignored issues
show
Bug introduced by
The method getErrors does only exist in Sebaks\Crud\View\Model\CreateViewModelInterface, 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...
71
        $this->assertSame($this->data, $actualViewModel->getInputData());
0 ignored issues
show
Bug introduced by
The method getInputData does only exist in Sebaks\Crud\View\Model\CreateViewModelInterface, 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...
72
    }
73
74
}