Completed
Pull Request — master (#5669)
by Jeremy
48:42 queued 40:04
created

MappingVisitorTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 28
rs 8.8571
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
4
namespace Doctrine\Tests\ORM\Persisters\Collection\Expr;
5
6
7
use Doctrine\Common\Collections\Expr\Comparison;
8
use Doctrine\Common\Collections\Expr\Value;
9
use Doctrine\Common\Collections\Expr\CompositeExpression;
10
use Doctrine\DBAL\Platforms\AbstractPlatform;
11
use Doctrine\ORM\Mapping\ClassMetadata;
12
use Doctrine\ORM\Mapping\QuoteStrategy;
13
use Doctrine\ORM\Persisters\Collection\Expr\MappingVisitor;
14
15
class MappingVisitorTest extends \PHPUnit_Framework_TestCase
16
{
17
    /** @var  QuoteStrategy */
18
    private $quoteStrategy;
19
    /** @var  MappingVisitor */
20
    private $sut;
21
22
    protected function setUp()
23
    {
24
        parent::setUp();
25
26
        /** @var ClassMetadata $metadata */
27
        $metadata = $this->getMockBuilder('\Doctrine\ORM\Mapping\ClassMetadata')
28
            ->disableOriginalConstructor()
29
            ->getMock();
30
31
        /** @var AbstractPlatform $platform */
32
        $platform = $this->getMockBuilder('\Doctrine\DBAL\Platforms\AbstractPlatform')
33
            ->disableOriginalConstructor()
34
            ->getMock();
35
36
        $this->quoteStrategy = $this->getMockBuilder('\Doctrine\ORM\Mapping\QuoteStrategy')
37
            ->getMock();
38
39
        $this->quoteStrategy->expects($this->any())
40
            ->method('getColumnName')
41
            ->with('field', $metadata, $platform)
42
            ->willReturn('column');
43
44
        $this->sut = new MappingVisitor(
45
            $this->quoteStrategy,
46
            $metadata,
47
            $platform
48
        );
49
    }
50
51
52
    public function testWalkComparisonReturnsComparisonWithMappedField()
53
    {
54
        $expr = new Comparison('field', Comparison::GT, 73);
55
        /** @var Comparison $mapped */
56
        $mapped = $this->sut->dispatch($expr);
57
        static::assertInstanceOf('\Doctrine\Common\Collections\Expr\Comparison', $mapped);
58
        static::assertEquals('column', $mapped->getField());
59
        static::assertEquals($expr->getOperator(), $mapped->getOperator());
60
        static::assertEquals($expr->getValue(), $mapped->getValue());
61
62
    }
63
64
    public function testWalkValueReturnsValue()
65
    {
66
        $value = new Value('foo');
67
        static::assertSame($value, $this->sut->dispatch($value));
68
    }
69
70
    public function testWalkCompositeExpressionReturnsMappedCompositeExpression()
71
    {
72
        $gtExpr = new Comparison('field', Comparison::GT, 73);
73
        $ltExpr = new Comparison('field', Comparison::LT, 53);
74
75
        $composite = new CompositeExpression(
76
            CompositeExpression::TYPE_OR, [$gtExpr, $ltExpr]);
77
78
        /** @var CompositeExpression $mapped */
79
        $mapped = $this->sut->dispatch($composite);
80
        static::assertInstanceOf('\Doctrine\Common\Collections\Expr\CompositeExpression', $mapped);
81
        /** @var Comparison[]|Value[] $mappedExpressions */
82
        $mappedExpressions = $mapped->getExpressionList();
83
84
        static::assertCount(2, $mappedExpressions);
85
86
        static::assertInstanceOf('\Doctrine\Common\Collections\Expr\Comparison', $mappedExpressions[0]);
87
        static::assertInstanceOf('\Doctrine\Common\Collections\Expr\Comparison', $mappedExpressions[1]);
88
89
        static::assertEquals('column', $mappedExpressions[0]->getField());
0 ignored issues
show
Bug introduced by
The method getField does only exist in Doctrine\Common\Collections\Expr\Comparison, but not in Doctrine\Common\Collections\Expr\Value.

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...
90
        static::assertEquals('column', $mappedExpressions[1]->getField());
91
92
        static::assertEquals($gtExpr->getOperator(), $mappedExpressions[0]->getOperator());
0 ignored issues
show
Bug introduced by
The method getOperator does only exist in Doctrine\Common\Collections\Expr\Comparison, but not in Doctrine\Common\Collections\Expr\Value.

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...
93
        static::assertEquals($ltExpr->getOperator(), $mappedExpressions[1]->getOperator());
94
95
        static::assertEquals($gtExpr->getValue(), $mappedExpressions[0]->getValue());
96
        static::assertEquals($ltExpr->getValue(), $mappedExpressions[1]->getValue());
97
    }
98
}
99