| Conditions | 1 |
| Paths | 1 |
| Total Lines | 90 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 24 | public function testGetCollection() |
||
| 25 | { |
||
| 26 | // Create an admin configuration to test the collection method |
||
| 27 | $configuration = $this->createMock(AdminConfiguration::class); |
||
| 28 | $configuration |
||
| 29 | ->expects($this->atLeastOnce()) |
||
| 30 | ->method('getParameter') |
||
| 31 | ->willReturnMap([ |
||
| 32 | ['entity', 'MyClass'], |
||
| 33 | ['pager', null], |
||
| 34 | ['page_parameter', 'page'], |
||
| 35 | ]) |
||
| 36 | ; |
||
| 37 | |||
| 38 | /** @var AdminInterface|MockObject $admin */ |
||
| 39 | $admin = $this->createMock(AdminInterface::class); |
||
| 40 | $admin |
||
| 41 | ->expects($this->atLeastOnce()) |
||
| 42 | ->method('getConfiguration') |
||
| 43 | ->willReturn($configuration) |
||
| 44 | ; |
||
| 45 | |||
| 46 | // Create fake entities to return |
||
| 47 | $entities = [ |
||
| 48 | new EntityFixture(uniqid()), |
||
| 49 | new EntityFixture(uniqid()), |
||
| 50 | ]; |
||
| 51 | |||
| 52 | // The query should return entities |
||
| 53 | $query = $this->createMock(AbstractQuery::class); |
||
| 54 | $query |
||
| 55 | ->expects($this->once()) |
||
| 56 | ->method('getResult') |
||
| 57 | ->willReturn($entities) |
||
| 58 | ; |
||
| 59 | |||
| 60 | // The query builder should call the getQuery() method |
||
| 61 | $queryBuilder = $this->createMock(QueryBuilder::class); |
||
| 62 | $queryBuilder |
||
| 63 | ->expects($this->once()) |
||
| 64 | ->method('getQuery') |
||
| 65 | ->willReturn($query) |
||
| 66 | ; |
||
| 67 | |||
| 68 | // The repository should return a query builder |
||
| 69 | $repository = $this->createMock(EntityRepository::class); |
||
| 70 | $repository |
||
| 71 | ->expects($this->once()) |
||
| 72 | ->method('createQueryBuilder') |
||
| 73 | ->with('entity') |
||
| 74 | ->willReturn($queryBuilder) |
||
| 75 | ; |
||
| 76 | |||
| 77 | // The entity manager should return a repository |
||
| 78 | /** @var EntityManagerInterface|MockObject $entityManager */ |
||
| 79 | $entityManager = $this->createMock(EntityManagerInterface::class); |
||
| 80 | $entityManager |
||
| 81 | ->expects($this->once()) |
||
| 82 | ->method('getRepository') |
||
| 83 | ->willReturn($repository) |
||
| 84 | ; |
||
| 85 | |||
| 86 | // The event dispatcher should dispatched once a ORM_FILTER event |
||
| 87 | /** @var EventDispatcherInterface|MockObject $eventDispatcher */ |
||
| 88 | $eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
||
| 89 | $eventDispatcher |
||
| 90 | ->expects($this->once()) |
||
| 91 | ->method('dispatch') |
||
| 92 | ->willReturnCallback(function ($eventName, $event) use ($queryBuilder, $admin) { |
||
| 93 | $this->assertEquals(AdminEvents::DOCTRINE_ORM_FILTER, $eventName); |
||
| 94 | /** @var DoctrineOrmFilterEvent $event */ |
||
| 95 | $this->assertInstanceOf(DoctrineOrmFilterEvent::class, $event); |
||
| 96 | $this->assertEquals($queryBuilder, $event->getQueryBuilder()); |
||
| 97 | $this->assertEquals($admin, $event->getAdmin()); |
||
| 98 | $this->assertEquals([], $event->getFilters()); |
||
| 99 | }) |
||
| 100 | ; |
||
| 101 | /** @var RequestStack|MockObject $requestStack */ |
||
| 102 | $requestStack = $this->createMock(RequestStack::class); |
||
| 103 | |||
| 104 | $provider = new ORMDataProvider( |
||
| 105 | $entityManager, |
||
| 106 | $eventDispatcher, |
||
| 107 | $requestStack |
||
| 108 | ); |
||
| 109 | $returnedEntities = $provider->getCollection($admin); |
||
| 110 | |||
| 111 | // The returned entities should be the same as those provided by the repository |
||
| 112 | $this->assertEquals($entities, $returnedEntities); |
||
| 113 | } |
||
| 114 | |||
| 266 |