Completed
Push — master ( 026476...5a8e15 )
by Gerrit
30:22
created

GenericTemplateRenderControllerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 111
Duplicated Lines 21.62 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 24
loc 111
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A shouldRenderTemplate() 0 32 1
A shouldCheckIfAccessIsGranted() 24 24 1
A shouldRejectConstructorCalledAgain() 0 12 1
A shouldThrowExceptionIfTemplatePathIsMissing() 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 (C) 2017  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\SymfonyGenerics\Tests\Unit\Controllers;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Controllers\GenericTemplateRenderController;
15
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
16
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
20
use InvalidArgumentException;
21
22
final class GenericTemplateRenderControllerTest extends TestCase
23
{
24
25
    /**
26
     * @var ControllerHelperInterface
27
     */
28
    private $controllerHelper;
29
30
    /**
31
     * @var ArgumentCompilerInterface
32
     */
33
    private $argumentCompiler;
34
35
    public function setUp()
36
    {
37
        $this->controllerHelper = $this->createMock(ControllerHelperInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...HelperInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...trollerHelperInterface> of property $controllerHelper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        $this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...mpilerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...umentCompilerInterface> of property $argumentCompiler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function shouldRenderTemplate()
45
    {
46
        /** @var Request $request */
47
        $request = $this->createMock(Request::class);
48
49
        /** @var Response $expectedResponse */
50
        $expectedResponse = $this->createMock(Response::class);
51
52
        $this->argumentCompiler->expects($this->once())->method('buildArguments')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...umentCompilerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            $this->equalTo(['foo' => 'bar']),
54
            $this->identicalTo($request)
55
        )->willReturn([
56
            'bar' => 'baz'
57
        ]);
58
59
        $this->controllerHelper->expects($this->once())->method('renderTemplate')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
            $this->equalTo("@foo/bar/baz.html"),
61
            $this->equalTo(['bar' => 'baz'])
62
        )->willReturn($expectedResponse);
63
64
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
65
            'template' => "@foo/bar/baz.html",
66
            'arguments' => [
67
                'foo' => 'bar'
68
            ]
69
        ]);
70
71
        /** @var Response $actualResponse */
72
        $actualResponse = $controller->renderTemplate($request);
73
74
        $this->assertSame($expectedResponse, $actualResponse);
75
    }
76
77
    /**
78
     * @test
79
     */
80 View Code Duplication
    public function shouldCheckIfAccessIsGranted()
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...
81
    {
82
        $this->expectException(AccessDeniedException::class);
83
84
        /** @var Request $request */
85
        $request = $this->createMock(Request::class);
86
87
        $this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
            $this->equalTo('some-attribute'),
89
            $this->equalTo($request)
90
        )->will($this->returnCallback(
91
            function () {
92
                throw new AccessDeniedException('Lorem ipsum!');
93
            }
94
        ));
95
96
        /** @var mixed $controller */
97
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
98
            'template' => "@foo/bar/baz.html",
99
            'authorization-attribute' => 'some-attribute',
100
        ]);
101
102
        $controller->renderTemplate($request);
103
    }
104
105
    /**
106
     * @test
107
     */
108
    public function shouldRejectConstructorCalledAgain()
109
    {
110
        $this->expectException(InvalidArgumentException::class);
111
112
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
113
            'template' => "@foo/bar/baz.html",
114
        ]);
115
116
        $controller->__construct($this->controllerHelper, $this->argumentCompiler, [
117
            'template' => "@foo/bar/baz.html",
118
        ]);
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function shouldThrowExceptionIfTemplatePathIsMissing()
125
    {
126
        $this->expectException(InvalidArgumentException::class);
127
128
        $controller = new GenericTemplateRenderController($this->controllerHelper, $this->argumentCompiler, [
0 ignored issues
show
Unused Code introduced by
$controller is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
129
        ]);
130
    }
131
132
}
133