| Conditions | 1 |
| Paths | 1 |
| Total Lines | 81 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 86 | public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $value, $identifierType) |
||
| 87 | { |
||
| 88 | $meta = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataInfo'); |
||
| 89 | $meta->expects($this->any()) |
||
| 90 | ->method('getIdentifierFieldNames') |
||
| 91 | ->willReturn([$id]); |
||
| 92 | $meta->expects($this->any()) |
||
| 93 | ->method('getTypeOfField') |
||
| 94 | ->willReturn($identifierType); |
||
| 95 | |||
| 96 | $mf = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataFactory'); |
||
| 97 | $mf->expects($this->any()) |
||
| 98 | ->method('getMetadataFor') |
||
| 99 | ->with($this->equalTo($class)) |
||
| 100 | ->willReturn($meta); |
||
| 101 | |||
| 102 | $platform = $this->createMock('Doctrine\DBAL\Platforms\PostgreSqlPlatform'); |
||
| 103 | |||
| 104 | $conn = $this->createMock('Doctrine\DBAL\Connection'); |
||
| 105 | $conn->expects($this->any()) |
||
| 106 | ->method('getDatabasePlatform') |
||
| 107 | ->willReturn($platform); |
||
| 108 | |||
| 109 | $em = $this->createMock('Doctrine\ORM\EntityManager'); |
||
| 110 | $em->expects($this->any()) |
||
| 111 | ->method('getMetadataFactory') |
||
| 112 | ->willReturn($mf); |
||
| 113 | $em->expects($this->any()) |
||
| 114 | ->method('getConnection') |
||
| 115 | ->willReturn($conn); |
||
| 116 | |||
| 117 | // NEXT MAJOR: Replace this when dropping PHP < 5.6 |
||
| 118 | // $q = $this->createMock('PDOStatement'); |
||
| 119 | $q = $this->getMockBuilder('stdClass') |
||
| 120 | ->setMethods(['execute']) |
||
| 121 | ->getMock(); |
||
| 122 | $q->expects($this->any()) |
||
| 123 | ->method('execute') |
||
| 124 | ->willReturn([[$id => $value]]); |
||
| 125 | |||
| 126 | $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder') |
||
| 127 | ->setConstructorArgs([$em]) |
||
| 128 | ->getMock(); |
||
| 129 | $qb->expects($this->any()) |
||
| 130 | ->method('getEntityManager') |
||
| 131 | ->willReturn($em); |
||
| 132 | $qb->expects($this->any()) |
||
| 133 | ->method('getQuery') |
||
| 134 | ->willReturn($q); |
||
| 135 | $qb->expects($this->once()) |
||
| 136 | ->method('setParameter') |
||
| 137 | ->with($this->equalTo($expectedId), $this->equalTo([$value])); |
||
| 138 | $qb->expects($this->any()) |
||
| 139 | ->method('getDQLPart') |
||
| 140 | ->will($this->returnCallBack(function ($part) use ($class, $alias) { |
||
| 141 | $parts = [ |
||
| 142 | 'from' => [new From($class, $alias)], |
||
| 143 | 'orderBy' => [new OrderBy('whatever', 'DESC')], |
||
| 144 | ]; |
||
| 145 | |||
| 146 | return $parts[$part]; |
||
| 147 | })); |
||
| 148 | $qb->expects($this->once()) |
||
| 149 | ->method('addOrderBy') |
||
| 150 | ->with("$alias.$id", null); |
||
| 151 | $qb->expects($this->once()) |
||
| 152 | ->method('getRootEntities') |
||
| 153 | ->willReturn([$class]); |
||
| 154 | $qb->expects($this->exactly(2)) |
||
| 155 | ->method('getRootAliases') |
||
| 156 | ->willReturn([$alias]); |
||
| 157 | |||
| 158 | $pq = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery') |
||
| 159 | ->setConstructorArgs([$qb]) |
||
| 160 | ->setMethods(['a']) |
||
| 161 | ->getMock(); |
||
| 162 | |||
| 163 | /* Work */ |
||
| 164 | |||
| 165 | $pq->execute(); |
||
| 166 | } |
||
| 167 | |||
| 244 |