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

EventlogServiceTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 11
rs 10
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\Eventlog;
26
27
use SP\Core\Exceptions\ConstraintException;
28
use SP\DataModel\EventlogData;
29
use SP\DataModel\ItemSearchData;
30
use SP\Services\EventLog\EventlogService;
31
use SP\Storage\Database\DatabaseConnectionData;
32
use SP\Tests\DatabaseTestCase;
33
use function SP\Tests\setupContext;
34
35
/**
36
 * Class EventlogServiceTest
37
 *
38
 * @package SP\Tests\Services\Eventlog
39
 */
40
class EventlogServiceTest extends DatabaseTestCase
41
{
42
    /**
43
     * @var EventlogService
44
     */
45
    private static $service;
46
47
    /**
48
     * @throws \DI\NotFoundException
49
     * @throws \SP\Core\Context\ContextException
50
     * @throws \DI\DependencyException
51
     */
52
    public static function setUpBeforeClass()
53
    {
54
        $dic = setupContext();
55
56
        self::$dataset = 'syspass_eventlog.xml';
57
58
        // Datos de conexión a la BBDD
59
        self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class);
60
61
        // Inicializar el servicio
62
        self::$service = $dic->get(EventlogService::class);
63
    }
64
65
    /**
66
     * @throws \SP\Core\Exceptions\ConstraintException
67
     * @throws \SP\Core\Exceptions\QueryException
68
     * @throws \SP\Core\Exceptions\SPException
69
     */
70
    public function testClear()
71
    {
72
        self::$service->clear();
73
74
        $this->assertEquals(0, $this->conn->getRowCount('EventLog'));
75
    }
76
77
    /**
78
     * @throws \SP\Core\Exceptions\ConstraintException
79
     * @throws \SP\Core\Exceptions\QueryException
80
     */
81
    public function testSearch()
82
    {
83
        $itemSearchData = new ItemSearchData();
84
        $itemSearchData->setLimitCount(10);
85
        $itemSearchData->setSeachString('login.auth.database');
86
87
        $result = self::$service->search($itemSearchData);
88
        $data = $result->getDataAsArray();
89
90
        $this->assertEquals(4, $result->getNumRows());
91
        $this->assertCount(4, $data);
92
        $this->assertInstanceOf(\stdClass::class, $data[0]);
93
        $this->assertEquals('login.auth.database', $data[0]->action);
94
95
        $itemSearchData->setSeachString('login.auth.');
96
97
        $result = self::$service->search($itemSearchData);
98
        $data = $result->getDataAsArray();
99
100
        $this->assertEquals(4, $result->getNumRows());
101
        $this->assertCount(4, $data);
102
        $this->assertInstanceOf(\stdClass::class, $data[0]);
103
104
        $itemSearchData->setSeachString('Tiempo inactivo : 0 min.');
105
106
        $result = self::$service->search($itemSearchData);
107
        $data = $result->getDataAsArray();
108
109
        $this->assertEquals(1, $result->getNumRows());
110
        $this->assertCount(1, $data);
111
        $this->assertInstanceOf(\stdClass::class, $data[0]);
112
113
        $itemSearchData->setSeachString('prueba');
114
115
        $result = self::$service->search($itemSearchData);
116
117
        $this->assertCount(0, $result->getDataAsArray());
118
        $this->assertEquals(0, $result->getNumRows());
119
    }
120
121
    /**
122
     * @throws ConstraintException
123
     * @throws \SP\Core\Exceptions\QueryException
124
     */
125
    public function testCreate()
126
    {
127
        $eventlogData = new EventlogData();
128
        $eventlogData->setAction('test');
129
        $eventlogData->setLevel('INFO');
130
        $eventlogData->setUserId(1);
131
        $eventlogData->setLogin('Admin');
132
        $eventlogData->setIpAddress('127.0.0.1');
133
        $eventlogData->setDescription('Prueba');
134
135
        $countBefore = $this->conn->getRowCount('EventLog');
136
137
        self::$service->create($eventlogData);
138
139
        $countAfter = $this->conn->getRowCount('EventLog');
140
141
        $this->assertEquals($countBefore + 1, $countAfter);
142
143
        $this->expectException(ConstraintException::class);
144
145
        self::$service->create(new EventlogData());
146
    }
147
}
148