TextnodeControllerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
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
namespace AdminBundle\Tests\Controller;
20
21
use AdminBundle\Controller\TextnodeController;
22
use DembeloMain\Document\Importfile;
23
use DembeloMain\Document\Licensee;
24
use DembeloMain\Document\Textnode;
25
use DembeloMain\Model\Repository\ImportfileRepositoryInterface;
26
use DembeloMain\Model\Repository\LicenseeRepositoryInterface;
27
use DembeloMain\Model\Repository\TextNodeRepositoryInterface;
28
use PHPUnit\Framework\TestCase;
29
use Symfony\Component\HttpFoundation\Response;
30
31
/**
32
 * Class TextnodeControllerTest
33
 */
34
class TextnodeControllerTest extends TestCase
35
{
36
    /**
37
     * @var TextnodeController
38
     */
39
    private $controller;
40
41
    /**
42
     * @var \PHPUnit_Framework_MockObject_MockObject|TextNodeRepositoryInterface
43
     */
44
    private $textnodeRepositoryMock;
45
46
    /**
47
     * @var \PHPUnit_Framework_MockObject_MockObject|ImportfileRepositoryInterface
48
     */
49
    private $importfileRepositoryMock;
50
51
    /**
52
     * @var \PHPUnit_Framework_MockObject_MockObject|LicenseeRepositoryInterface
53
     */
54
    private $licenseeRepositoryMock;
55
56
    /**
57
     * @return void
58
     */
59
    protected function setUp(): void
60
    {
61
        $this->textnodeRepositoryMock = $this->createTextnodeRepositoryMock();
62
        $this->importfileRepositoryMock = $this->createImportfileRepositoryMock();
63
        $this->licenseeRepositoryMock = $this->createLicenseeRepositoryMock();
64
65
        $this->controller = new TextnodeController(
66
            $this->textnodeRepositoryMock,
67
            $this->importfileRepositoryMock,
68
            $this->licenseeRepositoryMock
69
        );
70
    }
71
72
    /**
73
     * tests textnode action
74
     * @return void
75
     */
76
    public function testTextnodesAction(): void
77
    {
78
        $textnode = new Textnode();
79
        $textnode->setId('someId');
80
        $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

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