Completed
Pull Request — master (#5800)
by Herberto
10:37
created

CollectionFactoryTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 77
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testCollectionFactoryInstantiatesPersistentCollectionIfCollectionTypeIsNotSet() 0 10 1
A testCollectionFactoryInstantiatesTheRequiredCollection() 0 12 1
A testCollectionFactoryThrowsExceptionIfCollectionTypeIsNotPersistentCollection() 0 10 1
1
<?php
2
namespace Doctrine\Tests\ORM\Mapping\Factory;
3
4
use Doctrine\Common\Collections\ArrayCollection;
5
use Doctrine\ORM\Mapping\ClassMetadata;
6
use Doctrine\ORM\Mapping\Factory\CollectionFactory;
7
use Doctrine\ORM\PersistentCollection;
8
use Doctrine\Tests\ORM\Persisters\Collection\ExtensionOfPersistentCollection;
9
use Doctrine\Tests\OrmTestCase;
10
11
class CollectionFactoryTest extends OrmTestCase
12
{
13
    /**
14
     * @var \Doctrine\ORM\EntityManager
15
     */
16
    protected $_em;
17
18
    /**
19
     * @var CollectionFactory
20
     */
21
    protected $collectionFactory;
22
23
    /**
24
     * @var ClassMetadata
25
     */
26
    protected $classMetadata;
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    protected function setUp()
32
    {
33
        parent::setUp();
34
35
        $this->_em = $this->_getTestEntityManager();
36
37
        $this->classMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\GeoNames\Admin1AlternateName');
38
39
        $this->collectionFactory = new CollectionFactory();
40
    }
41
42
    /**
43
     * @group custom-collections
44
     */
45
    public function testCollectionFactoryInstantiatesPersistentCollectionIfCollectionTypeIsNotSet()
46
    {
47
        $result = $this->collectionFactory->create(
48
            $this->_em,
49
            $this->classMetadata,
50
            new ArrayCollection([])
51
        );
52
53
        $this->assertInstanceOf(PersistentCollection::class, $result);
54
    }
55
56
    /**
57
     * @group custom-collections
58
     */
59
    public function testCollectionFactoryInstantiatesTheRequiredCollection()
60
    {
61
        $this->classMetadata->setCustomCollectionClass(ExtensionOfPersistentCollection::class);
62
63
        $result = $this->collectionFactory->create(
64
            $this->_em,
65
            $this->classMetadata,
66
            new ArrayCollection([])
67
        );
68
69
        $this->assertInstanceOf(ExtensionOfPersistentCollection::class, $result);
70
    }
71
72
    /**
73
     * @expectedException \Doctrine\ORM\ORMInvalidArgumentException
74
     *
75
     * @group custom-collections
76
     */
77
    public function testCollectionFactoryThrowsExceptionIfCollectionTypeIsNotPersistentCollection()
78
    {
79
        $this->classMetadata->setCustomCollectionClass(ArrayCollection::class);
80
81
        $this->collectionFactory->create(
82
            $this->_em,
83
            $this->classMetadata,
84
            new ArrayCollection([])
85
        );
86
    }
87
}
88