Completed
Pull Request — master (#602)
by Tom
07:01
created

MappingCollectorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 108
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModuleTest\Collector;
6
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
8
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
9
use DoctrineORMModule\Collector\MappingCollector;
10
use Laminas\Mvc\MvcEvent;
11
use PHPUnit\Framework\TestCase;
12
use PHPUnit_Framework_MockObject_MockObject;
13
use function assert;
14
use function serialize;
15
use function unserialize;
16
17
/**
18
 * Tests for the MappingCollector
19
 */
20
class MappingCollectorTest extends TestCase
21
{
22
    /** @var ClassMetadataFactory|PHPUnit_Framework_MockObject_MockObject */
23
    protected $metadataFactory;
24
25
    protected MappingCollector $collector;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
26
27
    /**
28
     * @covers \DoctrineORMModule\Collector\MappingCollector::__construct
29
     */
30
    protected function setUp() : void
31
    {
32
        parent::setUp();
33
34
        $this->metadataFactory = $this->createMock(ClassMetadataFactory::class);
35
        $this->collector       = new MappingCollector($this->metadataFactory, 'test-collector');
36
    }
37
38
    /**
39
     * @covers \DoctrineORMModule\Collector\MappingCollector::getName
40
     */
41
    public function testGetName() : void
42
    {
43
        $this->assertSame('test-collector', $this->collector->getName());
44
    }
45
46
    /**
47
     * @covers \DoctrineORMModule\Collector\MappingCollector::getPriority
48
     */
49
    public function testGetPriority() : void
50
    {
51
        $this->assertIsInt($this->collector->getPriority());
52
    }
53
54
    /**
55
     * @covers \DoctrineORMModule\Collector\MappingCollector::collect
56
     * @covers \DoctrineORMModule\Collector\MappingCollector::getClasses
57
     */
58
    public function testCollect() : void
59
    {
60
        $m1 = $this->createMock(ClassMetadata::class);
61
        $m1->expects($this->any())->method('getName')->will($this->returnValue('M1'));
62
        $m2 = $this->createMock(ClassMetadata::class);
63
        $m2->expects($this->any())->method('getName')->will($this->returnValue('M2'));
64
        $this
65
            ->metadataFactory
66
            ->expects($this->any())
67
            ->method('getAllMetadata')
68
            ->will($this->returnValue([$m1, $m2]));
69
70
        $this->collector->collect($this->createMock(MvcEvent::class));
71
72
        $classes = $this->collector->getClasses();
73
74
        $this->assertCount(2, $classes);
75
        $this->assertSame($classes['M1'], $m1);
76
        $this->assertSame($classes['M2'], $m2);
77
    }
78
79
    /**
80
     * @covers \DoctrineORMModule\Collector\MappingCollector::canHide
81
     */
82
    public function testCanHide() : void
83
    {
84
        $this->assertTrue($this->collector->canHide());
85
86
        $m1 = $this->createMock(ClassMetadata::class);
87
        $m1->expects($this->any())->method('getName')->will($this->returnValue('M1'));
88
        $this->metadataFactory->expects($this->any())->method('getAllMetadata')->will($this->returnValue([$m1]));
89
90
        $this->collector->collect($this->createMock(MvcEvent::class));
91
92
        $this->assertFalse($this->collector->canHide());
93
    }
94
95
    /**
96
     * @covers \DoctrineORMModule\Collector\MappingCollector::serialize
97
     * @covers \DoctrineORMModule\Collector\MappingCollector::unserialize
98
     * @covers \DoctrineORMModule\Collector\MappingCollector::collect
99
     */
100
    public function testSerializeUnserializeAndCollectWithNoMetadataFactory() : void
101
    {
102
        $m1 = $this->createMock(ClassMetadata::class);
103
        $m1->expects($this->any())->method('getName')->will($this->returnValue('M1'));
104
        $this->metadataFactory->expects($this->any())->method('getAllMetadata')->will($this->returnValue([$m1]));
105
106
        $this->collector->collect($this->createMock(MvcEvent::class));
107
108
        $collector = unserialize(serialize($this->collector));
109
        assert($collector instanceof MappingCollector);
110
111
        $classes = $collector->getClasses();
112
        $this->assertCount(1, $classes);
113
        $this->assertEquals($m1, $classes['M1']);
114
        $this->assertSame('test-collector', $collector->getName());
115
116
        $collector->collect($this->createMock(MvcEvent::class));
117
118
        $classes = $collector->getClasses();
119
        $this->assertCount(1, $classes);
120
        $this->assertEquals($m1, $classes['M1']);
121
    }
122
}
123