1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminListBundle\Tests\AdminList\Helper; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
6
|
|
|
use Doctrine\DBAL\Statement; |
7
|
|
|
use Kunstmaan\AdminListBundle\Helper\DoctrineDBALAdapter; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class DoctrineDBALAdapterTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @expectedException \LogicException |
14
|
|
|
* @expectedExceptionMessage The $countField must contain a table alias in the string. |
15
|
|
|
*/ |
16
|
|
|
public function testConstructorWithIncorrectCountField() |
17
|
|
|
{ |
18
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
19
|
|
|
new DoctrineDBALAdapter($qb, 'somefield'); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testGetQueryBuilder() |
23
|
|
|
{ |
24
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
25
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
26
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$this->assertInstanceOf(QueryBuilder::class, $adapter->getQueryBuilder()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @expectedException \LogicException |
33
|
|
|
* @expectedExceptionMessage Only SELECT queries can be paginated. |
34
|
|
|
*/ |
35
|
|
|
public function testConstructorThrowsAnotherException() |
36
|
|
|
{ |
37
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
38
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::DELETE); |
39
|
|
|
|
40
|
|
|
new DoctrineDBALAdapter($qb, 'table.somefield'); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGetSlice() |
44
|
|
|
{ |
45
|
|
|
$length = 3; |
46
|
|
|
|
47
|
|
|
$statement = $this->createMock(Statement::class); |
48
|
|
|
$statement->expects($this->once())->method('fetchAll')->willReturn([1, 2, 3]); |
49
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
50
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
51
|
|
|
$qb->expects($this->once())->method('setFirstResult')->willReturn($qb); |
52
|
|
|
$qb->expects($this->once())->method('setMaxResults')->with($length)->willReturn($qb); |
53
|
|
|
$qb->expects($this->once())->method('execute')->willReturn($statement); |
54
|
|
|
|
55
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
|
|
|
|
56
|
|
|
$result = $adapter->getSlice(0, $length); |
57
|
|
|
$this->assertCount($length, $result); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testNbResults() |
61
|
|
|
{ |
62
|
|
|
$statement = $this->createMock(Statement::class); |
63
|
|
|
$statement->expects($this->once())->method('fetchColumn')->with(0)->willReturn([1, 2, 3]); |
64
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
65
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
66
|
|
|
$qb->expects($this->once())->method('select')->willReturn($qb); |
67
|
|
|
$qb->expects($this->once())->method('orderBy')->willReturn($qb); |
68
|
|
|
$qb->expects($this->once())->method('setMaxResults')->with(1)->willReturn($qb); |
69
|
|
|
$qb->expects($this->once())->method('execute')->willReturn($statement); |
70
|
|
|
|
71
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
|
|
|
|
72
|
|
|
$result = $adapter->getNbResults(); |
73
|
|
|
$this->assertCount(3, $result); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testNbResultsWithZeroResults() |
77
|
|
|
{ |
78
|
|
|
$statement = $this->createMock(Statement::class); |
79
|
|
|
$statement->expects($this->once())->method('fetchColumn')->with(0)->willReturn(null); |
80
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
81
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
82
|
|
|
$qb->expects($this->once())->method('select')->willReturn($qb); |
83
|
|
|
$qb->expects($this->once())->method('orderBy')->willReturn($qb); |
84
|
|
|
$qb->expects($this->once())->method('setMaxResults')->with(1)->willReturn($qb); |
85
|
|
|
$qb->expects($this->once())->method('execute')->willReturn($statement); |
86
|
|
|
|
87
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
|
|
|
|
88
|
|
|
$result = $adapter->getNbResults(); |
89
|
|
|
$this->assertSame(0, $result); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: