Merge "Language: s/error_log/wfWarn/"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / ORMRowTest.php
1 <?php
2
3 /**
4 * Abstract class to construct tests for ORMRow deriving classes.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @since 1.20
23 *
24 * @ingroup Test
25 *
26 * @group ORM
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 * @group Database
31 *
32 * Some of the tests takes more time, and needs therefor longer time before they can be aborted
33 * as non-functional. The reason why tests are aborted is assumed to be set up of temporal databases
34 * that hold the first tests in a pending state awaiting access to the database.
35 * @group medium
36 *
37 * @licence GNU GPL v2+
38 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
39 */
40 abstract class ORMRowTest extends \MediaWikiTestCase {
41
42 /**
43 * @since 1.20
44 * @return string
45 */
46 abstract protected function getRowClass();
47
48 /**
49 * @since 1.20
50 * @return IORMTable
51 */
52 abstract protected function getTableInstance();
53
54 /**
55 * @since 1.20
56 * @return array
57 */
58 abstract public function constructorTestProvider();
59
60 /**
61 * @since 1.20
62 * @param IORMRow $row
63 * @param array $data
64 */
65 protected function verifyFields( IORMRow $row, array $data ) {
66 foreach ( array_keys( $data ) as $fieldName ) {
67 $this->assertEquals( $data[$fieldName], $row->getField( $fieldName ) );
68 }
69 }
70
71 /**
72 * @since 1.20
73 * @param array $data
74 * @param bool $loadDefaults
75 * @return IORMRow
76 */
77 protected function getRowInstance( array $data, $loadDefaults ) {
78 $class = $this->getRowClass();
79
80 return new $class( $this->getTableInstance(), $data, $loadDefaults );
81 }
82
83 /**
84 * @since 1.20
85 * @return array
86 */
87 protected function getMockValues() {
88 return array(
89 'id' => 1,
90 'str' => 'foobar4645645',
91 'int' => 42,
92 'float' => 4.2,
93 'bool' => true,
94 'array' => array( 42, 'foobar' ),
95 'blob' => new stdClass()
96 );
97 }
98
99 /**
100 * @since 1.20
101 * @return array
102 */
103 protected function getMockFields() {
104 $mockValues = $this->getMockValues();
105 $mockFields = array();
106
107 foreach ( $this->getTableInstance()->getFields() as $name => $type ) {
108 if ( $name !== 'id' ) {
109 $mockFields[$name] = $mockValues[$type];
110 }
111 }
112
113 return $mockFields;
114 }
115
116 /**
117 * @since 1.20
118 * @return array Array of IORMRow
119 */
120 public function instanceProvider() {
121 $instances = array();
122
123 foreach ( $this->constructorTestProvider() as $arguments ) {
124 $instances[] = array( call_user_func_array( array( $this, 'getRowInstance' ), $arguments ) );
125 }
126
127 return $instances;
128 }
129
130 /**
131 * @dataProvider constructorTestProvider
132 */
133 public function testConstructor( array $data, $loadDefaults ) {
134 $this->verifyFields( $this->getRowInstance( $data, $loadDefaults ), $data );
135 }
136
137 /**
138 * @dataProvider constructorTestProvider
139 */
140 public function testSaveAndRemove( array $data, $loadDefaults ) {
141 $item = $this->getRowInstance( $data, $loadDefaults );
142
143 $this->assertTrue( $item->save() );
144
145 $this->assertTrue( $item->hasIdField() );
146 $this->assertTrue( is_integer( $item->getId() ) );
147
148 $id = $item->getId();
149
150 $this->assertTrue( $item->save() );
151
152 $this->assertEquals( $id, $item->getId() );
153
154 $this->verifyFields( $item, $data );
155
156 $this->assertTrue( $item->remove() );
157
158 $this->assertFalse( $item->hasIdField() );
159
160 $this->assertTrue( $item->save() );
161
162 $this->verifyFields( $item, $data );
163
164 $this->assertTrue( $item->remove() );
165
166 $this->assertFalse( $item->hasIdField() );
167
168 $this->verifyFields( $item, $data );
169 }
170
171 /**
172 * @dataProvider instanceProvider
173 */
174 public function testSetField( IORMRow $item ) {
175 foreach ( $this->getMockFields() as $name => $value ) {
176 $item->setField( $name, $value );
177 $this->assertEquals( $value, $item->getField( $name ) );
178 }
179 }
180
181 /**
182 * @since 1.20
183 * @param array $expected
184 * @param IORMRow $item
185 */
186 protected function assertFieldValues( array $expected, IORMRow $item ) {
187 foreach ( $expected as $name => $type ) {
188 if ( $name !== 'id' ) {
189 $this->assertEquals( $expected[$name], $item->getField( $name ) );
190 }
191 }
192 }
193
194 /**
195 * @dataProvider instanceProvider
196 */
197 public function testSetFields( IORMRow $item ) {
198 $originalValues = $item->getFields();
199
200 $item->setFields( array(), false );
201
202 foreach ( $item->getTable()->getFields() as $name => $type ) {
203 $originalHas = array_key_exists( $name, $originalValues );
204 $newHas = $item->hasField( $name );
205
206 $this->assertEquals( $originalHas, $newHas );
207
208 if ( $originalHas && $newHas ) {
209 $this->assertEquals( $originalValues[$name], $item->getField( $name ) );
210 }
211 }
212
213 $mockFields = $this->getMockFields();
214
215 $item->setFields( $mockFields, false );
216
217 $this->assertFieldValues( $originalValues, $item );
218
219 $item->setFields( $mockFields, true );
220
221 $this->assertFieldValues( $mockFields, $item );
222 }
223
224 // TODO: test all of the methods!
225
226 }