Completed
Pull Request — master (#5798)
by Sergey
12:33
created

testSetDiscriminatorColumnWithoutLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 19
rs 9.4285
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\SchemaTool;
7
use Doctrine\ORM\Tools\ToolEvents;
8
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
9
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
10
11
class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
12
{
13
    public function testAddUniqueIndexForUniqueFieldAnnotation()
14
    {
15
        $em = $this->_getTestEntityManager();
16
        $schemaTool = new SchemaTool($em);
17
18
        $classes = array(
19
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'),
20
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsArticle'),
21
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsComment'),
22
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsEmployee'),
23
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup'),
24
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'),
25
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'),
26
        );
27
28
        $schema = $schemaTool->getSchemaFromMetadata($classes);
29
30
        $this->assertTrue($schema->hasTable('cms_users'), "Table cms_users should exist.");
31
        $this->assertTrue($schema->getTable('cms_users')->columnsAreIndexed(array('username')), "username column should be indexed.");
32
    }
33
34
    public function testAnnotationOptionsAttribute()
35
    {
36
        $em = $this->_getTestEntityManager();
37
        $schemaTool = new SchemaTool($em);
38
39
        $classes = array(
40
            $em->getClassMetadata(__NAMESPACE__ . '\\TestEntityWithAnnotationOptionsAttribute'),
41
        );
42
43
        $schema = $schemaTool->getSchemaFromMetadata($classes);
44
45
        $expected = array('foo' => 'bar', 'baz' => array('key' => 'val'));
46
47
        $this->assertEquals($expected, $schema->getTable('TestEntityWithAnnotationOptionsAttribute')->getOptions(), "options annotation are passed to the tables options");
48
        $this->assertEquals($expected, $schema->getTable('TestEntityWithAnnotationOptionsAttribute')->getColumn('test')->getCustomSchemaOptions(), "options annotation are passed to the columns customSchemaOptions");
49
    }
50
51
    /**
52
     * @group DDC-200
53
     */
54
    public function testPassColumnDefinitionToJoinColumn()
55
    {
56
        $customColumnDef = "MEDIUMINT(6) UNSIGNED NOT NULL";
57
58
        $em = $this->_getTestEntityManager();
59
        $schemaTool = new SchemaTool($em);
60
61
        $avatar = $em->getClassMetadata('Doctrine\Tests\Models\Forum\ForumAvatar');
62
        $avatar->fieldMappings['id']['columnDefinition'] = $customColumnDef;
63
        $user = $em->getClassMetadata('Doctrine\Tests\Models\Forum\ForumUser');
64
65
        $classes = array($avatar, $user);
66
67
        $schema = $schemaTool->getSchemaFromMetadata($classes);
68
69
        $this->assertTrue($schema->hasTable('forum_users'));
70
        $table = $schema->getTable("forum_users");
71
        $this->assertTrue($table->hasColumn('avatar_id'));
72
        $this->assertEquals($customColumnDef, $table->getColumn('avatar_id')->getColumnDefinition());
73
    }
74
75
    /**
76
     * @group DDC-283
77
     */
78
    public function testPostGenerateEvents()
79
    {
80
        $listener = new GenerateSchemaEventListener();
81
82
        $em = $this->_getTestEntityManager();
83
        $em->getEventManager()->addEventListener(
84
            array(ToolEvents::postGenerateSchemaTable, ToolEvents::postGenerateSchema), $listener
85
        );
86
        $schemaTool = new SchemaTool($em);
87
88
        $classes = array(
89
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'),
90
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsArticle'),
91
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsComment'),
92
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsEmployee'),
93
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup'),
94
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'),
95
            $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'),
96
        );
97
98
        $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...
99
100
        $this->assertEquals(count($classes), $listener->tableCalls);
101
        $this->assertTrue($listener->schemaCalled);
102
    }
103
104
    public function testNullDefaultNotAddedToCustomSchemaOptions()
105
    {
106
        $em = $this->_getTestEntityManager();
107
        $schemaTool = new SchemaTool($em);
108
109
        $classes = array(
110
            $em->getClassMetadata('Doctrine\Tests\Models\NullDefault\NullDefaultColumn'),
111
        );
112
113
        $customSchemaOptions = $schemaTool->getSchemaFromMetadata($classes)
114
            ->getTable('NullDefaultColumn')
115
            ->getColumn('nullDefault')
116
            ->getCustomSchemaOptions();
117
118
        $this->assertSame(array(), $customSchemaOptions);
119
    }
120
121
    /**
122
     * @group DDC-3671
123
     */
124
    public function testSchemaHasProperIndexesFromUniqueConstraintAnnotation()
125
    {
126
        $em         = $this->_getTestEntityManager();
127
        $schemaTool = new SchemaTool($em);
128
        $classes    = [
129
            $em->getClassMetadata(__NAMESPACE__ . '\\UniqueConstraintAnnotationModel'),
130
        ];
131
132
        $schema = $schemaTool->getSchemaFromMetadata($classes);
133
134
        $this->assertTrue($schema->hasTable('unique_constraint_annotation_table'));
135
        $table = $schema->getTable('unique_constraint_annotation_table');
136
137
        $this->assertEquals(2, count($table->getIndexes()));
138
        $this->assertTrue($table->hasIndex('primary'));
139
        $this->assertTrue($table->hasIndex('uniq_hash'));
140
    }
141
142
    public function testRemoveUniqueIndexOverruledByPrimaryKey()
143
    {
144
        $em         = $this->_getTestEntityManager();
145
        $schemaTool = new SchemaTool($em);
146
        $classes    = [
147
            $em->getClassMetadata(__NAMESPACE__ . '\\FirstEntity'),
148
            $em->getClassMetadata(__NAMESPACE__ . '\\SecondEntity')
149
        ];
150
151
        $schema = $schemaTool->getSchemaFromMetadata($classes);
152
153
        $this->assertTrue($schema->hasTable('first_entity'), "Table first_entity should exist.");
154
155
        $indexes = $schema->getTable('first_entity')->getIndexes();
156
157
        $this->assertCount(1, $indexes, "there should be only one index");
158
        $this->assertTrue(current($indexes)->isPrimary(), "index should be primary");
159
    }
160
161
    public function testSetDiscriminatorColumnWithoutLength()
162
    {
163
        $em         = $this->_getTestEntityManager();
164
        $schemaTool = new SchemaTool($em);
165
        $metadata   = $em->getClassMetadata(__NAMESPACE__ . '\\FirstEntity');
166
167
        $metadata->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
168
        $metadata->setDiscriminatorColumn(['name' => 'discriminator', 'type' => 'string']);
169
170
        $schema = $schemaTool->getSchemaFromMetadata([$metadata]);
171
172
        $this->assertTrue($schema->hasTable('first_entity'));
173
        $table = $schema->getTable('first_entity');
174
175
        $this->assertTrue($table->hasColumn('discriminator'));
176
        $column = $table->getColumn('discriminator');
177
178
        $this->assertEquals(255, $column->getLength());
179
    }
180
}
181
182
/**
183
 * @Entity
184
 * @Table(options={"foo": "bar", "baz": {"key": "val"}})
185
 */
186
class TestEntityWithAnnotationOptionsAttribute
187
{
188
    /** @Id @Column */
189
    private $id;
190
191
    /**
192
     * @Column(type="string", options={"foo": "bar", "baz": {"key": "val"}})
193
     */
194
    private $test;
195
}
196
197
class GenerateSchemaEventListener
198
{
199
    public $tableCalls = 0;
200
    public $schemaCalled = false;
201
202
    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...
203
    {
204
        $this->tableCalls++;
205
    }
206
207
    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...
208
    {
209
        $this->schemaCalled = true;
210
    }
211
}
212
213
/**
214
 * @Entity
215
 * @Table(name="unique_constraint_annotation_table", uniqueConstraints={
216
 *   @UniqueConstraint(name="uniq_hash", columns={"hash"})
217
 * })
218
 */
219
class UniqueConstraintAnnotationModel
220
{
221
    /** @Id @Column */
222
    private $id;
223
224
    /**
225
     * @Column(name="hash", type="string", length=8, nullable=false, unique=true)
226
     */
227
    private $hash;
228
}
229
230
/**
231
 * @Entity
232
 * @Table(name="first_entity")
233
 */
234
class FirstEntity
235
{
236
    /**
237
     * @Id
238
     * @Column(name="id")
239
     */
240
    public $id;
241
242
    /**
243
     * @OneToOne(targetEntity="SecondEntity")
244
     * @JoinColumn(name="id", referencedColumnName="fist_entity_id")
245
     */
246
    public $secondEntity;
247
248
    /**
249
     * @Column(name="name")
250
     */
251
    public $name;
252
}
253
254
/**
255
 * @Entity
256
 * @Table(name="second_entity")
257
 */
258
class SecondEntity
259
{
260
    /**
261
     * @Id
262
     * @Column(name="fist_entity_id")
263
     */
264
    public $fist_entity_id;
265
266
    /**
267
     * @Column(name="name")
268
     */
269
    public $name;
270
}