1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineORMModuleTest\Collector; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use DoctrineORMModule\Collector\MappingCollector; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Tests for the MappingCollector |
10
|
|
|
* |
11
|
|
|
* @author Marco Pivetta <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
*/ |
14
|
|
|
class MappingCollectorTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory|\PHPUnit_Framework_MockObject_MockObject |
18
|
|
|
*/ |
19
|
|
|
protected $metadataFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var MappingCollector |
23
|
|
|
*/ |
24
|
|
|
protected $collector; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @covers \DoctrineORMModule\Collector\MappingCollector::__construct |
28
|
|
|
*/ |
29
|
|
|
public function setUp() : void |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$this->metadataFactory = $this->createMock(\Doctrine\Common\Persistence\Mapping\ClassMetadataFactory::class); |
|
|
|
|
34
|
|
|
$this->collector = new MappingCollector($this->metadataFactory, 'test-collector'); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @covers \DoctrineORMModule\Collector\MappingCollector::getName |
39
|
|
|
*/ |
40
|
|
|
public function testGetName() |
41
|
|
|
{ |
42
|
|
|
$this->assertSame('test-collector', $this->collector->getName()); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @covers \DoctrineORMModule\Collector\MappingCollector::getPriority |
47
|
|
|
*/ |
48
|
|
|
public function testGetPriority() |
49
|
|
|
{ |
50
|
|
|
$this->assertIsInt($this->collector->getPriority()); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @covers \DoctrineORMModule\Collector\MappingCollector::collect |
55
|
|
|
* @covers \DoctrineORMModule\Collector\MappingCollector::getClasses |
56
|
|
|
*/ |
57
|
|
|
public function testCollect() |
58
|
|
|
{ |
59
|
|
|
$m1 = $this->createMock(\Doctrine\Common\Persistence\Mapping\ClassMetadata::class); |
|
|
|
|
60
|
|
|
$m1->expects($this->any())->method('getName')->will($this->returnValue('M1')); |
|
|
|
|
61
|
|
|
$m2 = $this->createMock(\Doctrine\Common\Persistence\Mapping\ClassMetadata::class); |
|
|
|
|
62
|
|
|
$m2->expects($this->any())->method('getName')->will($this->returnValue('M2')); |
|
|
|
|
63
|
|
|
$this |
|
|
|
|
64
|
|
|
->metadataFactory |
65
|
|
|
->expects($this->any()) |
|
|
|
|
66
|
|
|
->method('getAllMetadata') |
67
|
|
|
->will($this->returnValue([$m1, $m2])); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$this->collector->collect($this->createMock(\Laminas\Mvc\MvcEvent::class)); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$classes = $this->collector->getClasses(); |
72
|
|
|
|
73
|
|
|
$this->assertCount(2, $classes); |
|
|
|
|
74
|
|
|
$this->assertSame($classes['M1'], $m1); |
|
|
|
|
75
|
|
|
$this->assertSame($classes['M2'], $m2); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @covers \DoctrineORMModule\Collector\MappingCollector::canHide |
80
|
|
|
*/ |
81
|
|
|
public function testCanHide() |
82
|
|
|
{ |
83
|
|
|
$this->assertTrue($this->collector->canHide()); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$m1 = $this->createMock(\Doctrine\Common\Persistence\Mapping\ClassMetadata::class); |
|
|
|
|
86
|
|
|
$m1->expects($this->any())->method('getName')->will($this->returnValue('M1')); |
|
|
|
|
87
|
|
|
$this->metadataFactory->expects($this->any())->method('getAllMetadata')->will($this->returnValue([$m1])); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$this->collector->collect($this->createMock(\Laminas\Mvc\MvcEvent::class)); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$this->assertFalse($this->collector->canHide()); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @covers \DoctrineORMModule\Collector\MappingCollector::serialize |
96
|
|
|
* @covers \DoctrineORMModule\Collector\MappingCollector::unserialize |
97
|
|
|
* @covers \DoctrineORMModule\Collector\MappingCollector::collect |
98
|
|
|
*/ |
99
|
|
|
public function testSerializeUnserializeAndCollectWithNoMetadataFactory() |
100
|
|
|
{ |
101
|
|
|
$m1 = $this->createMock(\Doctrine\Common\Persistence\Mapping\ClassMetadata::class); |
|
|
|
|
102
|
|
|
$m1->expects($this->any())->method('getName')->will($this->returnValue('M1')); |
|
|
|
|
103
|
|
|
$this->metadataFactory->expects($this->any())->method('getAllMetadata')->will($this->returnValue([$m1])); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$this->collector->collect($this->createMock(\Laminas\Mvc\MvcEvent::class)); |
|
|
|
|
106
|
|
|
|
107
|
|
|
/* @var $collector MappingCollector */ |
108
|
|
|
$collector = unserialize(serialize($this->collector)); |
109
|
|
|
|
110
|
|
|
$classes = $collector->getClasses(); |
111
|
|
|
$this->assertCount(1, $classes); |
|
|
|
|
112
|
|
|
$this->assertEquals($m1, $classes['M1']); |
|
|
|
|
113
|
|
|
$this->assertSame('test-collector', $collector->getName()); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$collector->collect($this->createMock(\Laminas\Mvc\MvcEvent::class)); |
|
|
|
|
116
|
|
|
|
117
|
|
|
$classes = $collector->getClasses(); |
118
|
|
|
$this->assertCount(1, $classes); |
|
|
|
|
119
|
|
|
$this->assertEquals($m1, $classes['M1']); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: