Completed
Pull Request — master (#482)
by Bruno
05:25
created

getAssociationTargetClassMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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\Yuml;
21
22
use DoctrineORMModule\Yuml\MetadataGrapher;
23
use PHPUnit_Framework_TestCase;
24
25
/**
26
 * Tests for the metadata to string converter
27
 *
28
 * @license MIT
29
 * @link    http://www.doctrine-project.org/
30
 * @author  Marco Pivetta <[email protected]>
31
 */
32
class MetadataGrapherTest extends PHPUnit_Framework_TestCase
33
{
34
    /**
35
     * @var MetadataGrapher
36
     */
37
    protected $grapher;
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function setUp()
43
    {
44
        parent::setUp();
45
46
        $this->grapher = new MetadataGrapher();
47
    }
48
49
    /**
50
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
51
     */
52
    public function testDrawSimpleEntity()
53
    {
54
        $class = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
55
        $class->expects($this->any())->method('getName')->will($this->returnValue('Simple\\Entity'));
56
        $class->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
57
        $class->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
58
59
        $this->assertSame('[Simple.Entity]', $this->grapher->generateFromMetadata(array($class)));
60
    }
61
62
    /**
63
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
64
     */
65
    public function testDrawSimpleEntityWithFields()
66
    {
67
        $class = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
68
        $class->expects($this->any())->method('getName')->will($this->returnValue('Simple\\Entity'));
69
        $class->expects($this->any())->method('getFieldNames')->will($this->returnValue(array('a', 'b', 'c')));
70
        $class->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
71
        $class->expects($this->any())->method('isIdentifier')->will(
72
            $this->returnCallback(
73
                function ($field) {
74
                    return $field === 'a';
75
                }
76
            )
77
        );
78
79
        $this->assertSame('[Simple.Entity|+a;b;c]', $this->grapher->generateFromMetadata(array($class)));
80
    }
81
82
    /**
83
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
84
     */
85
    public function testDrawOneToOneUniDirectionalAssociation()
86
    {
87
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
88
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
89
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('b')));
90
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('B'));
91
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
92
            'isOwningSide' => true,
93
            'mappedBy'      => null,
94
            'inversedBy'    =>null
95
        )));
96
97
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
98
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(false));
99
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
100
101
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
102
        $class2->expects($this->any())->method('getName')->will($this->returnValue('B'));
103
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
104
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
105
106
        $this->assertSame('[A]-b 1>[B]', $this->grapher->generateFromMetadata(array($class1, $class2)));
107
    }
108
109
    /**
110
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
111
     */
112
    public function testDrawOneToOneBiDirectionalAssociation()
113
    {
114
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
115
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
116
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('b')));
117
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('B'));
118
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
119
            'isOwningSide' => true,
120
            'mappedBy'      => null,
121
            'inversedBy'    =>'a'
122
        )));
123
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
124
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(false));
125
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
126
127
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
128
        $class2->expects($this->any())->method('getName')->will($this->returnValue('B'));
129
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('a')));
130
        $class2->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('A'));
131
        $class2->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
132
            'isOwningSide' => false,
133
            'mappedBy'      => 'b',
134
            'inversedBy'    => null
135
        )));
136
        $class2->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(true));
137
        $class2->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(false));
138
        $class2->expects($this->any())->method('getAssociationMappedByTargetField')->will($this->returnValue('b'));
139
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
140
141
        $this->assertSame('[A]<>a 1-b 1>[B]', $this->grapher->generateFromMetadata(array($class1, $class2)));
142
    }
143
144
    /**
145
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
146
     */
147
    public function testDrawOneToOneBiDirectionalInverseAssociation()
148
    {
149
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
150
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
151
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('b')));
152
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('B'));
153
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
154
            'isOwningSide' => false,
155
            'mappedBy'      => 'a',
156
            'inversedBy'    => null
157
        )));
158
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(true));
159
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(false));
160
        $class1->expects($this->any())->method('getAssociationMappedByTargetField')->will($this->returnValue('a'));
161
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
162
163
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
164
        $class2->expects($this->any())->method('getName')->will($this->returnValue('B'));
165
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('a')));
166
        $class2->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('A'));
167
        $class2->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
168
            'isOwningSide' => true,
169
            'mappedBy'      => null,
170
            'inversedBy'    => 'a'
171
        )));
172
        $class2->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
173
        $class2->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(false));
174
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
175
        $expected = "[A]<a 1-b 1<>[B]";
176
        $this->assertSame($expected, $this->grapher->generateFromMetadata(array($class1, $class2)));
177
    }
178
179
    /**
180
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
181
     */
182
    public function testDrawOneToManyBiDirectionalAssociation()
183
    {
184
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
185
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
186
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('b')));
187
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('B'));
188
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
189
            'isOwningSide' => true,
190
            'mappedBy'      => null,
191
            'inversedBy'    => 'a'
192
        )));
193
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
194
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
195
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
196
197
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
198
        $class2->expects($this->any())->method('getName')->will($this->returnValue('B'));
199
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('a')));
200
        $class2->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('A'));
201
        $class2->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
202
            'isOwningSide' => false,
203
            'mappedBy'      => 'b',
204
            'inversedBy'    => null
205
        )));
206
        $class2->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(true));
207
        $class2->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(false));
208
        $class2->expects($this->any())->method('getAssociationMappedByTargetField')->will($this->returnValue('b'));
209
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
210
211
        $this->assertSame('[A]<>a 1-b *>[B]', $this->grapher->generateFromMetadata(array($class1, $class2)));
212
    }
213
214
    /**
215
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
216
     */
217
    public function testDrawOneToManyBiDirectionalInverseAssociation()
218
    {
219
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
220
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
221
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('b')));
222
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('B'));
223
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
224
            'isOwningSide' => true,
225
            'mappedBy'      => null,
226
            'inversedBy'    => 'a'
227
        )));
228
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
229
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(false));
230
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
231
232
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
233
        $class2->expects($this->any())->method('getName')->will($this->returnValue('B'));
234
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('a')));
235
        $class2->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('A'));
236
        $class2->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
237
            'isOwningSide' => false,
238
            'mappedBy'      => 'b',
239
            'inversedBy'    => null
240
        )));
241
        $class2->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(true));
242
        $class2->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
243
        $class2->expects($this->any())->method('getAssociationMappedByTargetField')->will($this->returnValue('b'));
244
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
245
246
        $this->assertSame('[A]<>a *-b 1>[B]', $this->grapher->generateFromMetadata(array($class1, $class2)));
247
    }
248
249
    /**
250
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
251
     */
252
    public function testDrawManyToManyUniDirectionalAssociation()
253
    {
254
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
255
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
256
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('b')));
257
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('B'));
258
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
259
            'isOwningSide' => true,
260
            'mappedBy'      => null,
261
            'inversedBy'    => null
262
        )));
263
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
264
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
265
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
266
267
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
268
        $class2->expects($this->any())->method('getName')->will($this->returnValue('B'));
269
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
270
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
271
272
        $this->assertSame('[A]-b *>[B]', $this->grapher->generateFromMetadata(array($class1, $class2)));
273
    }
274
275
    /**
276
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
277
     */
278
    public function testDrawManyToManyUniDirectionalInverseAssociation()
279
    {
280
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
281
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
282
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
283
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
284
285
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
286
        $class2->expects($this->any())->method('getName')->will($this->returnValue('B'));
287
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('a')));
288
        $class2->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('A'));
289
        $class2->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(null));
290
        $class2->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
291
            'isOwningSide' => true,
292
            'mappedBy'      => null,
293
            'inversedBy'    => null
294
        )));
295
        $class2->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
296
        $class2->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
297
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
298
299
        $this->assertSame('[A],[B]-a *>[A]', $this->grapher->generateFromMetadata(array($class1, $class2)));
300
    }
301
302
    /**
303
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
304
     */
305
    public function testDrawManyToManyBiDirectionalAssociation()
306
    {
307
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
308
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
309
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('b')));
310
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('B'));
311
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
312
            'isOwningSide' => true,
313
            'mappedBy'      => null,
314
            'inversedBy'    => 'a'
315
        )));
316
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
317
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
318
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
319
320
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
321
        $class2->expects($this->any())->method('getName')->will($this->returnValue('B'));
322
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('a')));
323
        $class2->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('A'));
324
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
325
            'isOwningSide' => false,
326
            'mappedBy'      => 'a',
327
            'inversedBy'    => null
328
        )));
329
        $class2->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(true));
330
        $class2->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
331
        $class2->expects($this->any())->method('getAssociationMappedByTargetField')->will($this->returnValue('b'));
332
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
333
334
        $this->assertSame('[A]<>a *-b *>[B]', $this->grapher->generateFromMetadata(array($class1, $class2)));
335
    }
336
337
    /**
338
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
339
     */
340
    public function testDrawManyToManyBiDirectionalInverseAssociation()
341
    {
342
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
343
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
344
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('b')));
345
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('B'));
346
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
347
            'isOwningSide' => false,
348
            'mappedBy'      => 'a',
349
            'inversedBy'    => null
350
        )));
351
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(true));
352
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
353
        $class1->expects($this->any())->method('getAssociationMappedByTargetField')->will($this->returnValue('a'));
354
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
355
356
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
357
        $class2->expects($this->any())->method('getName')->will($this->returnValue('B'));
358
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('a')));
359
        $class2->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('A'));
360
        $class2->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
361
            'isOwningSide' => true,
362
            'mappedBy'      => null,
363
            'inversedBy'    => 'b'
364
        )));
365
        $class2->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
366
        $class2->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
367
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
368
369
        $this->assertSame('[A]<a *-b *<>[B]', $this->grapher->generateFromMetadata(array($class1, $class2)));
370
    }
371
372
    /**
373
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
374
     */
375
    public function testDrawManyToManyAssociationWithoutKnownInverseSide()
376
    {
377
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
378
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
379
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('b')));
380
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('B'));
381
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
382
            'isOwningSide' => true,
383
            'mappedBy'      => null,
384
            'inversedBy'    => null
385
        )));
386
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
387
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
388
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
389
390
        $this->assertSame('[A]<>-b *>[B]', $this->grapher->generateFromMetadata(array($class1)));
391
    }
392
393
    /**
394
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
395
     */
396
    public function testDrawInheritance()
397
    {
398
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
399
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
400
        $child  = get_class($this->getMock('stdClass'));
401
        $class1->expects($this->any())->method('getName')->will($this->returnValue('stdClass'));
402
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
403
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
404
        $class2->expects($this->any())->method('getName')->will($this->returnValue($child));
405
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
406
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
407
408
        $this->assertSame(
409
            '[stdClass]^[' . str_replace('\\', '.', $child) . ']',
410
            $this->grapher->generateFromMetadata(array($class2, $class1))
411
        );
412
    }
413
414
    /**
415
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
416
     */
417
    public function testDrawInheritedFields()
418
    {
419
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
420
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
421
        $child  = get_class($this->getMock('stdClass'));
422
423
        $class1->expects($this->any())->method('getName')->will($this->returnValue('stdClass'));
424
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
425
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array('inherited')));
426
427
        $class2->expects($this->any())->method('getName')->will($this->returnValue($child));
428
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
429
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array('inherited', 'field2')));
430
431
        $this->assertSame(
432
            '[stdClass|inherited]^[' . str_replace('\\', '.', $child) . '|field2]',
433
            $this->grapher->generateFromMetadata(array($class2, $class1))
434
        );
435
    }
436
437
    /**
438
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
439
     */
440
    public function testDrawInheritedAssociations()
441
    {
442
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
443
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
444
        $class3 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
445
        $class4 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
446
        $child  = get_class($this->getMock('stdClass'));
447
448
        $class1->expects($this->any())->method('getName')->will($this->returnValue('stdClass'));
449
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('a')));
450
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('A'));
451
452
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
453
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
454
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
455
456
        $class2->expects($this->any())->method('getName')->will($this->returnValue($child));
457
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('a', 'b')));
458
        $class2
459
            ->expects($this->any())
460
            ->method('getAssociationTargetClass')
461
            ->will(
462
                $this->returnCallback(
463
                    function ($assoc) {
464
                        return strtoupper($assoc);
465
                    }
466
                )
467
            );
468
        $class2->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
469
        $class2->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
470
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
471
472
        $class3->expects($this->any())->method('getName')->will($this->returnValue('A'));
473
        $class3->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
474
        $class3->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
475
476
        $class4->expects($this->any())->method('getName')->will($this->returnValue('B'));
477
        $class4->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
478
        $class4->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
479
480
        $childName = str_replace('\\', '.', $child);
481
        $this->assertSame(
482
            '[stdClass]<>-a *>[A],[stdClass]^[' . $childName . '],[' . $childName . ']<>-b *>[B]',
483
            $this->grapher->generateFromMetadata(array($class1, $class2))
484
        );
485
    }
486
487
    /**
488
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
489
     * @dataProvider injectMultipleRelationsWithBothBiAndMonoDirectional
490
     */
491
    public function testDrawMultipleClassRelatedBothBiAndMonoDirectional($class1, $class2, $class3, $expected)
492
    {
493
        $this->assertSame(
494
            $expected,
495
            $this->grapher->generateFromMetadata(array($class1, $class2,$class3))
496
        );
497
    }
498
499
    /**
500
     * dataProvider to inject classes in every possible order into the test
501
     *     testDrawMultipleClassRelatedBothBiAndMonoDirectional
502
     *
503
     * @return array
504
     */
505
    public function injectMultipleRelationsWithBothBiAndMonoDirectional()
506
    {
507
        $class1 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
508
        $class1->expects($this->any())->method('getName')->will($this->returnValue('A'));
509
        $class1->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('c')));
510
        $class1->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('C'));
511
        $class1->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
512
            'isOwningSide' => true,
513
            'mappedBy'      => null,
514
            'inversedBy'    => null
515
        )));
516
        $class1->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
517
        $class1->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(false));
518
        $class1->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
519
520
521
        $class2 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
522
        $class2->expects($this->any())->method('getName')->will($this->returnValue('B'));
523
        $class2->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('c')));
524
        $class2->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('C'));
525
        $class2->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
526
            'isOwningSide' => true,
527
            'mappedBy'      => null,
528
            'inversedBy'    => 'b'
529
        )));
530
        $class2->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
531
        $class2->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(false));
532
        $class2->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
533
534
535
        $class3 = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
536
        $class3->expects($this->any())->method('getName')->will($this->returnValue('C'));
537
        $class3->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('b')));
538
        $class3->expects($this->any())->method('getAssociationTargetClass')->will($this->returnValue('B'));
539
        $class3->expects($this->any())->method('getAssociationMapping')->will($this->returnValue(array(
540
            'isOwningSide' => false,
541
            'mappedBy'      => 'c',
542
            'inversedBy'    => null
543
        )));
544
545
546
        $class3->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(true));
547
        $class3->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
548
        $class3->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
549
550
        return array(
551
            array($class1, $class2, $class3, '[A]-c 1>[C],[B]<>b *-c 1>[C]'),
552
            array($class1, $class3, $class2, '[A]-c 1>[C],[C]<c 1-b *<>[B]'),
553
            array($class2, $class1, $class3, '[B]<>b *-c 1>[C],[A]-c 1>[C]'),
554
            array($class2, $class3, $class1, '[B]<>b *-c 1>[C],[A]-c 1>[C]'),
555
            array($class3, $class1, $class2, '[C]<c 1-b *<>[B],[A]-c 1>[C]'),
556
            array($class3, $class2, $class1, '[C]<c 1-b *<>[B],[A]-c 1>[C]')
557
        );
558
    }
559
560
    /**
561
     * To mock getAssociationTargetClass method with args
562
     *
563
     * @param  string $a
564
     * @return string
565
     */
566
    public function getAssociationTargetClassMock($a)
567
    {
568
        return strtoupper($a);
569
    }
570
571
    /**
572
     * @return array
573
     */
574
    public function injectTwoClassesWithTwoDifferentRelationsOneToManyBidirectionnal()
575
    {
576
        $classAB = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
577
        $classAB->expects($this->any())->method('getName')->will($this->returnValue('AB'));
578
        $classAB->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('c','d')));
579
        $classAB
580
            ->expects($this->any())
581
            ->method('getAssociationTargetClass')
582
            ->with($this->logicalOr($this->equalTo('c'), $this->equalTo('d')))
583
            ->will($this->returnCallback(array($this, 'getAssociationClassMock')));
584
585
        $classAB
586
            ->expects($this->any())
587
            ->method('getAssociationMapping')
588
            ->with($this->logicalOr(
589
                $this->equalTo('c'),
590
                $this->equalTo('d')
591
            ))
592
            ->will($this->returnCallback(array($this, 'getAssociationMappingMock')));
593
594
595
        $classAB->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(true));
596
        $classAB->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(true));
597
        $classAB->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
598
599
        $classCD = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
600
        $classCD->expects($this->any())->method('getName')->will($this->returnValue('CD'));
601
        $classCD->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array('a','b')));
602
        $classCD
603
            ->expects($this->any())
604
            ->method('getAssociationTargetClass')
605
            ->with($this->logicalOr($this->equalTo('a'), $this->equalTo('b')))
606
            ->will($this->returnCallback(array($this, 'getAssociationClassMock')));
607
        $classCD
608
            ->expects($this->any())
609
            ->method('getAssociationMapping')
610
            ->with($this->logicalOr(
611
                $this->equalTo('a'),
612
                $this->equalTo('b')
613
            ))
614
            ->will($this->returnCallback(array($this, 'getAssociationMappingMock')));
615
        $classCD->expects($this->any())->method('isAssociationInverseSide')->will($this->returnValue(false));
616
        $classCD->expects($this->any())->method('isCollectionValuedAssociation')->will($this->returnValue(false));
617
        $classCD->expects($this->any())->method('getFieldNames')->will($this->returnValue(array()));
618
619
        return array(
620
            array($classAB, $classCD, "[AB]<a 1-c *<>[CD],[AB]<b 1-d *<>[CD]"),
621
            array($classCD, $classAB, "[CD]<>c *-a 1>[AB],[CD]<>d *-b 1>[AB]"),
622
        );
623
    }
624
625
    /**
626
     * @covers \DoctrineORMModule\Yuml\MetadataGrapher
627
     * @dataProvider injectTwoClassesWithTwoDifferentRelationsOneToManyBidirectionnal
628
     */
629
    public function testMultipleRelationsManyToOneBeetweenTwoSameClasses($class1, $class2, $expected)
630
    {
631
        $this->assertSame(
632
            $expected,
633
            $this->grapher->generateFromMetadata(array($class1, $class2))
634
        );
635
    }
636
637
    public function getAssociationClassMock($a)
638
    {
639
        switch ($a) {
640
            case 'a':
641
            case 'b':
642
                return 'AB';
643
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
644
            case 'c':
645
            case 'd':
646
                return 'CD';
647
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
648
        }
649
        return false;
650
    }
651
652
    public function getAssociationMappingMock($a)
653
    {
654
        switch ($a) {
655
            case 'a':
656
                $return = array(
657
                    'isOwningSide' => true,
658
                    'mappedBy'      => null,
659
                    'inversedBy'    => 'c'
660
                );
661
                break;
662
            case 'b':
663
                $return = array(
664
                    'isOwningSide' => true,
665
                    'mappedBy'      => null,
666
                    'inversedBy'    => 'd'
667
                );
668
                break;
669
            case 'c':
670
                $return = array(
671
                    'isOwningSide' => false,
672
                    'mappedBy'      => 'a',
673
                    'inversedBy'    => null
674
                );
675
                break;
676
            case 'd':
677
                $return = array(
678
                    'isOwningSide' => false,
679
                    'mappedBy'      => 'b',
680
                    'inversedBy'    => null
681
                );
682
                break;
683
            default:
684
                $return = false;
685
        }
686
        return $return;
687
    }
688
}
689