Completed
Pull Request — master (#14)
by Pavel
04:00
created

ProxyTestAbstract::getClientNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Tests;
4
5
use Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity;
6
use Doctrine\Common\Proxy\Proxy;
7
use ScayTrase\Api\Rpc\RpcRequestInterface;
8
9
class ProxyTestAbstract extends AbstractEntityManagerTest
10
{
11
    public function testLazyProxy()
12
    {
13
        $manager = $this->getManager();
14
15
        $this->getClient()->push(
16
            $this->getResponseMock(
17
                true,
18
                (object)[
19
                    'id'          => 2,
20
                    'payload'     => 'test-payload',
21
                    'sub-payload' => 'sub-payload',
22
                ]
23
            ),
24
            function (RpcRequestInterface $request) {
25
                self::assertEquals('test-entity/find', $request->getMethod());
26
                self::assertEquals(['id' => 2], $request->getParameters());
27
28
                return true;
29
            }
30
        );
31
32
        /** @var SubEntity|Proxy $entity */
33
        $entity = $manager->getReference(SubEntity::class, 2);
34
35
        //Test that entity is a proxy and request was not send
36
        self::assertInstanceOf(Proxy::class, $entity);
37
        self::assertFalse($entity->__isInitialized());
0 ignored issues
show
Bug introduced by
The method __isInitialized does only exist in Doctrine\Common\Proxy\Proxy, but not in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity.

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...
38
        self::assertCount(1, $this->getClient());
39
40
        //Test that we can obtain ID and request was still not sent
41
        self::assertEquals(2, $entity->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity, but not in Doctrine\Common\Proxy\Proxy.

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...
42
        self::assertInstanceOf(Proxy::class, $entity);
43
        self::assertFalse($entity->__isInitialized());
44
        self::assertCount(1, $this->getClient());
45
46
        //Test that we can obtain data and request was sent
47
        self::assertInstanceOf(SubEntity::class, $entity);
48
        self::assertEquals('test-payload', $entity->getPayload());
0 ignored issues
show
Bug introduced by
The method getPayload does only exist in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity, but not in Doctrine\Common\Proxy\Proxy.

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...
49
        self::assertEquals('sub-payload', $entity->getSubPayload());
0 ignored issues
show
Bug introduced by
The method getSubPayload does only exist in Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity, but not in Doctrine\Common\Proxy\Proxy.

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...
50
51
        //Test that we are still a Proxy object
52
        self::assertInstanceOf(Proxy::class, $entity);
53
        self::assertTrue($entity->__isInitialized());
54
        self::assertCount(0, $this->getClient());
55
    }
56
57 View Code Duplication
    public function testSimpleProxy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
58
    {
59
        $repository = $this->getManager()->getRepository(SubEntity::class);
60
61
        $this->getClient()->push(
62
            $this->getResponseMock(
63
                true,
64
                (object)[
65
                    'id'          => 2,
66
                    'payload'     => 'test-payload',
67
                    'sub-payload' => 'sub-payload',
68
                ]
69
            ),
70
            function (RpcRequestInterface $request) {
71
                self::assertEquals('test-entity/find', $request->getMethod());
72
                self::assertEquals(['id' => 2], $request->getParameters());
73
74
                return true;
75
            }
76
        );
77
78
        /** @var SubEntity $entity */
79
        $entity = $repository->find(2);
80
81
        //Test that we can obtain data and request was sent
82
        self::assertInstanceOf(SubEntity::class, $entity);
83
        self::assertEquals(2, $entity->getId());
84
        self::assertEquals('test-payload', $entity->getPayload());
85
        self::assertEquals('sub-payload', $entity->getSubPayload());
86
    }
87
88
    protected function getClientNames()
89
    {
90
        return [self::DEFAULT_CLIENT, 'test-reference-client'];
91
    }
92
}
93