Completed
Push — master ( 9e925c...214c62 )
by Pavel
03:41
created

CollectionLoadingTest::testFindBy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 53
Code Lines 31

Duplication

Lines 10
Ratio 18.87 %

Code Coverage

Tests 41
CRAP Score 3

Importance

Changes 0
Metric Value
dl 10
loc 53
ccs 41
cts 41
cp 1
rs 9.5797
c 0
b 0
f 0
cc 3
eloc 31
nc 3
nop 0
crap 3

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 Bankiru\Api\Tests;
4
5
use Bankiru\Api\Doctrine\Proxy\ApiCollection;
6
use Bankiru\Api\Test\Entity\Sub\SubEntity;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use GuzzleHttp\Psr7\Response;
9
10
class CollectionLoadingTest extends AbstractEntityManagerTest
11
{
12 2
    protected function getClientNames()
13
    {
14 2
        return [self::DEFAULT_CLIENT, 'test-reference-client'];
15
    }
16
17 1
    public function testLazyCollections()
18
    {
19 1
        $repository = $this->getManager()->getRepository(SubEntity::class);
20
        /** @var SubEntity[]|ArrayCollection|ApiCollection $entities */
21 1
        $entities = $repository->findBy(['subPayload' => 'sub-payload']);
22
23 1
        self::assertInstanceOf(ApiCollection::class, $entities);
24 1
        self::assertFalse($entities->isInitialized());
25
26
        try {
27 1
            $entities->count();
28 1
        } catch (\OutOfBoundsException $exception) {
29 1
            self::assertEquals('Mock queue is empty', $exception->getMessage());
30
        }
31 1
    }
32
33 2
    public function testFindBy()
34
    {
35 1
        $repository = $this->getManager()->getRepository(SubEntity::class);
36
37 1
        $this->getResponseMock()->append(
38 1
            new Response(
39 1
                200,
40 1
                [],
41 1
                json_encode(
42
                    [
43 1
                        'jsonrpc' => '2.0',
44 1
                        'id'      => 'test',
45
                        'result'  => [
46
                            [
47 1
                                'id'             => '1',
48 1
                                'payload'        => 'test-payload-1',
49 1
                                'sub-payload'    => 'sub-payload',
50 1
                                'string-payload' => null,
51 1
                            ],
52
                            [
53 1
                                'id'             => '2',
54 1
                                'payload'        => 'test-payload-2',
55 1
                                'sub-payload'    => 'sub-payload',
56 1
                                'string-payload' => 123456,
57 1
                            ],
58
                            [
59 1
                                'id'             => '3',
60 1
                                'payload'        => 'test-payload-3',
61 1
                                'sub-payload'    => 'sub-payload',
62 1
                                'string-payload' => 'sub-payload',
63 1
                            ],
64 1
                        ],
65
                    ]
66 1
                )
67 1
            )
68 1
        );
69
70
        /** @var SubEntity[]|ArrayCollection $entities */
71 1
        $entities = $repository->findBy(['subPayload' => 'sub-payload']);
72 1
        self::assertInstanceOf(\Countable::class, $entities);
73 1
        self::assertCount(3, $entities);
74
75 1
        foreach ($entities as $entity) {
76 1
            self::assertInternalType('int', $entity->getId());
77 1
            self::assertInstanceOf(SubEntity::class, $entity);
78 1
            self::assertEquals('test-payload-' . $entity->getId(), $entity->getPayload());
79 2
            self::assertEquals('sub-payload', $entity->getSubPayload());
80
81 1
            if (null !== $entity->getStringPayload()) {
82 1
                self::assertInternalType('string', $entity->getStringPayload());
83 1
            }
84 1
        }
85 1
    }
86
}
87