Completed
Pull Request — master (#8)
by Pavel
05:07
created

CollectionLoadingTest::getClientNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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());
0 ignored issues
show
Bug introduced by
The method isInitialized does only exist in Bankiru\Api\Doctrine\Proxy\ApiCollection, but not in Doctrine\Common\Collections\ArrayCollection.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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 View Code Duplication
        foreach ($entities as $entity) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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