This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathCreateTableTest.php
201 lines (181 loc) · 6.99 KB
/
CreateTableTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Db\Sql\Ddl;
use PHPUnit\Framework\TestCase;
use Zend\Db\Adapter\Platform;
use Zend\Db\Sql\Ddl\Column\Column;
use Zend\Db\Sql\Ddl\Index\Index;
use Zend\Db\Sql\Ddl\Constraint;
use Zend\Db\Sql\Ddl\CreateTable;
class CreateTableTest extends TestCase
{
/**
* test object construction
* @covers \Zend\Db\Sql\Ddl\CreateTable::__construct
*/
public function testObjectConstruction()
{
$ct = new CreateTable('foo', true);
self::assertEquals('foo', $ct->getRawState($ct::TABLE));
self::assertTrue($ct->isTemporary());
}
/**
* @covers \Zend\Db\Sql\Ddl\CreateTable::setTemporary
*/
public function testSetTemporary()
{
$ct = new CreateTable();
self::assertSame($ct, $ct->setTemporary(false));
self::assertFalse($ct->isTemporary());
$ct->setTemporary(true);
self::assertTrue($ct->isTemporary());
$ct->setTemporary('yes');
self::assertTrue($ct->isTemporary());
self::assertStringStartsWith("CREATE TEMPORARY TABLE", $ct->getSqlString());
}
/**
* @covers \Zend\Db\Sql\Ddl\CreateTable::isTemporary
*/
public function testIsTemporary()
{
$ct = new CreateTable();
self::assertFalse($ct->isTemporary());
$ct->setTemporary(true);
self::assertTrue($ct->isTemporary());
}
/**
* @covers \Zend\Db\Sql\Ddl\CreateTable::setTable
*/
public function testSetTable()
{
$ct = new CreateTable();
self::assertEquals('', $ct->getRawState('table'));
$ct->setTable('test');
return $ct;
}
/**
* @covers \Zend\Db\Sql\Ddl\CreateTable::getRawState
* @depends testSetTable
*/
public function testRawStateViaTable(CreateTable $ct)
{
self::assertEquals('test', $ct->getRawState('table'));
}
/**
* @covers \Zend\Db\Sql\Ddl\CreateTable::addColumn
*/
public function testAddColumn()
{
$column = $this->getMockBuilder('Zend\Db\Sql\Ddl\Column\ColumnInterface')->getMock();
$ct = new CreateTable;
self::assertSame($ct, $ct->addColumn($column));
return $ct;
}
/**
* @covers \Zend\Db\Sql\Ddl\CreateTable::getRawState
* @depends testAddColumn
*/
public function testRawStateViaColumn(CreateTable $ct)
{
$state = $ct->getRawState('columns');
self::assertInternalType('array', $state);
$column = array_pop($state);
self::assertInstanceOf('Zend\Db\Sql\Ddl\Column\ColumnInterface', $column);
}
/**
* @covers \Zend\Db\Sql\Ddl\CreateTable::addConstraint
*/
public function testAddConstraint()
{
$constraint = $this->getMockBuilder('Zend\Db\Sql\Ddl\Constraint\ConstraintInterface')->getMock();
$ct = new CreateTable;
self::assertSame($ct, $ct->addConstraint($constraint));
return $ct;
}
/**
* @covers \Zend\Db\Sql\Ddl\CreateTable::getRawState
* @depends testAddConstraint
*/
public function testRawStateViaConstraint(CreateTable $ct)
{
$state = $ct->getRawState('constraints');
self::assertInternalType('array', $state);
$constraint = array_pop($state);
self::assertInstanceOf('Zend\Db\Sql\Ddl\Constraint\ConstraintInterface', $constraint);
}
/**
* @covers \Zend\Db\Sql\Ddl\CreateTable::getSqlString
*/
public function testGetSqlString()
{
$ct = new CreateTable('foo');
self::assertEquals("CREATE TABLE \"foo\" ( \n)", $ct->getSqlString());
$ct = new CreateTable('foo', true);
self::assertEquals("CREATE TEMPORARY TABLE \"foo\" ( \n)", $ct->getSqlString());
$ct = new CreateTable('foo');
$ct->addColumn(new Column('bar'));
self::assertEquals("CREATE TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL \n)", $ct->getSqlString());
$ct = new CreateTable('foo', true);
$ct->addColumn(new Column('bar'));
self::assertEquals("CREATE TEMPORARY TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL \n)", $ct->getSqlString());
$ct = new CreateTable('foo', true);
$ct->addColumn(new Column('bar'));
$ct->addColumn(new Column('baz'));
self::assertEquals(
"CREATE TEMPORARY TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL,\n \"baz\" INTEGER NOT NULL \n)",
$ct->getSqlString()
);
$ct = new CreateTable('foo');
$ct->addColumn(new Column('bar'));
$ct->addConstraint(new Constraint\PrimaryKey('bat'));
self::assertEquals(
"CREATE TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL , \n PRIMARY KEY (\"bat\") \n)",
$ct->getSqlString()
);
$ct = new CreateTable('foo');
$ct->addConstraint(new Constraint\PrimaryKey('bar'));
$ct->addConstraint(new Constraint\PrimaryKey('bat'));
self::assertEquals(
"CREATE TABLE \"foo\" ( \n PRIMARY KEY (\"bar\"),\n PRIMARY KEY (\"bat\") \n)",
$ct->getSqlString()
);
/**
* @link https://github.com/zendframework/zend-db/issues/266
*/
$ct = new CreateTable('t\'e"s`t');
$ct->addColumn(new Column('t\'e"s`tCol'));
$ct->addColumn(new Column('t\'e"s`tCol2'));
$ct->addConstraint(new Constraint\PrimaryKey('t\'e"s`tCol'));
$ct->addConstraint(new Index('t\'e"s`tCol2', 't\'e"s`tIndex'));
self::assertEquals(
"CREATE TABLE \"t'e\"\"s`t\" ( \n \"t'e\"\"s`tCol\" INTEGER NOT NULL,\n" .
" \"t'e\"\"s`tCol2\" INTEGER NOT NULL , \n PRIMARY KEY (\"t'e\"\"s`tCol\"),\n" .
" INDEX \"t'e\"\"s`tIndex\"(\"t'e\"\"s`tCol2\") \n)",
$ct->getSqlString()
);
self::assertEquals(
"CREATE TABLE `t'e\"s``t` ( \n `t'e\"s``tCol` INTEGER NOT NULL,\n" .
" `t'e\"s``tCol2` INTEGER NOT NULL , \n PRIMARY KEY (`t'e\"s``tCol`),\n" .
" INDEX `t'e\"s``tIndex`(`t'e\"s``tCol2`) \n)",
$ct->getSqlString(new Platform\Mysql())
);
self::assertEquals(
"CREATE TABLE \"t'e\"\"s`t\" ( \n \"t'e\"\"s`tCol\" INTEGER NOT NULL,\n" .
" \"t'e\"\"s`tCol2\" INTEGER NOT NULL , \n PRIMARY KEY (\"t'e\"\"s`tCol\"),\n" .
" INDEX \"t'e\"\"s`tIndex\"(\"t'e\"\"s`tCol2\") \n)",
$ct->getSqlString(new Platform\Postgresql())
);
self::assertEquals(
"CREATE TABLE \"t'e\"\"s`t\" ( \n \"t'e\"\"s`tCol\" INTEGER NOT NULL,\n" .
" \"t'e\"\"s`tCol2\" INTEGER NOT NULL , \n PRIMARY KEY (\"t'e\"\"s`tCol\"),\n" .
" INDEX \"t'e\"\"s`tIndex\"(\"t'e\"\"s`tCol2\") \n)",
$ct->getSqlString(new Platform\Sqlite())
);
}
}