-
Notifications
You must be signed in to change notification settings - Fork 19
/
StorageTest.php
133 lines (114 loc) · 4.22 KB
/
StorageTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php declare(strict_types=1);
/**
* @maintainer Timur Shagiakhmetov <timur.shagiakhmetov@corp.badoo.com>
*/
namespace unit\Badoo\LiveProfilerUI\DB\Adapters;
class StorageTest extends \unit\Badoo\BaseTestCase
{
/** @var \Badoo\LiveProfilerUI\DB\Storage */
protected $Storage;
protected function setUp(): void
{
parent::setUp();
$this->Storage = new \Badoo\LiveProfilerUI\DB\Storage('sqlite:///:memory:');
}
public function providerGetSelectQueryBuilder() : array
{
return [
[
'fields' => ['all'],
'params' => [],
'expected' => 'SELECT * FROM details'
],
[
'fields' => ['a', ['field' => 'b']],
'params' => [],
'expected' => 'SELECT a,b b FROM details'
],
[
'fields' => ['all'],
'params' => ['filter' => [['a', 1]]],
'expected' => 'SELECT * FROM details WHERE a = :dcValue1'
],
[
'fields' => [['field' => 'a', 'function' => 'sum', 'alias' => 'sum_a']],
'params' => [
'group' => ['a'],
'order' => ['b' => 'desc'],
'having' => [['sum_a', 1, '>'], ['sum_a', 0]],
'limit' => 10
],
'expected' => 'SELECT sum(a) sum_a FROM details GROUP BY a HAVING (sum_a > :dcValue1) AND (sum_a = :dcValue2) ORDER BY b desc LIMIT 10'
],
];
}
/**
* @dataProvider providerGetSelectQueryBuilder
* @param $fields
* @param $params
* @param $expected
* @throws \ReflectionException
*/
public function testGetSelectQueryBuilder(array $fields, array $params, string $expected)
{
$table = 'details';
$QueryBuilder = $this->invokeMethod($this->Storage, 'getSelectQueryBuilder', [$table, $fields, $params]);
self::assertEquals($expected, $QueryBuilder->getSql());
}
public function testInsertError()
{
$this->expectException(\Badoo\LiveProfilerUI\Exceptions\DatabaseException::class);
$this->expectExceptionMessage('Can\'t insert into details');
$this->Storage->insert('details', ['a' => 1]);
}
public function testInsertManyError()
{
$this->expectException(\Badoo\LiveProfilerUI\Exceptions\DatabaseException::class);
$this->expectExceptionMessage('Can\'t insert into details');
$this->Storage->insertMany('details', [['a' => 1]]);
}
public function testInsertManyEmptyData()
{
$this->expectException(\Badoo\LiveProfilerUI\Exceptions\InvalidFieldValueException::class);
$this->expectExceptionMessage('Can\'t insert empty data');
$this->Storage->insertMany('details', []);
}
public function testInsertManyEmptyOneOfData()
{
$this->expectException(\Badoo\LiveProfilerUI\Exceptions\InvalidFieldValueException::class);
$this->expectExceptionMessage('Can\'t insert empty data');
$this->Storage->insertMany('details', [[]]);
}
/**
* @throws \Doctrine\DBAL\DBALException
*/
public function testMultiQuery()
{
$sql = "SELECT 1;\n\nSELECT 2;";
$result = $this->Storage->multiQuery($sql);
self::assertTrue($result);
}
public function testDeleteEmptyData()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can\'t delete without any conditions');
$this->Storage->delete('details', []);
}
public function testUpdateEmptyParams()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can\'t update without any conditions');
$this->Storage->update('details', ['a' => 1], []);
}
public function testUpdateEmptyFields()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can\'t update without any fields');
$this->Storage->update('details', [], ['a' => 1]);
}
public function testQueryError()
{
$result = $this->Storage->query('SELECT * FROM t;');
self::assertFalse($result);
}
}