Conditions | 1 |
Paths | 1 |
Total Lines | 81 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
85 | public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $value, $identifierType) |
||
86 | { |
||
87 | $meta = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataInfo'); |
||
88 | $meta->expects($this->any()) |
||
89 | ->method('getIdentifierFieldNames') |
||
90 | ->willReturn(array($id)); |
||
91 | $meta->expects($this->any()) |
||
92 | ->method('getTypeOfField') |
||
93 | ->willReturn($identifierType); |
||
94 | |||
95 | $mf = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataFactory'); |
||
96 | $mf->expects($this->any()) |
||
97 | ->method('getMetadataFor') |
||
98 | ->with($this->equalTo($class)) |
||
99 | ->willReturn($meta); |
||
100 | |||
101 | $platform = $this->createMock('Doctrine\DBAL\Platforms\PostgreSqlPlatform'); |
||
102 | |||
103 | $conn = $this->createMock('Doctrine\DBAL\Connection'); |
||
104 | $conn->expects($this->any()) |
||
105 | ->method('getDatabasePlatform') |
||
106 | ->willReturn($platform); |
||
107 | |||
108 | $em = $this->createMock('Doctrine\ORM\EntityManager'); |
||
109 | $em->expects($this->any()) |
||
110 | ->method('getMetadataFactory') |
||
111 | ->willReturn($mf); |
||
112 | $em->expects($this->any()) |
||
113 | ->method('getConnection') |
||
114 | ->willReturn($conn); |
||
115 | |||
116 | // NEXT MAJOR: Replace this when dropping PHP < 5.6 |
||
117 | // $q = $this->createMock('PDOStatement'); |
||
118 | $q = $this->getMockBuilder('stdClass') |
||
119 | ->setMethods(array('execute')) |
||
120 | ->getMock(); |
||
121 | $q->expects($this->any()) |
||
122 | ->method('execute') |
||
123 | ->willReturn(array(array($id => $value))); |
||
124 | |||
125 | $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder') |
||
126 | ->setConstructorArgs(array($em)) |
||
127 | ->getMock(); |
||
128 | $qb->expects($this->any()) |
||
129 | ->method('getEntityManager') |
||
130 | ->willReturn($em); |
||
131 | $qb->expects($this->any()) |
||
132 | ->method('getQuery') |
||
133 | ->willReturn($q); |
||
134 | $qb->expects($this->once()) |
||
135 | ->method('setParameter') |
||
136 | ->with($this->equalTo($expectedId), $this->equalTo(array($value))); |
||
137 | $qb->expects($this->any()) |
||
138 | ->method('getDQLPart') |
||
139 | ->will($this->returnCallBack(function ($part) use ($class, $alias) { |
||
140 | $parts = array( |
||
141 | 'from' => array(new From($class, $alias)), |
||
142 | 'orderBy' => array(new OrderBy('whatever', 'DESC')), |
||
143 | ); |
||
144 | |||
145 | return $parts[$part]; |
||
146 | })); |
||
147 | $qb->expects($this->once()) |
||
148 | ->method('addOrderBy') |
||
149 | ->with("$alias.$id", null); |
||
150 | $qb->expects($this->once()) |
||
151 | ->method('getRootEntities') |
||
152 | ->willReturn(array($class)); |
||
153 | $qb->expects($this->exactly(2)) |
||
154 | ->method('getRootAliases') |
||
155 | ->willReturn(array($alias)); |
||
156 | |||
157 | $pq = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery') |
||
158 | ->setConstructorArgs(array($qb)) |
||
159 | ->setMethods(array('a')) |
||
160 | ->getMock(); |
||
161 | |||
162 | /* Work */ |
||
163 | |||
164 | $pq->execute(); |
||
165 | } |
||
166 | |||
227 |