Passed
Push — master ( fe8259...98aa25 )
by Marco
16:10 queued 03:20
created

testSetDiscriminatorColumnWithoutLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 19
rs 9.4285
c 2
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Tools;
4
5
use Doctrine\ORM\Mapping\ClassMetadata;
6
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
7
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
8
use Doctrine\ORM\Tools\SchemaTool;
9
use Doctrine\ORM\Tools\ToolEvents;
10
use Doctrine\Tests\Models\CMS\CmsAddress;
11
use Doctrine\Tests\Models\CMS\CmsArticle;
12
use Doctrine\Tests\Models\CMS\CmsComment;
13
use Doctrine\Tests\Models\CMS\CmsEmployee;
14
use Doctrine\Tests\Models\CMS\CmsGroup;
15
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
16
use Doctrine\Tests\Models\CMS\CmsUser;
17
use Doctrine\Tests\Models\Forum\ForumAvatar;
18
use Doctrine\Tests\Models\Forum\ForumUser;
19
use Doctrine\Tests\Models\NullDefault\NullDefaultColumn;
20
use Doctrine\Tests\OrmTestCase;
21
22
class SchemaToolTest extends OrmTestCase
23
{
24
    public function testAddUniqueIndexForUniqueFieldAnnotation()
25
    {
26
        $em = $this->_getTestEntityManager();
27
        $schemaTool = new SchemaTool($em);
28
29
        $classes = [
30
            $em->getClassMetadata(CmsAddress::class),
31
            $em->getClassMetadata(CmsArticle::class),
32
            $em->getClassMetadata(CmsComment::class),
33
            $em->getClassMetadata(CmsEmployee::class),
34
            $em->getClassMetadata(CmsGroup::class),
35
            $em->getClassMetadata(CmsPhonenumber::class),
36
            $em->getClassMetadata(CmsUser::class),
37
        ];
38
39
        $schema = $schemaTool->getSchemaFromMetadata($classes);
40
41
        $this->assertTrue($schema->hasTable('cms_users'), "Table cms_users should exist.");
42
        $this->assertTrue($schema->getTable('cms_users')->columnsAreIndexed(['username']), "username column should be indexed.");
43
    }
44
45
    public function testAnnotationOptionsAttribute()
46
    {
47
        $em = $this->_getTestEntityManager();
48
        $schemaTool = new SchemaTool($em);
49
50
        $classes = [
51
            $em->getClassMetadata(TestEntityWithAnnotationOptionsAttribute::class),
52
        ];
53
54
        $schema = $schemaTool->getSchemaFromMetadata($classes);
55
56
        $expected = ['foo' => 'bar', 'baz' => ['key' => 'val']];
57
58
        $this->assertEquals($expected, $schema->getTable('TestEntityWithAnnotationOptionsAttribute')->getOptions(), "options annotation are passed to the tables options");
59
        $this->assertEquals($expected, $schema->getTable('TestEntityWithAnnotationOptionsAttribute')->getColumn('test')->getCustomSchemaOptions(), "options annotation are passed to the columns customSchemaOptions");
60
    }
61
62
    /**
63
     * @group DDC-200
64
     */
65
    public function testPassColumnDefinitionToJoinColumn()
66
    {
67
        $customColumnDef = "MEDIUMINT(6) UNSIGNED NOT NULL";
68
69
        $em = $this->_getTestEntityManager();
70
        $schemaTool = new SchemaTool($em);
71
72
        $avatar = $em->getClassMetadata(ForumAvatar::class);
73
        $avatar->fieldMappings['id']['columnDefinition'] = $customColumnDef;
74
        $user = $em->getClassMetadata(ForumUser::class);
75
76
        $classes = [$avatar, $user];
77
78
        $schema = $schemaTool->getSchemaFromMetadata($classes);
79
80
        $this->assertTrue($schema->hasTable('forum_users'));
81
        $table = $schema->getTable("forum_users");
82
        $this->assertTrue($table->hasColumn('avatar_id'));
83
        $this->assertEquals($customColumnDef, $table->getColumn('avatar_id')->getColumnDefinition());
84
    }
85
86
    /**
87
     * @group DDC-283
88
     */
89
    public function testPostGenerateEvents()
90
    {
91
        $listener = new GenerateSchemaEventListener();
92
93
        $em = $this->_getTestEntityManager();
94
        $em->getEventManager()->addEventListener(
95
            [ToolEvents::postGenerateSchemaTable, ToolEvents::postGenerateSchema], $listener
96
        );
97
        $schemaTool = new SchemaTool($em);
98
99
        $classes = [
100
            $em->getClassMetadata(CmsAddress::class),
101
            $em->getClassMetadata(CmsArticle::class),
102
            $em->getClassMetadata(CmsComment::class),
103
            $em->getClassMetadata(CmsEmployee::class),
104
            $em->getClassMetadata(CmsGroup::class),
105
            $em->getClassMetadata(CmsPhonenumber::class),
106
            $em->getClassMetadata(CmsUser::class),
107
        ];
108
109
        $schema = $schemaTool->getSchemaFromMetadata($classes);
0 ignored issues
show
Unused Code introduced by
$schema is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
110
111
        $this->assertEquals(count($classes), $listener->tableCalls);
112
        $this->assertTrue($listener->schemaCalled);
113
    }
114
115
    public function testNullDefaultNotAddedToCustomSchemaOptions()
116
    {
117
        $em = $this->_getTestEntityManager();
118
        $schemaTool = new SchemaTool($em);
119
120
        $customSchemaOptions = $schemaTool->getSchemaFromMetadata([$em->getClassMetadata(NullDefaultColumn::class)])
121
            ->getTable('NullDefaultColumn')
122
            ->getColumn('nullDefault')
123
            ->getCustomSchemaOptions();
124
125
        $this->assertSame([], $customSchemaOptions);
126
    }
127
128
    /**
129
     * @group DDC-3671
130
     */
131 View Code Duplication
    public function testSchemaHasProperIndexesFromUniqueConstraintAnnotation()
132
    {
133
        $em         = $this->_getTestEntityManager();
134
        $schemaTool = new SchemaTool($em);
135
        $classes    = [
136
            $em->getClassMetadata(UniqueConstraintAnnotationModel::class),
137
        ];
138
139
        $schema = $schemaTool->getSchemaFromMetadata($classes);
140
141
        $this->assertTrue($schema->hasTable('unique_constraint_annotation_table'));
142
        $table = $schema->getTable('unique_constraint_annotation_table');
143
144
        $this->assertEquals(2, count($table->getIndexes()));
145
        $this->assertTrue($table->hasIndex('primary'));
146
        $this->assertTrue($table->hasIndex('uniq_hash'));
147
    }
148
149 View Code Duplication
    public function testRemoveUniqueIndexOverruledByPrimaryKey()
150
    {
151
        $em         = $this->_getTestEntityManager();
152
        $schemaTool = new SchemaTool($em);
153
        $classes    = [
154
            $em->getClassMetadata(FirstEntity::class),
155
            $em->getClassMetadata(SecondEntity::class)
156
        ];
157
158
        $schema = $schemaTool->getSchemaFromMetadata($classes);
159
160
        $this->assertTrue($schema->hasTable('first_entity'), "Table first_entity should exist.");
161
162
        $indexes = $schema->getTable('first_entity')->getIndexes();
163
164
        $this->assertCount(1, $indexes, "there should be only one index");
165
        $this->assertTrue(current($indexes)->isPrimary(), "index should be primary");
166
    }
167
168
    public function testSetDiscriminatorColumnWithoutLength() : void
169
    {
170
        $em         = $this->_getTestEntityManager();
171
        $schemaTool = new SchemaTool($em);
172
        $metadata   = $em->getClassMetadata(FirstEntity::class);
173
174
        $metadata->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
175
        $metadata->setDiscriminatorColumn(['name' => 'discriminator', 'type' => 'string']);
176
177
        $schema = $schemaTool->getSchemaFromMetadata([$metadata]);
178
179
        $this->assertTrue($schema->hasTable('first_entity'));
180
        $table = $schema->getTable('first_entity');
181
182
        $this->assertTrue($table->hasColumn('discriminator'));
183
        $column = $table->getColumn('discriminator');
184
185
        $this->assertEquals(255, $column->getLength());
186
    }
187
}
188
189
/**
190
 * @Entity
191
 * @Table(options={"foo": "bar", "baz": {"key": "val"}})
192
 */
193
class TestEntityWithAnnotationOptionsAttribute
194
{
195
    /** @Id @Column */
196
    private $id;
197
198
    /**
199
     * @Column(type="string", options={"foo": "bar", "baz": {"key": "val"}})
200
     */
201
    private $test;
202
}
203
204
class GenerateSchemaEventListener
205
{
206
    public $tableCalls = 0;
207
    public $schemaCalled = false;
208
209
    public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs)
0 ignored issues
show
Unused Code introduced by
The parameter $eventArgs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
210
    {
211
        $this->tableCalls++;
212
    }
213
214
    public function postGenerateSchema(GenerateSchemaEventArgs $eventArgs)
0 ignored issues
show
Unused Code introduced by
The parameter $eventArgs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
215
    {
216
        $this->schemaCalled = true;
217
    }
218
}
219
220
/**
221
 * @Entity
222
 * @Table(name="unique_constraint_annotation_table", uniqueConstraints={
223
 *   @UniqueConstraint(name="uniq_hash", columns={"hash"})
224
 * })
225
 */
226
class UniqueConstraintAnnotationModel
227
{
228
    /** @Id @Column */
229
    private $id;
230
231
    /**
232
     * @Column(name="hash", type="string", length=8, nullable=false, unique=true)
233
     */
234
    private $hash;
235
}
236
237
/**
238
 * @Entity
239
 * @Table(name="first_entity")
240
 */
241
class FirstEntity
242
{
243
    /**
244
     * @Id
245
     * @Column(name="id")
246
     */
247
    public $id;
248
249
    /**
250
     * @OneToOne(targetEntity="SecondEntity")
251
     * @JoinColumn(name="id", referencedColumnName="fist_entity_id")
252
     */
253
    public $secondEntity;
254
255
    /**
256
     * @Column(name="name")
257
     */
258
    public $name;
259
}
260
261
/**
262
 * @Entity
263
 * @Table(name="second_entity")
264
 */
265
class SecondEntity
266
{
267
    /**
268
     * @Id
269
     * @Column(name="fist_entity_id")
270
     */
271
    public $fist_entity_id;
272
273
    /**
274
     * @Column(name="name")
275
     */
276
    public $name;
277
}
278