1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Apicart\Utils\Tests\Sql; |
4
|
|
|
|
5
|
|
|
use Apicart\Utils\Sql\QueryBuilder; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
final class QueryBuilderTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var QueryBuilder |
13
|
|
|
*/ |
14
|
|
|
private $queryBuilder; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
protected function setUp(): void |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
|
21
|
|
|
$this->queryBuilder = $this->createQueryBuilder(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
public function testGetSql(): void |
26
|
|
|
{ |
27
|
|
|
self::assertSame( |
28
|
|
|
'SELECT a.id, a.name, a.price' . PHP_EOL |
29
|
|
|
. 'FROM table a' . PHP_EOL |
30
|
|
|
. 'INNER JOIN inner_table b ON b.id = a.id' . PHP_EOL |
31
|
|
|
. 'LEFT JOIN left_table c ON c.id = b.id' . PHP_EOL |
32
|
|
|
. 'WHERE a.id IN ($1,$2,$3) AND (b.id = $4) OR ((c.id IS NULL OR c.id > $5)' . PHP_EOL, |
33
|
|
|
$this->queryBuilder->getSql() |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function testGetParameters(): void |
39
|
|
|
{ |
40
|
|
|
self::assertSame([ |
41
|
|
|
'ids' => [1, 2, 3], |
42
|
|
|
'bId' => 'aaa', |
43
|
|
|
'minId' => 0, |
44
|
|
|
], $this->queryBuilder->getParameters()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
private function createQueryBuilder(): QueryBuilder |
49
|
|
|
{ |
50
|
|
|
return QueryBuilder::create() |
51
|
|
|
->select('a.id, a.name') |
52
|
|
|
->addSelect('a.price') |
53
|
|
|
->from('table', 'a') |
54
|
|
|
->innerJoin('inner_table', 'b', 'b.id = a.id') |
55
|
|
|
->leftJoin('left_table', 'c', 'c.id = b.id') |
56
|
|
|
->where('a.id IN (:ids)') |
57
|
|
|
->andWhere('b.id = :bId') |
58
|
|
|
->orWhere('(c.id IS NULL OR c.id > :minId') |
59
|
|
|
->setParameters([ |
60
|
|
|
'ids' => [1, 2, 3], |
61
|
|
|
'bId' => 'aaa', |
62
|
|
|
]) |
63
|
|
|
->setParameter('minId', 0); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|