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

CollectionLoadingTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 12.99 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 10
loc 77
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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