1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminListBundle\Tests\AdminList; |
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
|
|
|
/** |
11
|
|
|
* Class AdminListTest |
12
|
|
|
*/ |
13
|
|
|
class DoctrineDBALAdapterTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @expectedException \LogicException |
17
|
|
|
* @expectedExceptionMessage The $countField must contain a table alias in the string. |
18
|
|
|
*/ |
19
|
|
|
public function testConstructorWithIncorrectCountField() |
20
|
|
|
{ |
21
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
22
|
|
|
new DoctrineDBALAdapter($qb, 'somefield'); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testGetQueryBuilder() |
26
|
|
|
{ |
27
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
28
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
29
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$this->assertInstanceOf(QueryBuilder::class, $adapter->getQueryBuilder()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @expectedException \LogicException |
36
|
|
|
* @expectedExceptionMessage Only SELECT queries can be paginated. |
37
|
|
|
*/ |
38
|
|
|
public function testConstructorThrowsAnotherException() |
39
|
|
|
{ |
40
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
41
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::DELETE); |
42
|
|
|
|
43
|
|
|
new DoctrineDBALAdapter($qb, 'table.somefield'); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGetSlice() |
47
|
|
|
{ |
48
|
|
|
$length = 3; |
49
|
|
|
|
50
|
|
|
$statement = $this->createMock(Statement::class); |
51
|
|
|
$statement->expects($this->once())->method('fetchAll')->willReturn([1, 2, 3]); |
52
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
53
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
54
|
|
|
$qb->expects($this->once())->method('setFirstResult')->willReturn($qb); |
55
|
|
|
$qb->expects($this->once())->method('setMaxResults')->with($length)->willReturn($qb); |
56
|
|
|
$qb->expects($this->once())->method('execute')->willReturn($statement); |
57
|
|
|
|
58
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
|
|
|
|
59
|
|
|
$result = $adapter->getSlice(0, $length); |
60
|
|
|
$this->assertCount($length, $result); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testNbResults() |
64
|
|
|
{ |
65
|
|
|
$statement = $this->createMock(Statement::class); |
66
|
|
|
$statement->expects($this->once())->method('fetchColumn')->with(0)->willReturn([1, 2, 3]); |
67
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
68
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
69
|
|
|
$qb->expects($this->once())->method('select')->willReturn($qb); |
70
|
|
|
$qb->expects($this->once())->method('orderBy')->willReturn($qb); |
71
|
|
|
$qb->expects($this->once())->method('setMaxResults')->with(1)->willReturn($qb); |
72
|
|
|
$qb->expects($this->once())->method('execute')->willReturn($statement); |
73
|
|
|
|
74
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
|
|
|
|
75
|
|
|
$result = $adapter->getNbResults(); |
76
|
|
|
$this->assertCount(3, $result); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testNbResultsWithZeroResults() |
80
|
|
|
{ |
81
|
|
|
$statement = $this->createMock(Statement::class); |
82
|
|
|
$statement->expects($this->once())->method('fetchColumn')->with(0)->willReturn(null); |
83
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
84
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
85
|
|
|
$qb->expects($this->once())->method('select')->willReturn($qb); |
86
|
|
|
$qb->expects($this->once())->method('orderBy')->willReturn($qb); |
87
|
|
|
$qb->expects($this->once())->method('setMaxResults')->with(1)->willReturn($qb); |
88
|
|
|
$qb->expects($this->once())->method('execute')->willReturn($statement); |
89
|
|
|
|
90
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
|
|
|
|
91
|
|
|
$result = $adapter->getNbResults(); |
92
|
|
|
$this->assertSame(0, $result); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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: