Merge "Make enhanced recent changes and extended watchlist default"
[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 * @since 1.20
24 *
25 * @ingroup Test
26 *
27 * @group ORM
28 *
29 * The database group has as a side effect that temporal database tables are created. This makes
30 * it possible to test without poisoning a production database.
31 * @group Database
32 *
33 * Some of the tests takes more time, and needs therefor longer time before they can be aborted
34 * as non-functional. The reason why tests are aborted is assumed to be set up of temporal databases
35 * that hold the first tests in a pending state awaiting access to the database.
36 * @group medium
37 *
38 * @licence GNU GPL v2+
39 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
40 */
41 require_once __DIR__ . "/ORMRowTest.php";
42
43 /**
44 * @covers TestORMRow
45 */
46 class TestORMRowTest extends ORMRowTest {
47
48 /**
49 * @since 1.20
50 * @return string
51 */
52 protected function getRowClass() {
53 return 'TestORMRow';
54 }
55
56 /**
57 * @since 1.20
58 * @return IORMTable
59 */
60 protected function getTableInstance() {
61 return TestORMTable::singleton();
62 }
63
64 protected function setUp() {
65 parent::setUp();
66
67 $dbw = wfGetDB( DB_MASTER );
68
69 $isSqlite = $GLOBALS['wgDBtype'] === 'sqlite';
70 $isPostgres = $GLOBALS['wgDBtype'] === 'postgres';
71
72 $idField = $isSqlite ? 'INTEGER' : 'INT unsigned';
73 $primaryKey = $isSqlite ? 'PRIMARY KEY AUTOINCREMENT' : 'auto_increment PRIMARY KEY';
74
75 if ( $isPostgres ) {
76 $dbw->query(
77 'CREATE TABLE IF NOT EXISTS ' . $dbw->tableName( 'orm_test' ) . "(
78 test_id serial PRIMARY KEY,
79 test_name TEXT NOT NULL DEFAULT '',
80 test_age INTEGER NOT NULL DEFAULT 0,
81 test_height REAL NOT NULL DEFAULT 0,
82 test_awesome INTEGER NOT NULL DEFAULT 0,
83 test_stuff BYTEA,
84 test_moarstuff BYTEA,
85 test_time TIMESTAMPTZ
86 );",
87 __METHOD__
88 );
89 } else {
90 $dbw->query(
91 'CREATE TABLE IF NOT EXISTS ' . $dbw->tableName( 'orm_test' ) . '(
92 test_id ' . $idField . ' NOT NULL ' . $primaryKey . ',
93 test_name VARCHAR(255) NOT NULL,
94 test_age TINYINT unsigned NOT NULL,
95 test_height FLOAT NOT NULL,
96 test_awesome TINYINT unsigned NOT NULL,
97 test_stuff BLOB NOT NULL,
98 test_moarstuff BLOB NOT NULL,
99 test_time varbinary(14) NOT NULL
100 );',
101 __METHOD__
102 );
103 }
104 }
105
106 protected function tearDown() {
107 $dbw = wfGetDB( DB_MASTER );
108 $dbw->dropTable( 'orm_test', __METHOD__ );
109
110 parent::tearDown();
111 }
112
113 public function constructorTestProvider() {
114 $dbw = wfGetDB( DB_MASTER );
115 return array(
116 array(
117 array(
118 'name' => 'Foobar',
119 'time' => $dbw->timestamp( '20120101020202' ),
120 'age' => 42,
121 'height' => 9000.1,
122 'awesome' => true,
123 'stuff' => array( 13, 11, 7, 5, 3, 2 ),
124 'moarstuff' => (object)array( 'foo' => 'bar', 'bar' => array( 4, 2 ), 'baz' => true )
125 ),
126 true
127 ),
128 );
129 }
130
131 /**
132 * @since 1.21
133 * @return array
134 */
135 protected function getMockValues() {
136 return array(
137 'id' => 1,
138 'str' => 'foobar4645645',
139 'int' => 42,
140 'float' => 4.2,
141 'bool' => '',
142 'array' => array( 42, 'foobar' ),
143 'blob' => new stdClass()
144 );
145 }
146 }
147
148 class TestORMRow extends ORMRow {
149 }
150
151 class TestORMTable extends ORMTable {
152
153 public function __construct() {
154 $this->fieldPrefix = 'test_';
155 }
156
157 /**
158 * Returns the name of the database table objects of this type are stored in.
159 *
160 * @since 1.20
161 *
162 * @return string
163 */
164 public function getName() {
165 return 'orm_test';
166 }
167
168 /**
169 * Returns the name of a IORMRow implementing class that
170 * represents single rows in this table.
171 *
172 * @since 1.20
173 *
174 * @return string
175 */
176 public function getRowClass() {
177 return 'TestORMRow';
178 }
179
180 /**
181 * Returns an array with the fields and their types this object contains.
182 * This corresponds directly to the fields in the database, without prefix.
183 *
184 * field name => type
185 *
186 * Allowed types:
187 * * id
188 * * str
189 * * int
190 * * float
191 * * bool
192 * * array
193 * * blob
194 *
195 * @since 1.20
196 *
197 * @return array
198 */
199 public function getFields() {
200 return array(
201 'id' => 'id',
202 'name' => 'str',
203 'age' => 'int',
204 'height' => 'float',
205 'awesome' => 'bool',
206 'stuff' => 'array',
207 'moarstuff' => 'blob',
208 'time' => 'str', // TS_MW
209 );
210 }
211 }