ViewTraitTest::testGetOutput()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Web;
3
4
use Fwlib\Web\ViewTrait;
5
use FwlibTest\Aide\ObjectMockBuilder\FwlibWebRequestTrait;
6
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
7
use PHPUnit_Framework_MockObject_MockObject as MockObject;
8
9
/**
10
 * @copyright   Copyright 2013-2015 Fwolf
11
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
12
 */
13
class ViewTraitTest extends PHPUnitTestCase
14
{
15
    use FwlibWebRequestTrait;
16
17
18
    /**
19
     * @return MockObject | ViewTrait
20
     */
21
    protected function buildMock()
22
    {
23
        $mock = $this->getMockBuilder(ViewTrait::class)
24
            ->setMethods(['getOutputBody'])
25
            ->getMockForTrait();
26
27
        $mock->expects($this->any())
28
            ->method('getOutputBody')
29
            ->will($this->returnValue('body for test action'));
30
31
        // Simulate property
32
        /** @noinspection PhpUndefinedFieldInspection */
33
        $mock->outputParts = [];
0 ignored issues
show
Bug introduced by
Accessing outputParts on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
34
35
        return $mock;
36
    }
37
38
39
    public function testAccessors()
40
    {
41
        $view = $this->buildMock();
42
43
        $this->reflectionCall($view, 'setTitle', ['Title']);
44
        $this->assertEquals('Title', $this->reflectionGet($view, 'title'));
45
    }
46
47
48
    public function testGetOutput()
49
    {
50
        $view = $this->buildMock();
51
52
        $view->outputParts = [];
0 ignored issues
show
Bug introduced by
Accessing outputParts on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
53
        $this->assertEquals(
54
            '',
55
            $view->getOutput()
56
        );
57
58
        $view->outputParts = [0 => 'body',];
0 ignored issues
show
Bug introduced by
Accessing outputParts on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
59
        $this->assertEquals(
60
            'body for test action',
61
            $view->getOutput()
62
        );
63
    }
64
65
66
    /**
67
     * @expectedException \Fwlib\Web\Exception\InvalidOutputPartException
68
     * @expectedExceptionMessage View method for part
69
     */
70
    public function testGetOutputWithInvalidPart()
71
    {
72
        $view = $this->buildMock();
73
74
        $view->outputParts = ['NotExist'];
0 ignored issues
show
Bug introduced by
Accessing outputParts on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
75
        $view->getOutput();
76
    }
77
}
78