Passed
Push — master ( c71cfc...3755d0 )
by Michael
01:44
created

UserControllerTest::createRequestMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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
20
namespace AdminBundle\Tests\Controller;
21
22
use AdminBundle\Controller\UserController;
23
use DembeloMain\Document\User;
24
use DembeloMain\Model\Repository\UserRepositoryInterface;
25
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
26
use Symfony\Component\HttpFoundation\Response;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\ParameterBag;
29
30
/**
31
 * Class UserControllerTest
32
 * @package AdminBundle\Tests\Controller
33
 */
34
class UserControllerTest extends WebTestCase
35
{
36
    /**
37
     * @var UserController
38
     */
39
    private $controller;
40
41
    /**
42
     * @var \PHPUnit_Framework_MockObject_MockObject|UserRepositoryInterface
43
     */
44
    private $userRepositoryMock;
45
46
    /**
47
     * @var \PHPUnit_Framework_MockObject_MockObject|\Swift_Mailer
48
     */
49
    private $mailerMock;
50
51
    /**
52
     * @return void
53
     */
54
    protected function setUp(): void
55
    {
56
        $this->userRepositoryMock = $this->createUserRepositoryMock();
57
        $this->mailerMock = $this->createMailerMock();
58
59
        $this->controller = new UserController(
60
            $this->userRepositoryMock,
61
            $this->mailerMock
62
        );
63
    }
64
65
    /**
66
     * tests controller's userAction with no users in db
67
     * @return void
68
     */
69
    public function testUserAction(): void
70
    {
71
        $requestMock = $this->createRequestMock();
72
        $queryMock = $this->getMockBuilder('foobar')->setMethods(array('execute', 'getQuery'))->getMock();
73
74
        $queryMock->expects($this->once())
75
            ->method('getQuery')
76
            ->will($this->returnSelf());
77
78
        $queryMock->expects($this->once())
79
            ->method('execute')
80
            ->will($this->returnValue(array()));
81
82
        $this->userRepositoryMock->expects($this->once())
83
            ->method('createQueryBuilder')
84
            ->will($this->returnValue($queryMock));
85
86
        /* @var $response \Symfony\Component\HttpFoundation\Response */
87
        $response = $this->controller->usersAction($requestMock);
88
        $this->assertInstanceOf(Response::class, $response);
89
        $this->assertJsonStringEqualsJsonString('[]', $response->getContent());
90
        $this->assertEquals('200', $response->getStatusCode());
91
    }
92
93
    /**
94
     * tests controller's userAction with two users in db
95
     * @return void
96
     */
97
    public function testUserActionWithUsers(): void
98
    {
99
        $requestMock = $this->createRequestMock();
100
        $queryMock = $this->getMockBuilder('foobar')->setMethods(['execute', 'getQuery'])->getMock();
101
102
        $queryMock->expects($this->once())
103
            ->method('getQuery')
104
            ->will($this->returnSelf());
105
106
        $this->userRepositoryMock->expects($this->once())
107
            ->method('createQueryBuilder')
108
            ->will($this->returnValue($queryMock));
109
110
        $user1 = new User();
111
        $user1->setEmail('email1');
112
        $user1->setId('id1');
113
        $user1->setRoles('ROLE_ADMIN');
114
        $user1->setLicenseeId('lic1');
115
        $user2 = new User();
116
        $user2->setEmail('email2');
117
        $user2->setId('id2');
118
        $user2->setRoles('ROLE_USER');
119
        $user2->setLicenseeId('lic2');
120
121
        $userArray = array(
122
            $user1,
123
            $user2,
124
        );
125
126
        $queryMock->expects($this->once())
127
            ->method('execute')
128
            ->will($this->returnValue($userArray));
129
130
        /* @var $response \Symfony\Component\HttpFoundation\Response */
131
        $response = $this->controller->usersAction($requestMock);
132
        $this->assertInstanceOf(Response::class, $response);
133
        $this->assertJsonStringEqualsJsonString('[{"id":"id1","gender":null,"email":"email1","roles":"ROLE_ADMIN","licenseeId":"lic1","status":null,"source":null,"reason":null,"created":"'.date('Y-m-d H:i:s', 0).'","updated":"'.date('Y-m-d H:i:s', 0).'"},{"id":"id2","email":"email2","roles":"ROLE_USER","licenseeId":"lic2","status":null,"source":null,"reason":null,"gender":null,"created":"'.date('Y-m-d H:i:s', 0).'","updated":"'.date('Y-m-d H:i:s', 0).'"}]', $response->getContent());
134
        $this->assertEquals('200', $response->getStatusCode());
135
    }
136
137
    /**
138
     * @return UserRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
139
     */
140 View Code Duplication
    private function createUserRepositoryMock(): UserRepositoryInterface
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...
141
    {
142
        return $this->getMockBuilder(UserRepositoryInterface::class)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMockBui...ClassName'))->getMock() returns the type PHPUnit_Framework_MockObject_MockObject which is incompatible with the type-hinted return DembeloMain\Model\Reposi...UserRepositoryInterface.
Loading history...
143
            ->disableOriginalConstructor()
144
            ->setMethods(['createQueryBuilder', 'find', 'findByEmail', 'findAll', 'save', 'findBy', 'findOneBy', 'getClassName'])
145
            ->getMock();
146
    }
147
148
    /**
149
     * @return \Swift_Mailer|\PHPUnit_Framework_MockObject_MockObject
150
     */
151
    private function createMailerMock(): \Swift_Mailer
152
    {
153
        return $this->createMock(\Swift_Mailer::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createMock(Swift_Mailer::class) returns the type PHPUnit_Framework_MockObject_MockObject which is incompatible with the type-hinted return Swift_Mailer.
Loading history...
154
    }
155
156
    /**
157
     * @return \PHPUnit_Framework_MockObject_MockObject|Request
158
     */
159
    private function createRequestMock()
160
    {
161
        $requestMock = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock();
162
        $postMock = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->getMock();
163
        $postArray = [];
164
        $postMock->expects($this->once())
165
            ->method('get')
166
            ->will($this->returnValue($postArray));
167
        $requestMock->query = $postMock;
0 ignored issues
show
Bug introduced by
Accessing query on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
168
169
        return $requestMock;
170
    }
171
}
172