Completed
Push — master ( f0cf2e...0fffc8 )
by Karel
11:40 queued 04:22
created

testTransformUsesDefaultQueryBuilderMethodConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 21
Ratio 100 %
Metric Value
dl 21
loc 21
rs 9.3142
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace FOS\ElasticaBundle\Tests\Doctrine\ORM;
4
5
use FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer;
6
7
class ElasticaToModelTransformerTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var \Doctrine\Common\Persistence\ManagerRegistry|\PHPUnit_Framework_MockObject_MockObject
11
     */
12
    protected $registry;
13
14
    /**
15
     * @var \Doctrine\ORM\EntityManager|\PHPUnit_Framework_MockObject_MockObject
16
     */
17
    protected $manager;
18
19
    /**
20
     * @var \Doctrine\Common\Persistence\ObjectRepository|\PHPUnit_Framework_MockObject_MockObject
21
     */
22
    protected $repository;
23
24
    /**
25
     * @var string
26
     */
27
    protected $objectClass = 'stdClass';
28
29
    /**
30
     * Tests that the Transformer uses the query_builder_method configuration option
31
     * allowing configuration of createQueryBuilder call.
32
     */
33 View Code Duplication
    public function testTransformUsesQueryBuilderMethodConfiguration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
34
    {
35
        $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
36
            ->disableOriginalConstructor()
37
            ->getMock();
38
39
        $this->repository->expects($this->once())
40
            ->method('customQueryBuilderCreator')
41
            ->with($this->equalTo(ElasticaToModelTransformer::ENTITY_ALIAS))
42
            ->will($this->returnValue($qb));
43
        $this->repository->expects($this->never())
44
            ->method('createQueryBuilder');
45
46
        $transformer = new ElasticaToModelTransformer($this->registry, $this->objectClass, array(
47
            'query_builder_method' => 'customQueryBuilderCreator',
48
        ));
49
50
        $class = new \ReflectionClass('FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer');
51
        $method = $class->getMethod('getEntityQueryBuilder');
52
        $method->setAccessible(true);
53
54
        $method->invokeArgs($transformer, array());
55
    }
56
57
    /**
58
     * Tests that the Transformer uses the query_builder_method configuration option
59
     * allowing configuration of createQueryBuilder call.
60
     */
61 View Code Duplication
    public function testTransformUsesDefaultQueryBuilderMethodConfiguration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
62
    {
63
        $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
64
            ->disableOriginalConstructor()
65
            ->getMock();
66
67
        $this->repository->expects($this->never())
68
            ->method('customQueryBuilderCreator');
69
        $this->repository->expects($this->once())
70
            ->method('createQueryBuilder')
71
            ->with($this->equalTo(ElasticaToModelTransformer::ENTITY_ALIAS))
72
            ->will($this->returnValue($qb));
73
74
        $transformer = new ElasticaToModelTransformer($this->registry, $this->objectClass);
75
76
        $class = new \ReflectionClass('FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer');
77
        $method = $class->getMethod('getEntityQueryBuilder');
78
        $method->setAccessible(true);
79
80
        $method->invokeArgs($transformer, array());
81
    }
82
83
84
    /**
85
     * Checks that the 'hints' parameter is used on the created query
86
     */
87
    public function testUsesHintsConfigurationIfGiven()
88
    {
89
        $query = $this->getMockBuilder('Doctrine\ORM\AbstractQuery')
90
            ->setMethods(array('setHint', 'execute', 'setHydrationMode'))
91
            ->disableOriginalConstructor()
92
            ->getMockForAbstractClass();
93
        $query->expects($this->any())->method('setHydrationMode')->willReturnSelf();
94
        $query->expects($this->once())  //  check if the hint is set
95
            ->method('setHint')
96
            ->with('customHintName', 'Custom\Hint\Class')
97
            ->willReturnSelf();
98
99
        $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
100
            ->disableOriginalConstructor()
101
            ->getMock();
102
        $qb->expects($this->any())->method('getQuery')->willReturn($query);
103
        $qb->expects($this->any())->method('expr')->willReturn($this->getMock('Doctrine\ORM\Query\Expr'));
104
        $qb->expects($this->any())->method('andWhere')->willReturnSelf();
105
106
        $this->repository->expects($this->once())
107
            ->method('createQueryBuilder')
108
            ->with($this->equalTo(ElasticaToModelTransformer::ENTITY_ALIAS))
109
            ->will($this->returnValue($qb));
110
111
        $transformer = new ElasticaToModelTransformer($this->registry, $this->objectClass, array(
112
            'hints' => array(
113
                array('name' => 'customHintName', 'value' => 'Custom\Hint\Class')
114
            )
115
        ));
116
117
        $class = new \ReflectionClass('FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer');
118
        $method = $class->getMethod('findByIdentifiers');
119
        $method->setAccessible(true);
120
121
        $method->invokeArgs($transformer, array(array(1, 2, 3), /* $hydrate */true));
122
    }
123
124
    protected function setUp()
125
    {
126
        $this->registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
127
            ->disableOriginalConstructor()
128
            ->getMock();
129
130
        $this->manager = $this->getMockBuilder('Doctrine\ORM\EntityManager')
131
            ->disableOriginalConstructor()
132
            ->getMock();
133
134
        $this->registry->expects($this->any())
135
            ->method('getManagerForClass')
136
            ->with($this->objectClass)
137
            ->will($this->returnValue($this->manager));
138
139
        $this->repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository', array(
140
            'customQueryBuilderCreator',
141
            'createQueryBuilder',
142
            'find',
143
            'findAll',
144
            'findBy',
145
            'findOneBy',
146
            'getClassName',
147
        ));
148
149
        $this->manager->expects($this->any())
150
            ->method('getRepository')
151
            ->with($this->objectClass)
152
            ->will($this->returnValue($this->repository));
153
    }
154
}
155