-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
QueryAdapterTest.php
75 lines (57 loc) · 1.91 KB
/
QueryAdapterTest.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
<?php declare(strict_types=1);
namespace Pagerfanta\Doctrine\DBAL\Tests;
use Doctrine\DBAL\Query\QueryBuilder;
use Pagerfanta\Doctrine\DBAL\QueryAdapter;
final class QueryAdapterTest extends DBALTestCase
{
/**
* @var QueryBuilder
*/
private $qb;
protected function setUp(): void
{
parent::setUp();
$this->qb = new QueryBuilder($this->connection);
$this->qb->select('p.*')->from('posts', 'p');
}
public function testAdapterReturnsNumberOfResults(): void
{
$adapter = $this->createAdapterToTestGetNbResults();
$this->assertSame(50, $adapter->getNbResults());
}
public function testResultCountStaysConsistentAfterSlicing(): void
{
$adapter = $this->createAdapterToTestGetNbResults();
$adapter->getSlice(1, 10);
$this->assertSame(50, $adapter->getNbResults());
}
public function testGetSlice(): void
{
$adapter = new QueryAdapter($this->qb, static function (QueryBuilder $qb): void { });
$offset = 30;
$length = 10;
$this->qb->setFirstResult($offset)
->setMaxResults($length);
$this->assertSame($this->qb->executeQuery()->fetchAllAssociative(), $adapter->getSlice($offset, $length));
}
public function testTheAdapterUsesAClonedQuery(): void
{
$adapter = $this->createAdapterToTestGetNbResults();
$this->qb->innerJoin('p', 'comments', 'c', 'c.post_id = p.id')
->groupBy('c.post_id');
$this->assertSame(50, $adapter->getNbResults());
}
/**
* @return QueryAdapter<mixed>
*/
private function createAdapterToTestGetNbResults(): QueryAdapter
{
return new QueryAdapter(
$this->qb,
static function (QueryBuilder $qb): void {
$qb->select('COUNT(DISTINCT p.id) AS total_results')
->setMaxResults(1);
}
);
}
}