RequestTraitTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 85
Duplicated Lines 21.18 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 18
loc 85
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildMock() 0 14 1
A setUpBeforeClass() 18 18 1
A tearDownAfterClass() 0 6 1
A testAccessors() 0 24 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace FwlibTest\Web;
3
4
use Fwlib\Util\Common\HttpUtil;
5
use Fwlib\Util\UtilContainer;
6
use Fwlib\Web\RequestTrait;
7
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
8
use PHPUnit_Framework_MockObject_MockObject as MockObject;
9
10
/**
11
 * @copyright   Copyright 2015 Fwolf
12
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
13
 */
14
class RequestTraitTest extends PHPUnitTestCase
15
{
16
    /**
17
     * @var string
18
     */
19
    protected static $getGet = '';
20
21
    /**
22
     * @var HttpUtil
23
     */
24
    protected static $httpUtilBackup = null;
25
26
27
    /**
28
     * @return MockObject | RequestTrait
29
     */
30
    protected function buildMock()
31
    {
32
        $mock = $this->getMockBuilder(RequestTrait::class)
33
            ->disableOriginalConstructor()
34
            ->getMockForTrait();
35
36
        /** @noinspection PhpUndefinedFieldInspection */
37
        {
38
            $mock->actionParameter = 'a';
0 ignored issues
show
Bug introduced by
Accessing actionParameter 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...
39
            $mock->moduleParameter = 'm';
0 ignored issues
show
Bug introduced by
Accessing moduleParameter 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...
40
        }
41
42
        return $mock;
43
    }
44
45
46 View Code Duplication
    public static function setUpBeforeClass()
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...
47
    {
48
        $utilContainer = UtilContainer::getInstance();
49
        self::$httpUtilBackup = $utilContainer->getHttp();
50
51
        $testCase = new self;
52
        $httpUtil = $testCase->getMock(
53
            HttpUtil::class,
54
            ['getGet']
55
        );
56
        $httpUtil->expects($testCase->any())
57
            ->method('getGet')
58
            ->willReturnCallback(function () {
59
                return RequestTraitTest::$getGet;
60
            });
61
62
        $utilContainer->register('Http', $httpUtil);
63
    }
64
65
66
    public static function tearDownAfterClass()
67
    {
68
        $utilContainer = UtilContainer::getInstance();
69
70
        $utilContainer->register('Http', self::$httpUtilBackup);
71
    }
72
73
74
    public function testAccessors()
75
    {
76
        $request = $this->buildMock();
77
78
        self::$getGet = 'foo';
79
        $this->assertEquals('foo', $request->getAction());
80
81
        $request->setAction('foo1');
82
        $this->assertEquals('foo1', $request->getAction());
83
84
        self::$getGet = 'bar';
85
        $this->assertEquals('bar', $request->getModule());
86
87
        $request->setModule('bar1');
88
        $this->assertEquals('bar1', $request->getModule());
89
90
91
        // Action and module parameter
92
        $request->setActionParameter('aa');
93
        $this->assertEquals('aa', $request->getActionParameter());
94
95
        $request->setModuleParameter('mm');
96
        $this->assertEquals('mm', $request->getModuleParameter());
97
    }
98
}
99