Skip to content

Commit 5c8b1e9

Browse files
committed
fix(ExpressionBuilderTest): Fix test and improve typing
Signed-off-by: Carl Schwan <carl.schwan@nextcloud.com>
1 parent 81a301f commit 5c8b1e9

File tree

1 file changed

+44
-127
lines changed

1 file changed

+44
-127
lines changed

tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php

Lines changed: 44 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -9,45 +11,42 @@
911
namespace Test\DB\QueryBuilder;
1012

1113
use Doctrine\DBAL\Query\Expression\ExpressionBuilder as DoctrineExpressionBuilder;
14+
use OC\DB\Connection;
1215
use OC\DB\QueryBuilder\ExpressionBuilder\ExpressionBuilder;
1316
use OC\DB\QueryBuilder\Literal;
17+
use OCP\DB\QueryBuilder\IFunctionBuilder;
1418
use OCP\DB\QueryBuilder\IQueryBuilder;
1519
use OCP\IDBConnection;
1620
use OCP\Server;
21+
use PHPUnit\Framework\Attributes\DataProvider;
22+
use PHPUnit\Framework\Attributes\Group;
23+
use PHPUnit\Framework\MockObject\MockObject;
1724
use Psr\Log\LoggerInterface;
1825
use Test\TestCase;
1926

2027
/**
2128
* Class ExpressionBuilderTest
2229
*
23-
*
2430
* @package Test\DB\QueryBuilder
2531
*/
26-
#[\PHPUnit\Framework\Attributes\Group('DB')]
32+
#[Group('DB')]
2733
class ExpressionBuilderTest extends TestCase {
28-
/** @var ExpressionBuilder */
29-
protected $expressionBuilder;
30-
31-
/** @var DoctrineExpressionBuilder */
32-
protected $doctrineExpressionBuilder;
33-
34-
/** @var IDBConnection */
35-
protected $connection;
36-
37-
/** @var \Doctrine\DBAL\Connection */
38-
protected $internalConnection;
39-
40-
/** @var LoggerInterface */
41-
protected $logger;
34+
protected ExpressionBuilder $expressionBuilder;
35+
protected DoctrineExpressionBuilder $doctrineExpressionBuilder;
36+
protected IDBConnection $connection;
37+
protected \Doctrine\DBAL\Connection $internalConnection;
38+
protected LoggerInterface&MockObject $logger;
4239

4340
protected function setUp(): void {
4441
parent::setUp();
4542

4643
$this->connection = Server::get(IDBConnection::class);
47-
$this->internalConnection = Server::get(\OC\DB\Connection::class);
44+
$this->internalConnection = Server::get(Connection::class);
4845
$this->logger = $this->createMock(LoggerInterface::class);
4946

5047
$queryBuilder = $this->createMock(IQueryBuilder::class);
48+
$queryBuilder->method('func')
49+
->willReturn($this->createMock(IFunctionBuilder::class));
5150

5251
$this->expressionBuilder = new ExpressionBuilder($this->connection, $queryBuilder, $this->logger);
5352

@@ -67,16 +66,8 @@ public static function dataComparison(): array {
6766
return $testSets;
6867
}
6968

70-
/**
71-
*
72-
* @param string $comparison
73-
* @param mixed $input1
74-
* @param bool $isInput1Literal
75-
* @param mixed $input2
76-
* @param bool $isInput2Literal
77-
*/
78-
#[\PHPUnit\Framework\Attributes\DataProvider('dataComparison')]
79-
public function testComparison($comparison, $input1, $isInput1Literal, $input2, $isInput2Literal): void {
69+
#[DataProvider('dataComparison')]
70+
public function testComparison(string $comparison, string $input1, bool $isInput1Literal, string $input2, bool $isInput2Literal): void {
8071
[$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
8172
[$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
8273

@@ -95,15 +86,8 @@ public static function dataComparisons(): array {
9586
];
9687
}
9788

98-
/**
99-
*
100-
* @param mixed $input1
101-
* @param bool $isInput1Literal
102-
* @param mixed $input2
103-
* @param bool $isInput2Literal
104-
*/
105-
#[\PHPUnit\Framework\Attributes\DataProvider('dataComparisons')]
106-
public function testEquals($input1, $isInput1Literal, $input2, $isInput2Literal): void {
89+
#[DataProvider('dataComparisons')]
90+
public function testEquals(string $input1, bool $isInput1Literal, string $input2, bool $isInput2Literal): void {
10791
[$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
10892
[$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
10993

@@ -113,15 +97,8 @@ public function testEquals($input1, $isInput1Literal, $input2, $isInput2Literal)
11397
);
11498
}
11599

116-
/**
117-
*
118-
* @param mixed $input1
119-
* @param bool $isInput1Literal
120-
* @param mixed $input2
121-
* @param bool $isInput2Literal
122-
*/
123-
#[\PHPUnit\Framework\Attributes\DataProvider('dataComparisons')]
124-
public function testNotEquals($input1, $isInput1Literal, $input2, $isInput2Literal): void {
100+
#[DataProvider('dataComparisons')]
101+
public function testNotEquals(string $input1, bool $isInput1Literal, string $input2, bool $isInput2Literal): void {
125102
[$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
126103
[$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
127104

@@ -131,15 +108,8 @@ public function testNotEquals($input1, $isInput1Literal, $input2, $isInput2Liter
131108
);
132109
}
133110

134-
/**
135-
*
136-
* @param mixed $input1
137-
* @param bool $isInput1Literal
138-
* @param mixed $input2
139-
* @param bool $isInput2Literal
140-
*/
141-
#[\PHPUnit\Framework\Attributes\DataProvider('dataComparisons')]
142-
public function testLowerThan($input1, $isInput1Literal, $input2, $isInput2Literal): void {
111+
#[DataProvider('dataComparisons')]
112+
public function testLowerThan(string $input1, bool $isInput1Literal, string $input2, bool $isInput2Literal): void {
143113
[$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
144114
[$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
145115

@@ -149,15 +119,8 @@ public function testLowerThan($input1, $isInput1Literal, $input2, $isInput2Liter
149119
);
150120
}
151121

152-
/**
153-
*
154-
* @param mixed $input1
155-
* @param bool $isInput1Literal
156-
* @param mixed $input2
157-
* @param bool $isInput2Literal
158-
*/
159-
#[\PHPUnit\Framework\Attributes\DataProvider('dataComparisons')]
160-
public function testLowerThanEquals($input1, $isInput1Literal, $input2, $isInput2Literal): void {
122+
#[DataProvider('dataComparisons')]
123+
public function testLowerThanEquals(string $input1, bool $isInput1Literal, string $input2, bool $isInput2Literal): void {
161124
[$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
162125
[$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
163126

@@ -167,15 +130,8 @@ public function testLowerThanEquals($input1, $isInput1Literal, $input2, $isInput
167130
);
168131
}
169132

170-
/**
171-
*
172-
* @param mixed $input1
173-
* @param bool $isInput1Literal
174-
* @param mixed $input2
175-
* @param bool $isInput2Literal
176-
*/
177-
#[\PHPUnit\Framework\Attributes\DataProvider('dataComparisons')]
178-
public function testGreaterThan($input1, $isInput1Literal, $input2, $isInput2Literal): void {
133+
#[DataProvider('dataComparisons')]
134+
public function testGreaterThan(string $input1, bool $isInput1Literal, string $input2, bool $isInput2Literal): void {
179135
[$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
180136
[$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
181137

@@ -185,15 +141,8 @@ public function testGreaterThan($input1, $isInput1Literal, $input2, $isInput2Lit
185141
);
186142
}
187143

188-
/**
189-
*
190-
* @param mixed $input1
191-
* @param bool $isInput1Literal
192-
* @param mixed $input2
193-
* @param bool $isInput2Literal
194-
*/
195-
#[\PHPUnit\Framework\Attributes\DataProvider('dataComparisons')]
196-
public function testGreaterThanEquals($input1, $isInput1Literal, $input2, $isInput2Literal): void {
144+
#[DataProvider('dataComparisons')]
145+
public function testGreaterThanEquals(string $input1, bool $isInput1Literal, string $input2, bool $isInput2Literal): void {
197146
[$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
198147
[$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
199148

@@ -224,13 +173,8 @@ public static function dataLike(): array {
224173
];
225174
}
226175

227-
/**
228-
*
229-
* @param mixed $input
230-
* @param bool $isLiteral
231-
*/
232-
#[\PHPUnit\Framework\Attributes\DataProvider('dataLike')]
233-
public function testLike($input, $isLiteral): void {
176+
#[DataProvider('dataLike')]
177+
public function testLike(string $input, bool $isLiteral): void {
234178
[$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
235179

236180
$this->assertEquals(
@@ -239,13 +183,8 @@ public function testLike($input, $isLiteral): void {
239183
);
240184
}
241185

242-
/**
243-
*
244-
* @param mixed $input
245-
* @param bool $isLiteral
246-
*/
247-
#[\PHPUnit\Framework\Attributes\DataProvider('dataLike')]
248-
public function testNotLike($input, $isLiteral): void {
186+
#[DataProvider('dataLike')]
187+
public function testNotLike(string $input, bool $isLiteral): void {
249188
[$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
250189

251190
$this->assertEquals(
@@ -263,13 +202,8 @@ public static function dataIn(): array {
263202
];
264203
}
265204

266-
/**
267-
*
268-
* @param mixed $input
269-
* @param bool $isLiteral
270-
*/
271-
#[\PHPUnit\Framework\Attributes\DataProvider('dataIn')]
272-
public function testIn($input, $isLiteral): void {
205+
#[DataProvider('dataIn')]
206+
public function testIn(string|array $input, bool $isLiteral): void {
273207
[$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
274208

275209
$this->assertEquals(
@@ -278,13 +212,8 @@ public function testIn($input, $isLiteral): void {
278212
);
279213
}
280214

281-
/**
282-
*
283-
* @param mixed $input
284-
* @param bool $isLiteral
285-
*/
286-
#[\PHPUnit\Framework\Attributes\DataProvider('dataIn')]
287-
public function testNotIn($input, $isLiteral): void {
215+
#[DataProvider('dataIn')]
216+
public function testNotIn(string|array $input, bool $isLiteral): void {
288217
[$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
289218

290219
$this->assertEquals(
@@ -293,7 +222,7 @@ public function testNotIn($input, $isLiteral): void {
293222
);
294223
}
295224

296-
protected function helpWithLiteral($input, $isLiteral) {
225+
protected function helpWithLiteral(string|array $input, bool $isLiteral) {
297226
if ($isLiteral) {
298227
if (is_array($input)) {
299228
$doctrineInput = array_map(function ($ident) {
@@ -332,13 +261,8 @@ public static function dataLiteral(): array {
332261
];
333262
}
334263

335-
/**
336-
*
337-
* @param mixed $input
338-
* @param string|null $type
339-
*/
340-
#[\PHPUnit\Framework\Attributes\DataProvider('dataLiteral')]
341-
public function testLiteral($input, $type): void {
264+
#[DataProvider('dataLiteral')]
265+
public function testLiteral(string|int $input, string|int|null $type): void {
342266
/** @var Literal $actual */
343267
$actual = $this->expressionBuilder->literal($input, $type);
344268

@@ -376,15 +300,8 @@ public static function dataClobComparisons(): array {
376300
];
377301
}
378302

379-
/**
380-
* @param string $function
381-
* @param mixed $value
382-
* @param mixed $type
383-
* @param bool $compareKeyToValue
384-
* @param int $expected
385-
*/
386-
#[\PHPUnit\Framework\Attributes\DataProvider('dataClobComparisons')]
387-
public function testClobComparisons($function, $value, $type, $compareKeyToValue, $expected): void {
303+
#[DataProvider('dataClobComparisons')]
304+
public function testClobComparisons(string $function, string|array $value, int $type, bool $compareKeyToValue, int $expected): void {
388305
$appId = $this->getUniqueID('testing');
389306
$this->createConfig($appId, 1, 4);
390307
$this->createConfig($appId, 2, 5);
@@ -419,7 +336,7 @@ public function testClobComparisons($function, $value, $type, $compareKeyToValue
419336
->executeStatement();
420337
}
421338

422-
protected function createConfig($appId, $key, $value) {
339+
protected function createConfig(string $appId, int $key, int|string $value) {
423340
$query = $this->connection->getQueryBuilder();
424341
$query->insert('appconfig')
425342
->values([

0 commit comments

Comments
 (0)