Failed Conditions
Push — master ( ddccd4...8ad3df )
by Marco
13:34
created

testNewEntitiesFoundThroughRelationships()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Doctrine\Tests\ORM;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\NotifyPropertyChanged;
7
use Doctrine\Common\PropertyChangedListener;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Doctrine\ORM\ORMInvalidArgumentException;
10
use Doctrine\ORM\UnitOfWork;
11
use Doctrine\ORM\ORMException;
12
use Doctrine\Tests\Mocks\ConnectionMock;
13
use Doctrine\Tests\Mocks\DriverMock;
14
use Doctrine\Tests\Mocks\EntityManagerMock;
15
use Doctrine\Tests\Mocks\EntityPersisterMock;
16
use Doctrine\Tests\Mocks\UnitOfWorkMock;
17
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
18
use Doctrine\Tests\Models\CMS\CmsUser;
19
use Doctrine\Tests\Models\Forum\ForumAvatar;
20
use Doctrine\Tests\Models\Forum\ForumUser;
21
use Doctrine\Tests\Models\GeoNames\City;
22
use Doctrine\Tests\Models\GeoNames\Country;
23
use Doctrine\Tests\OrmTestCase;
24
use PHPUnit\Framework\TestCase;
25
use stdClass;
26
27
/**
28
 * @covers \Doctrine\ORM\ORMInvalidArgumentException
29
 */
30
class ORMInvalidArgumentExceptionTest extends TestCase
31
{
32
    /**
33
     * @dataProvider invalidEntityNames
34
     *
35
     * @param mixed  $value
36
     * @param string $expectedMessage
37
     *
38
     * @return void
39
     */
40
    public function testInvalidEntityName($value, $expectedMessage)
41
    {
42
        $exception = ORMInvalidArgumentException::invalidEntityName($value);
43
44
        self::assertInstanceOf(ORMInvalidArgumentException::class, $exception);
45
        self::assertSame($expectedMessage, $exception->getMessage());
46
    }
47
48
    /**
49
     * @return string[][]
50
     */
51
    public function invalidEntityNames()
52
    {
53
        return [
54
            [null, 'Entity name must be a string, NULL given'],
55
            [true, 'Entity name must be a string, boolean given'],
56
            [123, 'Entity name must be a string, integer given'],
57
            [123.45, 'Entity name must be a string, double given'],
58
            [new \stdClass(), 'Entity name must be a string, object given'],
59
        ];
60
    }
61
62
    /**
63
     * @dataProvider newEntitiesFoundThroughRelationshipsErrorMessages
64
     */
65
    public function testNewEntitiesFoundThroughRelationships(array $newEntities, string $expectedMessage) : void
66
    {
67
        $exception = ORMInvalidArgumentException::newEntitiesFoundThroughRelationships($newEntities);
68
69
        self::assertInstanceOf(ORMInvalidArgumentException::class, $exception);
70
        self::assertSame($expectedMessage, $exception->getMessage());
71
    }
72
73
    public function newEntitiesFoundThroughRelationshipsErrorMessages() : array
74
    {
75
        $stringEntity3 = uniqid('entity3', true);
76
        $entity1       = new \stdClass();
77
        $entity2       = new \stdClass();
78
        $entity3       = $this->getMockBuilder(\stdClass::class)->setMethods(['__toString'])->getMock();
79
        $association1  = [
80
            'sourceEntity' => 'foo1',
81
            'fieldName'    => 'bar1',
82
            'targetEntity' => 'baz1',
83
        ];
84
        $association2  = [
85
            'sourceEntity' => 'foo2',
86
            'fieldName'    => 'bar2',
87
            'targetEntity' => 'baz2',
88
        ];
89
        $association3  = [
90
            'sourceEntity' => 'foo3',
91
            'fieldName'    => 'bar3',
92
            'targetEntity' => 'baz3',
93
        ];
94
95
        $entity3->expects(self::any())->method('__toString')->willReturn($stringEntity3);
96
97
        return [
98
            'one entity found' => [
99
                [
100
                    [
101
                        $association1,
102
                        $entity1,
103
                    ],
104
                ],
105
                'A new entity was found through the relationship \'foo1#bar1\' that was not configured to cascade '
106
                . 'persist operations for entity: stdClass@' . spl_object_hash($entity1)
107
                . '. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity '
108
                . 'or configure cascade persist this association in the mapping for example '
109
                . '@ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem '
110
                . 'implement \'baz1#__toString()\' to get a clue.',
111
            ],
112
            'two entities found' => [
113
                [
114
                    [
115
                        $association1,
116
                        $entity1,
117
                    ],
118
                    [
119
                        $association2,
120
                        $entity2,
121
                    ],
122
                ],
123
                'Multiple non-persisted new entities were found through the given association graph:' . "\n\n"
124
                . ' * A new entity was found through the relationship \'foo1#bar1\' that was not configured to '
125
                . 'cascade persist operations for entity: stdClass@' . spl_object_hash($entity1) . '. '
126
                . 'To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity '
127
                . 'or configure cascade persist this association in the mapping for example '
128
                . '@ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem '
129
                . 'implement \'baz1#__toString()\' to get a clue.' . "\n"
130
                . ' * A new entity was found through the relationship \'foo2#bar2\' that was not configured to '
131
                . 'cascade persist operations for entity: stdClass@' . spl_object_hash($entity2) . '. To solve '
132
                . 'this issue: Either explicitly call EntityManager#persist() on this unknown entity or '
133
                . 'configure cascade persist this association in the mapping for example '
134
                . '@ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem '
135
                . 'implement \'baz2#__toString()\' to get a clue.'
136
            ],
137
            'two entities found, one is stringable' => [
138
                [
139
                    [
140
                        $association3,
141
                        $entity3,
142
                    ],
143
                ],
144
                'A new entity was found through the relationship \'foo3#bar3\' that was not configured to cascade '
145
                . 'persist operations for entity: ' . $stringEntity3
146
                . '. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity '
147
                . 'or configure cascade persist this association in the mapping for example '
148
                . '@ManyToOne(..,cascade={"persist"}).',
149
            ],
150
        ];
151
    }
152
}
153