Completed
Push — master ( 4e4cd8...063296 )
by Maksim
16s
created

ORMPagerProviderTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 97
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testShouldImplementPagerProviderInterface() 0 6 1
A testCouldBeConstructedWithExpectedArguments() 0 8 1
B testShouldReturnPagerfanataPagerWithDoctrineODMMongoDBAdapter() 0 37 1
B testShouldAllowCallCustomRepositoryMethod() 0 32 1
A createDoctrineMock() 0 4 1
1
<?php
2
3
namespace FOS\ElasticaBundle\Tests\Doctrine;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\ORM\QueryBuilder;
8
use FOS\ElasticaBundle\Doctrine\ORMPagerProvider;
9
use FOS\ElasticaBundle\Provider\PagerfantaPager;
10
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
11
use FOS\ElasticaBundle\Tests\Mocks\DoctrineORMCustomRepositoryMock;
12
use Pagerfanta\Adapter\DoctrineORMAdapter;
13
use Symfony\Bridge\Doctrine\ManagerRegistry;
14
15
class ORMPagerProviderTest extends \PHPUnit_Framework_TestCase
16
{
17
    public function testShouldImplementPagerProviderInterface()
18
    {
19
        $rc = new \ReflectionClass(ORMPagerProvider::class);
20
21
        $this->assertTrue($rc->implementsInterface(PagerProviderInterface::class));
22
    }
23
24
    public function testCouldBeConstructedWithExpectedArguments()
25
    {
26
        $doctrine = $this->createDoctrineMock();
27
        $objectClass = 'anObjectClass';
28
        $baseConfig = [];
29
30
        new ORMPagerProvider($doctrine, $objectClass, $baseConfig);
31
    }
32
33
    public function testShouldReturnPagerfanataPagerWithDoctrineODMMongoDBAdapter()
34
    {
35
        $objectClass = 'anObjectClass';
36
        $baseConfig = ['query_builder_method' => 'createQueryBuilder'];
37
38
        $expectedBuilder = $this->getMock(QueryBuilder::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
39
40
        $repository = $this->getMock(EntityRepository::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
41
        $repository
42
            ->expects($this->once())
43
            ->method('createQueryBuilder')
44
            ->willReturn($expectedBuilder);
45
46
        $manager = $this->getMock(EntityManager::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
47
        $manager
48
            ->expects($this->once())
49
            ->method('getRepository')
50
            ->with($objectClass)
51
            ->willReturn($repository);
52
53
54
        $doctrine = $this->createDoctrineMock();
55
        $doctrine
56
            ->expects($this->once())
57
            ->method('getManagerForClass')
58
            ->with($objectClass)
59
            ->willReturn($manager);
60
61
        $provider = new ORMPagerProvider($doctrine, $objectClass, $baseConfig);
62
63
        $pager = $provider->provide();
64
65
        $this->assertInstanceOf(PagerfantaPager::class, $pager);
66
67
        $adapter = $pager->getPagerfanta()->getAdapter();
68
        $this->assertInstanceOf(DoctrineORMAdapter::class, $adapter);
69
    }
70
71
    public function testShouldAllowCallCustomRepositoryMethod()
72
    {
73
        $objectClass = 'anObjectClass';
74
        $baseConfig = ['query_builder_method' => 'createQueryBuilder'];
75
76
        $repository = $this->getMock(DoctrineORMCustomRepositoryMock::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
77
        $repository
78
            ->expects($this->once())
79
            ->method('createCustomQueryBuilder')
80
            ->willReturn($this->getMock(QueryBuilder::class, [], [], '', false));
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
81
82
        $manager = $this->getMock(EntityManager::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
83
        $manager
84
            ->expects($this->once())
85
            ->method('getRepository')
86
            ->with($objectClass)
87
            ->willReturn($repository);
88
89
90
        $doctrine = $this->createDoctrineMock();
91
        $doctrine
92
            ->expects($this->once())
93
            ->method('getManagerForClass')
94
            ->with($objectClass)
95
            ->willReturn($manager);
96
97
        $provider = new ORMPagerProvider($doctrine, $objectClass, $baseConfig);
98
99
        $pager = $provider->provide(['query_builder_method' => 'createCustomQueryBuilder']);
100
101
        $this->assertInstanceOf(PagerfantaPager::class, $pager);
102
    }
103
104
    /**
105
     * @return \PHPUnit_Framework_MockObject_MockObject|ManagerRegistry
106
     */
107
    private function createDoctrineMock()
108
    {
109
        return $this->getMock(ManagerRegistry::class, [], [], '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
110
    }
111
}
112