Completed
Push — master ( c00bbf...06e52a )
by Pavel
17:08 queued 22s
created

ReadControllerTest::testReadAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 9.7692
c 0
b 0
f 0
cc 1
eloc 41
nc 1
nop 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace ScayTrase\Api\Cruds\Tests\Controller;
4
5
use ScayTrase\Api\Cruds\Tests\WebTestCase;
6
use ScayTrase\Api\Cruds\Tests\Fixtures\Common\Entity\MyEntity;
7
8
class ReadControllerTest extends WebTestCase
9
{
10
    public function testReadAction()
11
    {
12
        $em     = $this->getEntityManager();
13
        $entity = new MyEntity('my-test-secret');
14
        $em->persist($entity);
15
        $parent = new MyEntity('non-recursing-entity');
16
        $em->persist($parent);
17
        $entity->setParent($parent);
18
        $em->flush();
19
        $em->clear();
20
21
        $client = self::createClient();
22
        $client->request(
23
            'GET',
24
            '/api/entity/my-entity/get',
25
            ['identifier' => $entity->getId()],
26
            [],
27
            ['HTTP_CONTENT_TYPE' => 'application/json']
28
        );
29
        $response = $client->getResponse();
30
31
        self::assertTrue($response->isSuccessful());
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($entity->getId(), $data->id);
38
        self::assertObjectHasAttribute('public_api_field', $data);
39
        self::assertSame('defaults', $data->public_api_field);
40
        self::assertObjectNotHasAttribute('private_field', $data);
41
        self::assertSame($parent->getId(), $data->parent);
42
        self::assertSame([], $data->children);
43
44
        $client->request(
45
            'GET',
46
            '/api/entity/my-entity/get',
47
            ['identifier' => $parent->getId()],
48
            [],
49
            ['HTTP_CONTENT_TYPE' => 'application/json']
50
        );
51
        $response = $client->getResponse();
52
53
        self::assertTrue($response->isSuccessful());
54
        $data = json_decode($response->getContent());
55
56
        self::assertEquals(JSON_ERROR_NONE, json_last_error());
57
58
        self::assertInstanceOf(\stdClass::class, $data);
59
        self::assertSame($parent->getId(), $data->id);
60
        self::assertSame('defaults', $data->public_api_field);
61
        self::assertObjectNotHasAttribute('private_field', $data);
62
        self::assertNull($data->parent);
63
        self::assertSame([$entity->getId()], $data->children);
64
    }
65
}
66