Completed
Push — ezp26297-rest_embedding_http_c... ( 123323 )
by
unknown
80:28 queued 54:58
created

CachedValueUnwrapperControllerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 68.09 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testAnyMethodWithCachedValue() 16 16 1
A testAnyMethodWithoutCachedValue() 16 16 1
A getRandomMethod() 0 7 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
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
7
namespace eZ\Publish\Core\REST\Server\Tests\HttpCache\Controller;
8
9
use eZ\Publish\Core\REST\Server\HttpCache\Controller\CachedValueUnwrapperController;
10
use eZ\Publish\Core\REST\Server\Values\CachedValue;
11
use PHPUnit_Framework_TestCase;
12
use stdClass;
13
14
class CachedValueUnwrapperControllerTest extends PHPUnit_Framework_TestCase
15
{
16 View Code Duplication
    public function testAnyMethodWithCachedValue()
17
    {
18
        $method = $this->getRandomMethod();
19
20
        $innerControllerMock = $this->getMockBuilder('stdClass')->setMethods([$method])->getMock();
21
        $controller = new CachedValueUnwrapperController($innerControllerMock);
22
23
        $value = new stdClass;
24
25
        $innerControllerMock
26
            ->expects($this->once())
27
            ->method($method)
28
            ->will($this->returnValue(new CachedValue($value)));
29
30
        $this->assertSame($value, $controller->$method());
31
    }
32
33 View Code Duplication
    public function testAnyMethodWithoutCachedValue()
34
    {
35
        $method = $this->getRandomMethod();
36
37
        $innerControllerMock = $this->getMockBuilder('stdClass')->setMethods([$method])->getMock();
38
        $controller = new CachedValueUnwrapperController($innerControllerMock);
39
40
        $value = new stdClass;
41
42
        $innerControllerMock
43
            ->expects($this->once())
44
            ->method($method)
45
            ->will($this->returnValue($value));
46
47
        $this->assertSame($value, $controller->$method());
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    protected function getRandomMethod(): string
54
    {
55
        $actions = ['create', 'retrieve', 'update', 'delete'];
56
        $subjects = ['content', 'section', 'location'];
57
58
        return $actions[array_rand($actions)] . ucfirst($subjects[array_rand($subjects)]);
59
    }
60
}
61