| Conditions | 1 |
| Paths | 1 |
| Total Lines | 70 |
| Code Lines | 46 |
| 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 |
||
| 10 | public function testCreateAction() |
||
| 11 | { |
||
| 12 | $client = self::createClient(); |
||
| 13 | $client->request( |
||
| 14 | 'POST', |
||
| 15 | '/api/entity/my-entity/create', |
||
| 16 | [ |
||
| 17 | 'data' => [ |
||
| 18 | 'public_api_field' => 'my-data', |
||
| 19 | ], |
||
| 20 | ], |
||
| 21 | [], |
||
| 22 | ['HTTP_CONTENT_TYPE' => 'application/json'] |
||
| 23 | ); |
||
| 24 | $response = $client->getResponse(); |
||
| 25 | |||
| 26 | self::assertTrue($response->isSuccessful()); |
||
| 27 | |||
| 28 | $em = $this->getEntityManager(); |
||
| 29 | $parent = $em->getRepository(MyEntity::class)->findOneBy(['publicApiField' => 'my-data']); |
||
| 30 | self::assertNotNull($parent); |
||
| 31 | |||
| 32 | $data = json_decode($response->getContent()); |
||
| 33 | |||
| 34 | self::assertEquals(JSON_ERROR_NONE, json_last_error()); |
||
| 35 | |||
| 36 | self::assertInstanceOf(\stdClass::class, $data); |
||
| 37 | self::assertSame($parent->getId(), $data->id); |
||
| 38 | self::assertObjectHasAttribute('public_api_field', $data); |
||
| 39 | self::assertSame('my-data', $data->public_api_field); |
||
| 40 | self::assertObjectNotHasAttribute('private_field', $data); |
||
| 41 | self::assertNull($data->parent); |
||
| 42 | self::assertSame([], $data->children); |
||
| 43 | |||
| 44 | $client->request( |
||
| 45 | 'POST', |
||
| 46 | '/api/entity/my-entity/create', |
||
| 47 | [ |
||
| 48 | 'data' => [ |
||
| 49 | 'public_api_field' => 'my-child-data', |
||
| 50 | 'parent' => $parent->getId(), |
||
| 51 | ], |
||
| 52 | ], |
||
| 53 | [], |
||
| 54 | ['HTTP_CONTENT_TYPE' => 'application/json'] |
||
| 55 | ); |
||
| 56 | $response = $client->getResponse(); |
||
| 57 | |||
| 58 | self::assertTrue($response->isSuccessful()); |
||
| 59 | $entity = $em->getRepository(MyEntity::class)->findOneBy(['publicApiField' => 'my-child-data']); |
||
| 60 | self::assertNotNull($parent); |
||
| 61 | |||
| 62 | $data = json_decode($response->getContent()); |
||
| 63 | |||
| 64 | self::assertEquals(JSON_ERROR_NONE, json_last_error()); |
||
| 65 | |||
| 66 | self::assertInstanceOf(\stdClass::class, $data); |
||
| 67 | self::assertSame($entity->getId(), $data->id); |
||
| 68 | self::assertSame('my-child-data', $data->public_api_field); |
||
| 69 | self::assertObjectNotHasAttribute('private_field', $data); |
||
| 70 | self::assertSame($parent->getId(), $data->parent); |
||
| 71 | self::assertSame([], $data->children); |
||
| 72 | |||
| 73 | $em->clear(); |
||
| 74 | $entity = $em->getRepository(MyEntity::class)->findOneBy(['publicApiField' => 'my-child-data']); |
||
| 75 | $parent = $em->getRepository(MyEntity::class)->findOneBy(['publicApiField' => 'my-data']); |
||
| 76 | |||
| 77 | self::assertSame($parent, $entity->getParent()); |
||
| 78 | self::assertContains($entity, $parent->getChildren()); |
||
| 79 | } |
||
| 80 | } |
||
| 81 |