|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Tests\Doctrine; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
6
|
|
|
use Doctrine\ODM\PHPCR\DocumentManager; |
|
7
|
|
|
use Doctrine\ODM\PHPCR\DocumentRepository; |
|
8
|
|
|
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder; |
|
9
|
|
|
use FOS\ElasticaBundle\Doctrine\PHPCRPagerProvider; |
|
10
|
|
|
use FOS\ElasticaBundle\Provider\PagerfantaPager; |
|
11
|
|
|
use FOS\ElasticaBundle\Provider\PagerProviderInterface; |
|
12
|
|
|
use FOS\ElasticaBundle\Tests\Mocks\DoctrinePHPCRCustomRepositoryMock; |
|
13
|
|
|
use Pagerfanta\Adapter\DoctrineODMPhpcrAdapter; |
|
14
|
|
|
|
|
15
|
|
View Code Duplication |
class PHPCRPagerProviderTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
public function setUp() |
|
18
|
|
|
{ |
|
19
|
|
|
if (!class_exists(DocumentManager::class)) { |
|
20
|
|
|
$this->markTestSkipped('Doctrine PHPCR is not present'); |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testShouldImplementPagerProviderInterface() |
|
25
|
|
|
{ |
|
26
|
|
|
$rc = new \ReflectionClass(PHPCRPagerProvider::class); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertTrue($rc->implementsInterface(PagerProviderInterface::class)); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testCouldBeConstructedWithExpectedArguments() |
|
32
|
|
|
{ |
|
33
|
|
|
$doctrine = $this->createDoctrineMock(); |
|
34
|
|
|
$objectClass = 'anObjectClass'; |
|
35
|
|
|
$baseConfig = []; |
|
36
|
|
|
|
|
37
|
|
|
new PHPCRPagerProvider($doctrine, $objectClass, $baseConfig); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testShouldReturnPagerfanataPagerWithDoctrineODMMongoDBAdapter() |
|
41
|
|
|
{ |
|
42
|
|
|
$objectClass = 'anObjectClass'; |
|
43
|
|
|
$baseConfig = ['query_builder_method' => 'createQueryBuilder']; |
|
44
|
|
|
|
|
45
|
|
|
$expectedBuilder = $this->getMock(QueryBuilder::class, [], [], '', false); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$repository = $this->getMock(DocumentRepository::class, [], [], '', false); |
|
|
|
|
|
|
48
|
|
|
$repository |
|
49
|
|
|
->expects($this->once()) |
|
50
|
|
|
->method('createQueryBuilder') |
|
51
|
|
|
->willReturn($expectedBuilder); |
|
52
|
|
|
|
|
53
|
|
|
$manager = $this->getMock(DocumentManager::class, [], [], '', false); |
|
|
|
|
|
|
54
|
|
|
$manager |
|
55
|
|
|
->expects($this->once()) |
|
56
|
|
|
->method('getRepository') |
|
57
|
|
|
->with($objectClass) |
|
58
|
|
|
->willReturn($repository); |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$doctrine = $this->createDoctrineMock(); |
|
62
|
|
|
$doctrine |
|
63
|
|
|
->expects($this->once()) |
|
64
|
|
|
->method('getManagerForClass') |
|
65
|
|
|
->with($objectClass) |
|
66
|
|
|
->willReturn($manager); |
|
67
|
|
|
|
|
68
|
|
|
$provider = new PHPCRPagerProvider($doctrine, $objectClass, $baseConfig); |
|
69
|
|
|
|
|
70
|
|
|
$pager = $provider->provide(); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertInstanceOf(PagerfantaPager::class, $pager); |
|
73
|
|
|
|
|
74
|
|
|
$adapter = $pager->getPagerfanta()->getAdapter(); |
|
75
|
|
|
$this->assertInstanceOf(DoctrineODMPhpcrAdapter::class, $adapter); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertAttributeSame($expectedBuilder, 'queryBuilder', $adapter); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testShouldAllowCallCustomRepositoryMethod() |
|
81
|
|
|
{ |
|
82
|
|
|
$objectClass = 'anObjectClass'; |
|
83
|
|
|
$baseConfig = ['query_builder_method' => 'createQueryBuilder']; |
|
84
|
|
|
|
|
85
|
|
|
$repository = $this->getMock(DoctrinePHPCRCustomRepositoryMock::class, [], [], '', false); |
|
|
|
|
|
|
86
|
|
|
$repository |
|
87
|
|
|
->expects($this->once()) |
|
88
|
|
|
->method('createCustomQueryBuilder') |
|
89
|
|
|
->willReturn($this->getMock(QueryBuilder::class, [], [], '', false)); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$manager = $this->getMock(DocumentManager::class, [], [], '', false); |
|
|
|
|
|
|
92
|
|
|
$manager |
|
93
|
|
|
->expects($this->once()) |
|
94
|
|
|
->method('getRepository') |
|
95
|
|
|
->willReturn($repository); |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$doctrine = $this->createDoctrineMock(); |
|
99
|
|
|
$doctrine |
|
100
|
|
|
->expects($this->once()) |
|
101
|
|
|
->method('getManagerForClass') |
|
102
|
|
|
->willReturn($manager); |
|
103
|
|
|
|
|
104
|
|
|
$provider = new PHPCRPagerProvider($doctrine, $objectClass, $baseConfig); |
|
105
|
|
|
|
|
106
|
|
|
$pager = $provider->provide(['query_builder_method' => 'createCustomQueryBuilder']); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertInstanceOf(PagerfantaPager::class, $pager); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ManagerRegistry |
|
113
|
|
|
*/ |
|
114
|
|
|
private function createDoctrineMock() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->getMock(ManagerRegistry::class, [], [], '', false); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
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.