Completed
Pull Request — master (#476)
by Bruno
14:24
created

MappingCollectorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 108
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testGetName() 0 4 1
A testGetPriority() 0 4 1
A testCollect() 0 20 1
A testCanHide() 0 12 1
A testSerializeUnserializeAndCollectWithNoMetadataFactory() 0 22 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModuleTest\Collector;
21
22
use PHPUnit_Framework_TestCase;
23
use DoctrineORMModule\Collector\MappingCollector;
24
25
/**
26
 * Tests for the MappingCollector
27
 *
28
 * @author  Marco Pivetta <[email protected]>
29
 * @license MIT
30
 */
31
class MappingCollectorTest extends PHPUnit_Framework_TestCase
32
{
33
    /**
34
     * @var \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory|\PHPUnit_Framework_MockObject_MockObject
35
     */
36
    protected $metadataFactory;
37
38
    /**
39
     * @var MappingCollector
40
     */
41
    protected $collector;
42
43
    /**
44
     * @covers \DoctrineORMModule\Collector\MappingCollector::__construct
45
     */
46
    public function setUp()
47
    {
48
        parent::setUp();
49
50
        $this->metadataFactory = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadataFactory');
51
        $this->collector       = new MappingCollector($this->metadataFactory, 'test-collector');
52
    }
53
54
    /**
55
     * @covers \DoctrineORMModule\Collector\MappingCollector::getName
56
     */
57
    public function testGetName()
58
    {
59
        $this->assertSame('test-collector', $this->collector->getName());
60
    }
61
62
    /**
63
     * @covers \DoctrineORMModule\Collector\MappingCollector::getPriority
64
     */
65
    public function testGetPriority()
66
    {
67
        $this->assertInternalType('int', $this->collector->getPriority());
68
    }
69
70
    /**
71
     * @covers \DoctrineORMModule\Collector\MappingCollector::collect
72
     * @covers \DoctrineORMModule\Collector\MappingCollector::getClasses
73
     */
74
    public function testCollect()
75
    {
76
        $m1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
77
        $m1->expects($this->any())->method('getName')->will($this->returnValue('M1'));
78
        $m2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
79
        $m2->expects($this->any())->method('getName')->will($this->returnValue('M2'));
80
        $this
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persiste...ng\ClassMetadataFactory.

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...
81
            ->metadataFactory
82
            ->expects($this->any())
83
            ->method('getAllMetadata')
84
            ->will($this->returnValue(array($m1, $m2)));
85
86
        $this->collector->collect($this->getMock('Zend\\Mvc\\MvcEvent'));
87
88
        $classes = $this->collector->getClasses();
89
90
        $this->assertCount(2, $classes);
91
        $this->assertSame($classes['M1'], $m1);
92
        $this->assertSame($classes['M2'], $m2);
93
    }
94
95
    /**
96
     * @covers \DoctrineORMModule\Collector\MappingCollector::canHide
97
     */
98
    public function testCanHide()
99
    {
100
        $this->assertTrue($this->collector->canHide());
101
102
        $m1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
103
        $m1->expects($this->any())->method('getName')->will($this->returnValue('M1'));
104
        $this->metadataFactory->expects($this->any())->method('getAllMetadata')->will($this->returnValue(array($m1)));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persiste...ng\ClassMetadataFactory.

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...
105
106
        $this->collector->collect($this->getMock('Zend\\Mvc\\MvcEvent'));
107
108
        $this->assertFalse($this->collector->canHide());
109
    }
110
111
    /**
112
     * @covers \DoctrineORMModule\Collector\MappingCollector::serialize
113
     * @covers \DoctrineORMModule\Collector\MappingCollector::unserialize
114
     * @covers \DoctrineORMModule\Collector\MappingCollector::collect
115
     */
116
    public function testSerializeUnserializeAndCollectWithNoMetadataFactory()
117
    {
118
        $m1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
119
        $m1->expects($this->any())->method('getName')->will($this->returnValue('M1'));
120
        $this->metadataFactory->expects($this->any())->method('getAllMetadata')->will($this->returnValue(array($m1)));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persiste...ng\ClassMetadataFactory.

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...
121
122
        $this->collector->collect($this->getMock('Zend\\Mvc\\MvcEvent'));
123
124
        /* @var $collector MappingCollector */
125
        $collector = unserialize(serialize($this->collector));
126
127
        $classes = $collector->getClasses();
128
        $this->assertCount(1, $classes);
129
        $this->assertEquals($m1, $classes['M1']);
130
        $this->assertSame('test-collector', $collector->getName());
131
132
        $collector->collect($this->getMock('Zend\\Mvc\\MvcEvent'));
133
134
        $classes = $collector->getClasses();
135
        $this->assertCount(1, $classes);
136
        $this->assertEquals($m1, $classes['M1']);
137
    }
138
}
139