Completed
Push — master ( 984bef...668191 )
by Michael
04:42
created

TextnodeControllerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 116
Duplicated Lines 6.9 %

Importance

Changes 0
Metric Value
wmc 5
dl 8
loc 116
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B testTextnodesAction() 0 40 1
A createTextnodeRepositoryMock() 0 3 1
A createLicenseeRepositoryMock() 8 8 1
A createImportfileRepositoryMock() 0 8 1
A setUp() 0 10 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
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace AdminBundle\Tests\Controller;
21
22
use AdminBundle\Controller\TextnodeController;
23
use DembeloMain\Document\Importfile;
24
use DembeloMain\Document\Licensee;
25
use DembeloMain\Document\Textnode;
26
use DembeloMain\Model\Repository\ImportfileRepositoryInterface;
27
use DembeloMain\Model\Repository\LicenseeRepositoryInterface;
28
use DembeloMain\Model\Repository\TextNodeRepositoryInterface;
29
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
30
use Symfony\Component\HttpFoundation\Response;
31
32
/**
33
 * Class TextnodeControllerTest
34
 * @package AdminBundle\Tests\Controller
35
 */
36
class TextnodeControllerTest extends WebTestCase
37
{
38
    /**
39
     * @var TextnodeController
40
     */
41
    private $controller;
42
43
    /**
44
     * @var \PHPUnit_Framework_MockObject_MockObject|TextNodeRepositoryInterface
45
     */
46
    private $textnodeRepositoryMock;
47
48
    /**
49
     * @var \PHPUnit_Framework_MockObject_MockObject|ImportfileRepositoryInterface
50
     */
51
    private $importfileRepositoryMock;
52
53
    /**
54
     * @var \PHPUnit_Framework_MockObject_MockObject|LicenseeRepositoryInterface
55
     */
56
    private $licenseeRepositoryMock;
57
58
    /**
59
     * @return void
60
     */
61
    protected function setUp(): void
62
    {
63
        $this->textnodeRepositoryMock = $this->createTextnodeRepositoryMock();
64
        $this->importfileRepositoryMock = $this->createImportfileRepositoryMock();
65
        $this->licenseeRepositoryMock = $this->createLicenseeRepositoryMock();
66
67
        $this->controller = new TextnodeController(
68
            $this->textnodeRepositoryMock,
69
            $this->importfileRepositoryMock,
70
            $this->licenseeRepositoryMock
71
        );
72
    }
73
74
    /**
75
     * tests textnode action
76
     * @return void
77
     */
78
    public function testTextnodesAction(): void
79
    {
80
        $textnode = new Textnode();
81
        $textnode->setId('someId');
82
        $textnode->setCreated(new \DateTime('2017-01-01 12:00:00'));
0 ignored issues
show
Bug introduced by
new DateTime('2017-01-01 12:00:00') of type DateTime is incompatible with the type string expected by parameter $created of DembeloMain\Document\Textnode::setCreated(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $textnode->setCreated(/** @scrutinizer ignore-type */ new \DateTime('2017-01-01 12:00:00'));
Loading history...
83
        $textnode->setStatus(1);
84
        $textnode->setAccess(true);
85
        $textnode->setLicenseeId('someLicenseeId');
86
        $textnode->setArbitraryId('someArbitraryId');
87
        $textnode->setTwineId('someTwineId');
88
        $textnode->setMetadata(['key1' => 'val1', 'key2' => 'val2']);
89
90
        $licensee = new Licensee();
91
        $licensee->setId('someLicenseeId');
92
        $licensee->setName('someLicenseeName');
93
94
        $importfile = new Importfile();
95
        $importfile->setId('someImportfileId');
96
        $importfile->setName('someImportfileName');
97
98
        $this->textnodeRepositoryMock->expects($this->once())
99
            ->method('findAll')
100
            ->willReturn([$textnode]);
101
102
        $this->licenseeRepositoryMock->expects($this->once())
103
            ->method('findAll')
104
            ->willReturn([$licensee]);
105
106
        $this->importfileRepositoryMock->expects($this->once())
107
            ->method('findAll')
108
            ->willReturn([$importfile]);
109
110
        /* @var $response \Symfony\Component\HttpFoundation\Response */
111
        $response = $this->controller->textnodesAction();
112
        $this->assertInstanceOf(Response::class, $response);
113
        $expectedJson = '[{"id":"someId","status":"aktiv","created":"01.01.2017, 12:00:00",';
114
        $expectedJson .= '"access":"ja","licensee":"someLicenseeName","importfile":"unbekannt","beginning":"...",';
115
        $expectedJson .= '"financenode":"ja","arbitraryId":"someArbitraryId","twineId":"someTwineId",';
116
        $expectedJson .= '"metadata":"key1: val1\nkey2: val2\n"}]';
117
        $this->assertJsonStringEqualsJsonString($expectedJson, $response->getContent());
118
    }
119
120
    /**
121
     * @return TextNodeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
122
     */
123
    private function createTextnodeRepositoryMock(): TextNodeRepositoryInterface
124
    {
125
        return $this->createMock(TextNodeRepositoryInterface::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createMock...sitoryInterface::class) returns the type PHPUnit_Framework_MockObject_MockObject which is incompatible with the type-hinted return DembeloMain\Model\Reposi...NodeRepositoryInterface.
Loading history...
126
    }
127
128
    /**
129
     * @return \PHPUnit_Framework_MockObject_MockObject|ImportfileRepositoryInterface
130
     */
131
    private function createImportfileRepositoryMock(): ImportfileRepositoryInterface
132
    {
133
        $repository = $this->getMockBuilder(ImportfileRepositoryInterface::class)
134
            ->disableOriginalConstructor()
135
            ->setMethods(['findAll', 'save', 'find', 'findBy', 'findOneBy', 'getClassName'])
136
            ->getMock();
137
138
        return $repository;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $repository returns the type PHPUnit_Framework_MockObject_MockObject which is incompatible with the type-hinted return DembeloMain\Model\Reposi...fileRepositoryInterface.
Loading history...
139
    }
140
141
    /**
142
     * @return \PHPUnit_Framework_MockObject_MockObject|LicenseeRepositoryInterface
143
     */
144 View Code Duplication
    private function createLicenseeRepositoryMock(): LicenseeRepositoryInterface
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...
145
    {
146
        $repository = $this->getMockBuilder(LicenseeRepositoryInterface::class)
147
            ->disableOriginalConstructor()
148
            ->setMethods(['findAll', 'save', 'find', 'findBy', 'findOneBy', 'getClassName', 'findOneByName'])
149
            ->getMock();
150
151
        return $repository;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $repository returns the type PHPUnit_Framework_MockObject_MockObject which is incompatible with the type-hinted return DembeloMain\Model\Reposi...nseeRepositoryInterface.
Loading history...
152
    }
153
}
154