Merge "Remove unnecessary ZWNJ character [azb]"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / TestORMRowTest.php
1 <?php
2
3 /**
4 * Tests for the TestORMRow class.
5 * TestORMRow is a dummy class to be able to test the abstract ORMRow class.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Test
24 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
25 */
26
27 /**
28 * The database group has as a side effect that temporal database tables are created. This makes
29 * it possible to test without poisoning a production database.
30 *
31 * Some of the tests takes more time, and needs therefor longer time before they can be aborted
32 * as non-functional. The reason why tests are aborted is assumed to be set up of temporal databases
33 * that hold the first tests in a pending state awaiting access to the database.
34 *
35 * @since 1.20
36 *
37 * @group ORM
38 * @group Database
39 * @group medium
40 * @covers TestORMRow
41 */
42 class TestORMRowTest extends ORMRowTest {
43
44 /**
45 * @since 1.20
46 * @return string
47 */
48 protected function getRowClass() {
49 return 'TestORMRow';
50 }
51
52 /**
53 * @since 1.20
54 * @return IORMTable
55 */
56 protected function getTableInstance() {
57 return TestORMTable::singleton();
58 }
59
60 protected function setUp() {
61 parent::setUp();
62
63 $dbw = wfGetDB( DB_MASTER );
64
65 $isSqlite = $GLOBALS['wgDBtype'] === 'sqlite';
66 $isPostgres = $GLOBALS['wgDBtype'] === 'postgres';
67
68 $idField = $isSqlite ? 'INTEGER' : 'INT unsigned';
69 $primaryKey = $isSqlite ? 'PRIMARY KEY AUTOINCREMENT' : 'auto_increment PRIMARY KEY';
70
71 if ( $isPostgres ) {
72 $dbw->query(
73 'CREATE TABLE IF NOT EXISTS ' . $dbw->tableName( 'orm_test' ) . "(
74 test_id serial PRIMARY KEY,
75 test_name TEXT NOT NULL DEFAULT '',
76 test_age INTEGER NOT NULL DEFAULT 0,
77 test_height REAL NOT NULL DEFAULT 0,
78 test_awesome INTEGER NOT NULL DEFAULT 0,
79 test_stuff BYTEA,
80 test_moarstuff BYTEA,
81 test_time TIMESTAMPTZ
82 );",
83 __METHOD__
84 );
85 } else {
86 $dbw->query(
87 'CREATE TABLE IF NOT EXISTS ' . $dbw->tableName( 'orm_test' ) . '(
88 test_id ' . $idField . ' NOT NULL ' . $primaryKey . ',
89 test_name VARCHAR(255) NOT NULL,
90 test_age TINYINT unsigned NOT NULL,
91 test_height FLOAT NOT NULL,
92 test_awesome TINYINT unsigned NOT NULL,
93 test_stuff BLOB NOT NULL,
94 test_moarstuff BLOB NOT NULL,
95 test_time varbinary(14) NOT NULL
96 );',
97 __METHOD__
98 );
99 }
100 }
101
102 protected function tearDown() {
103 $dbw = wfGetDB( DB_MASTER );
104 $dbw->dropTable( 'orm_test', __METHOD__ );
105
106 parent::tearDown();
107 }
108
109 public function constructorTestProvider() {
110 $dbw = wfGetDB( DB_MASTER );
111 return array(
112 array(
113 array(
114 'name' => 'Foobar',
115 'time' => $dbw->timestamp( '20120101020202' ),
116 'age' => 42,
117 'height' => 9000.1,
118 'awesome' => true,
119 'stuff' => array( 13, 11, 7, 5, 3, 2 ),
120 'moarstuff' => (object)array( 'foo' => 'bar', 'bar' => array( 4, 2 ), 'baz' => true )
121 ),
122 true
123 ),
124 );
125 }
126
127 /**
128 * @since 1.21
129 * @return array
130 */
131 protected function getMockValues() {
132 return array(
133 'id' => 1,
134 'str' => 'foobar4645645',
135 'int' => 42,
136 'float' => 4.2,
137 'bool' => '',
138 'array' => array( 42, 'foobar' ),
139 'blob' => new stdClass()
140 );
141 }
142 }
143
144 class TestORMRow extends ORMRow {
145 }
146
147 class TestORMTable extends ORMTable {
148
149 public function __construct() {
150 $this->fieldPrefix = 'test_';
151 }
152
153 /**
154 * Returns the name of the database table objects of this type are stored in.
155 *
156 * @since 1.20
157 *
158 * @return string
159 */
160 public function getName() {
161 return 'orm_test';
162 }
163
164 /**
165 * Returns the name of a IORMRow implementing class that
166 * represents single rows in this table.
167 *
168 * @since 1.20
169 *
170 * @return string
171 */
172 public function getRowClass() {
173 return 'TestORMRow';
174 }
175
176 /**
177 * Returns an array with the fields and their types this object contains.
178 * This corresponds directly to the fields in the database, without prefix.
179 *
180 * field name => type
181 *
182 * Allowed types:
183 * * id
184 * * str
185 * * int
186 * * float
187 * * bool
188 * * array
189 * * blob
190 *
191 * @since 1.20
192 *
193 * @return array
194 */
195 public function getFields() {
196 return array(
197 'id' => 'id',
198 'name' => 'str',
199 'age' => 'int',
200 'height' => 'float',
201 'awesome' => 'bool',
202 'stuff' => 'array',
203 'moarstuff' => 'blob',
204 'time' => 'str', // TS_MW
205 );
206 }
207 }