Passed
Push — devel-3.0 ( 09ea81...3c7891 )
by Rubén
03:32
created

ClientServiceTest::testCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 22
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Tests\Services\Client;
26
27
use SP\Core\Context\ContextInterface;
28
use SP\Core\Exceptions\ConstraintException;
29
use SP\DataModel\ClientData;
30
use SP\DataModel\ItemSearchData;
31
use SP\Repositories\DuplicatedItemException;
32
use SP\Repositories\NoSuchItemException;
33
use SP\Services\Client\ClientService;
34
use SP\Services\ServiceException;
35
use SP\Services\User\UserLoginResponse;
36
use SP\Storage\Database\DatabaseConnectionData;
37
use SP\Tests\DatabaseTestCase;
38
use function SP\Tests\setupContext;
39
40
/**
41
 * Class ClientServiceTest
42
 *
43
 * @package SP\Tests\Services\Client
44
 */
45
class ClientServiceTest extends DatabaseTestCase
46
{
47
    /**
48
     * @var \Closure
49
     */
50
    private static $setupUser;
51
    /**
52
     * @var ClientService
53
     */
54
    private static $service;
55
56
    /**
57
     * @throws \DI\NotFoundException
58
     * @throws \SP\Core\Context\ContextException
59
     * @throws \DI\DependencyException
60
     */
61
    public static function setUpBeforeClass()
62
    {
63
        $dic = setupContext();
64
65
        self::$dataset = 'syspass.xml';
66
67
        // Datos de conexión a la BBDD
68
        self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class);
69
70
        // Inicializar el servicio
71
        self::$service = $dic->get(ClientService::class);
72
73
        self::$setupUser = function (UserLoginResponse $response) use ($dic) {
74
            $response->setLastUpdate(time());
75
76
            $dic->get(ContextInterface::class)->setUserData($response);
77
        };
78
    }
79
80
    /**
81
     * @throws \SP\Core\Exceptions\ConstraintException
82
     * @throws \SP\Core\Exceptions\QueryException
83
     */
84
    public function testSearch()
85
    {
86
        $itemSearchData = new ItemSearchData();
87
        $itemSearchData->setLimitCount(10);
88
        $itemSearchData->setSeachString('google');
89
90
        $result = self::$service->search($itemSearchData);
91
        $data = $result->getDataAsArray();
92
93
        $this->assertEquals(1, $result->getNumRows());
94
        $this->assertCount(1, $data);
95
        $this->assertInstanceOf(ClientData::class, $data[0]);
96
        $this->assertEquals(1, $data[0]->id);
97
        $this->assertEquals('Google Inc.', $data[0]->description);
98
99
        $itemSearchData = new ItemSearchData();
100
        $itemSearchData->setLimitCount(10);
101
        $itemSearchData->setSeachString('prueba');
102
103
        $result = self::$service->search($itemSearchData);
104
105
        $this->assertEquals(0, $result->getNumRows());
106
        $this->assertCount(0, $result->getDataAsArray());
107
    }
108
109
    /**
110
     * @throws \SP\Core\Exceptions\ConstraintException
111
     * @throws \SP\Core\Exceptions\QueryException
112
     */
113
    public function testGetAllBasic()
114
    {
115
        $count = $this->conn->getRowCount('Client');
116
117
        $results = self::$service->getAllBasic();
118
119
        $this->assertCount($count, $results);
120
121
        $this->assertInstanceOf(ClientData::class, $results[0]);
122
        $this->assertEquals('Apple', $results[0]->getName());
123
124
        $this->assertInstanceOf(ClientData::class, $results[1]);
125
        $this->assertEquals('Google', $results[1]->getName());
126
127
        $this->assertInstanceOf(ClientData::class, $results[2]);
128
        $this->assertEquals('Microsoft', $results[2]->getName());
129
    }
130
131
    /**
132
     * @throws ConstraintException
133
     * @throws NoSuchItemException
134
     * @throws \SP\Core\Exceptions\QueryException
135
     */
136
    public function testGetById()
137
    {
138
        $client = self::$service->getById(1);
139
140
        $this->assertEquals('Google', $client->getName());
141
        $this->assertEquals('Google Inc.', $client->getDescription());
142
143
        $client = self::$service->getById(2);
144
145
        $this->assertEquals('Apple', $client->getName());
146
        $this->assertEquals('Apple Inc.', $client->getDescription());
147
148
        $this->expectException(NoSuchItemException::class);
149
150
        self::$service->getById(10);
151
    }
152
153
    /**
154
     * @throws ConstraintException
155
     * @throws \SP\Core\Exceptions\QueryException
156
     */
157
    public function testGetAllForUserAdmin()
158
    {
159
        $this->assertCount(3, self::$service->getAllForUser());
160
    }
161
162
    /**
163
     * @throws ConstraintException
164
     * @throws \SP\Core\Exceptions\QueryException
165
     */
166
    public function testGetAllForUser()
167
    {
168
        $userData = new UserLoginResponse();
169
        $userData->setId(4);
170
171
        self::$setupUser->call($this, $userData);
172
173
        $this->assertCount(1, self::$service->getAllForUser());
174
    }
175
176
    /**
177
     * @throws \SP\Core\Exceptions\ConstraintException
178
     * @throws \SP\Core\Exceptions\QueryException
179
     * @throws \SP\Core\Exceptions\SPException
180
     * @throws \SP\Repositories\DuplicatedItemException
181
     */
182
    public function testCreate()
183
    {
184
        $data = new ClientData();
185
        $data->name = 'Cliente prueba';
186
        $data->description = 'Descripción prueba';
187
        $data->isGlobal = 1;
188
189
        $id = self::$service->create($data);
190
191
        // Comprobar que el Id devuelto corresponde con el cliente creado
192
        $result = self::$service->getById($id);
193
194
        $this->assertEquals($data->name, $result->getName());
195
        $this->assertEquals($data->isGlobal, $result->getIsGlobal());
196
197
        $countAfter = $this->conn->getRowCount('Client');
198
199
        $this->assertEquals(4, $countAfter);
200
201
        $this->expectException(DuplicatedItemException::class);
202
203
        self::$service->create($data);
204
    }
205
206
    /**
207
     * @throws \SP\Core\Exceptions\ConstraintException
208
     * @throws \SP\Core\Exceptions\QueryException
209
     * @throws \SP\Services\ServiceException
210
     */
211
    public function testDeleteByIdBatch()
212
    {
213
        $countBefore = $this->conn->getRowCount('Client');
214
215
        self::$service->deleteByIdBatch([3]);
216
217
        $countAfter = $this->conn->getRowCount('Client');
218
219
        $this->assertEquals($countBefore - 1, $countAfter);
220
221
        // Comprobar que se produce una excepción al tratar de eliminar clientes usados
222
        $this->expectException(ConstraintException::class);
223
224
        self::$service->deleteByIdBatch([1, 2]);
225
226
        $this->expectException(ServiceException::class);
227
228
        self::$service->deleteByIdBatch([10]);
229
    }
230
231
    /**
232
     * @throws ConstraintException
233
     * @throws NoSuchItemException
234
     * @throws \SP\Core\Exceptions\QueryException
235
     */
236
    public function testGetByName()
237
    {
238
        $data = self::$service->getByName('Google');
239
240
        $this->assertEquals(1, $data->getId());
241
        $this->assertEquals('Google Inc.', $data->getDescription());
242
243
        $data = self::$service->getByName('Apple');
244
245
        $this->assertEquals(2, $data->getId());
246
        $this->assertEquals('Apple Inc.', $data->getDescription());
247
248
        // Se comprueba que el hash generado es el mismo en para el nombre 'Web'
249
        $data = self::$service->getByName(' google. ');
250
251
        $this->assertEquals(1, $data->getId());
252
        $this->assertEquals('Google Inc.', $data->getDescription());
253
254
        $this->expectException(NoSuchItemException::class);
255
256
        self::$service->getByName('Amazon');
257
    }
258
259
    /**
260
     * @throws \SP\Core\Exceptions\SPException
261
     */
262
    public function testDelete()
263
    {
264
        $countBefore = $this->conn->getRowCount('Client');
265
266
        self::$service->delete(3);
267
268
        $countAfter = $this->conn->getRowCount('Client');
269
270
        $this->assertEquals($countBefore - 1, $countAfter);
271
272
        // Comprobar que se produce una excepción al tratar de eliminar clientes usados
273
        $this->expectException(ConstraintException::class);
274
275
        self::$service->delete(2);
276
277
        $this->expectException(NoSuchItemException::class);
278
279
        self::$service->delete(10);
280
    }
281
282
    /**
283
     * @throws ConstraintException
284
     * @throws \SP\Core\Exceptions\QueryException
285
     * @throws \SP\Core\Exceptions\SPException
286
     */
287
    public function testUpdate()
288
    {
289
        $data = new ClientData();
290
        $data->id = 1;
291
        $data->name = 'Cliente prueba';
292
        $data->description = 'Descripción cliente prueba';
293
294
        self::$service->update($data);
295
296
        $result = self::$service->getById(1);
297
298
        $this->assertEquals($data->name, $result->getName());
299
        $this->assertEquals($data->description, $result->getDescription());
300
301
        // Comprobar la a actualización con un nombre duplicado comprobando su hash
302
        $data = new ClientData();
303
        $data->id = 1;
304
        $data->name = ' apple.';
305
306
        $this->expectException(DuplicatedItemException::class);
307
308
        self::$service->update($data);
309
    }
310
}
311