Completed
Push — master ( 746635...1250b5 )
by Pavel
04:18
created

ReadControllerTest::testSearchAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 57
rs 9.6818
cc 1
eloc 42
nc 1
nop 1

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\AbstractCrudsWebTest;
6
use ScayTrase\Api\Cruds\Tests\Fixtures\Common\Entity\MyEntity;
7
8
class ReadControllerTest extends AbstractCrudsWebTest
9
{
10
    /**
11
     * @dataProvider getKernelClasses
12
     *
13
     * @param $kernel
14
     */
15
    public function testSearchAction($kernel)
16
    {
17
        self::createAndBootKernel($kernel);
18
        self::configureDb();
19
20
        $em     = self::getEntityManager();
21
        $entity = new MyEntity('my-test-secret');
22
        $em->persist($entity);
23
        $parent = new MyEntity('non-recursing-entity');
24
        $em->persist($parent);
25
        $entity->setParent($parent);
26
        $em->flush();
27
        $em->clear();
28
29
        $client = self::createClient();
30
        $client->request(
31
            'GET',
32
            '/api/entity/my-entity/read',
33
            ['identifier' => $entity->getId()],
34
            [],
35
            ['HTTP_CONTENT_TYPE' => 'application/json']
36
        );
37
        $response = $client->getResponse();
38
39
        self::assertTrue($response->isSuccessful());
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($entity->getId(), $data->id);
46
        self::assertSame('defaults', $data->public_api_field);
47
        self::assertObjectNotHasAttribute('private_field', $data);
48
        self::assertSame($parent->getId(), $data->parent);
49
        self::assertSame([], $data->children);
50
51
        $client->request(
52
            'GET',
53
            '/api/entity/my-entity/read',
54
            ['identifier' => $parent->getId()],
55
            [],
56
            ['HTTP_CONTENT_TYPE' => 'application/json']
57
        );
58
        $response = $client->getResponse();
59
60
        self::assertTrue($response->isSuccessful());
61
        $data = json_decode($response->getContent());
62
63
        self::assertEquals(JSON_ERROR_NONE, json_last_error());
64
65
        self::assertInstanceOf(\stdClass::class, $data);
66
        self::assertSame($parent->getId(), $data->id);
67
        self::assertSame('defaults', $data->public_api_field);
68
        self::assertObjectNotHasAttribute('private_field', $data);
69
        self::assertNull($data->parent);
70
        self::assertSame([$entity->getId()], $data->children);
71
    }
72
}
73