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
|
|
|
class DoctrineDBALAdapterTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testConstructorWithIncorrectCountField() |
13
|
|
|
{ |
14
|
|
|
$this->expectExceptionMessage('The $countField must contain a table alias in the string.'); |
15
|
|
|
$this->expectException(\LogicException::class); |
16
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
17
|
|
|
new DoctrineDBALAdapter($qb, 'somefield'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
View Code Duplication |
public function testGetQueryBuilder() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
23
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
24
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
25
|
|
|
|
26
|
|
|
$this->assertInstanceOf(QueryBuilder::class, $adapter->getQueryBuilder()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function testConstructorThrowsAnotherException() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->expectExceptionMessage('Only SELECT queries can be paginated.'); |
32
|
|
|
$this->expectException(\LogicException::class); |
33
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
34
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::DELETE); |
35
|
|
|
|
36
|
|
|
new DoctrineDBALAdapter($qb, 'table.somefield'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testGetSlice() |
40
|
|
|
{ |
41
|
|
|
$length = 3; |
42
|
|
|
|
43
|
|
|
$statement = $this->createMock(Statement::class); |
44
|
|
|
$statement->expects($this->once())->method('fetchAll')->willReturn([1, 2, 3]); |
45
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
46
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
47
|
|
|
$qb->expects($this->once())->method('setFirstResult')->willReturn($qb); |
48
|
|
|
$qb->expects($this->once())->method('setMaxResults')->with($length)->willReturn($qb); |
49
|
|
|
$qb->expects($this->once())->method('execute')->willReturn($statement); |
50
|
|
|
|
51
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
52
|
|
|
$result = $adapter->getSlice(0, $length); |
53
|
|
|
$this->assertCount($length, $result); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testNbResults() |
57
|
|
|
{ |
58
|
|
|
$statement = $this->createMock(Statement::class); |
59
|
|
|
$statement->expects($this->once())->method('fetchColumn')->with(0)->willReturn([1, 2, 3]); |
60
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
61
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
62
|
|
|
$qb->expects($this->once())->method('select')->willReturn($qb); |
63
|
|
|
$qb->expects($this->once())->method('orderBy')->willReturn($qb); |
64
|
|
|
$qb->expects($this->once())->method('setMaxResults')->with(1)->willReturn($qb); |
65
|
|
|
$qb->expects($this->once())->method('execute')->willReturn($statement); |
66
|
|
|
|
67
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
68
|
|
|
$result = $adapter->getNbResults(); |
69
|
|
|
$this->assertCount(3, $result); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testNbResultsWithZeroResults() |
73
|
|
|
{ |
74
|
|
|
$statement = $this->createMock(Statement::class); |
75
|
|
|
$statement->expects($this->once())->method('fetchColumn')->with(0)->willReturn(null); |
76
|
|
|
$qb = $this->createMock(QueryBuilder::class); |
77
|
|
|
$qb->expects($this->once())->method('getType')->willReturn(QueryBuilder::SELECT); |
78
|
|
|
$qb->expects($this->once())->method('select')->willReturn($qb); |
79
|
|
|
$qb->expects($this->once())->method('orderBy')->willReturn($qb); |
80
|
|
|
$qb->expects($this->once())->method('setMaxResults')->with(1)->willReturn($qb); |
81
|
|
|
$qb->expects($this->once())->method('execute')->willReturn($statement); |
82
|
|
|
|
83
|
|
|
$adapter = new DoctrineDBALAdapter($qb, 'table.somefield'); |
84
|
|
|
$result = $adapter->getNbResults(); |
85
|
|
|
$this->assertSame(0, $result); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.