1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Tests\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
6
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository; |
7
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
8
|
|
|
use FOS\ElasticaBundle\Doctrine\MongoDBPagerProvider; |
9
|
|
|
use FOS\ElasticaBundle\Provider\PagerfantaPager; |
10
|
|
|
use FOS\ElasticaBundle\Provider\PagerProviderInterface; |
11
|
|
|
use FOS\ElasticaBundle\Tests\Mocks\DoctrineMongoDBCustomRepositoryMock; |
12
|
|
|
use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter; |
13
|
|
|
use Symfony\Bridge\Doctrine\ManagerRegistry; |
14
|
|
|
|
15
|
|
View Code Duplication |
class MongoDBPagerProviderTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
if (!class_exists(DocumentManager::class)) { |
20
|
|
|
$this->markTestSkipped('Doctrine MongoDB ODM is not available.'); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testShouldImplementPagerProviderInterface() |
25
|
|
|
{ |
26
|
|
|
$rc = new \ReflectionClass(MongoDBPagerProvider::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 MongoDBPagerProvider($doctrine, $objectClass, $baseConfig); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testShouldReturnPagerfanataPagerWithDoctrineODMMongoDBAdapter() |
41
|
|
|
{ |
42
|
|
|
$objectClass = 'anObjectClass'; |
43
|
|
|
$baseConfig = ['query_builder_method' => 'createQueryBuilder']; |
44
|
|
|
|
45
|
|
|
$expectedBuilder = $this->getMock(Builder::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 MongoDBPagerProvider($doctrine, $objectClass, $baseConfig); |
69
|
|
|
|
70
|
|
|
$pager = $provider->provide(); |
71
|
|
|
|
72
|
|
|
$this->assertInstanceOf(PagerfantaPager::class, $pager); |
73
|
|
|
|
74
|
|
|
$adapter = $pager->getPagerfanta()->getAdapter(); |
75
|
|
|
$this->assertInstanceOf(DoctrineODMMongoDBAdapter::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(DoctrineMongoDBCustomRepositoryMock::class, [], [], '', false); |
|
|
|
|
86
|
|
|
$repository |
87
|
|
|
->expects($this->once()) |
88
|
|
|
->method('createCustomQueryBuilder') |
89
|
|
|
->willReturn($this->getMock(Builder::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 MongoDBPagerProvider($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
|
|
|
} |
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.