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