Merge "WebInstallerOutput: Apply wfBCP47() to lang attribute"
[mediawiki.git] / tests / phpunit / includes / db / ORMRowTest.php
blob447bf219970f62c993bec256c1f72b6eb21519f1
1 <?php
3 /**
4 * Abstract class to construct tests for ORMRow deriving classes.
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.
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.
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
21 * @file
22 * @since 1.20
24 * @ingroup Test
26 * @group ORM
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
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
37 * @licence GNU GPL v2+
38 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
40 abstract class ORMRowTest extends \MediaWikiTestCase {
42 /**
43 * @since 1.20
44 * @return string
46 abstract protected function getRowClass();
48 /**
49 * @since 1.20
50 * @return IORMTable
52 abstract protected function getTableInstance();
54 /**
55 * @since 1.20
56 * @return array
58 abstract public function constructorTestProvider();
60 /**
61 * @since 1.20
62 * @param IORMRow $row
63 * @param array $data
65 protected function verifyFields( IORMRow $row, array $data ) {
66 foreach ( array_keys( $data ) as $fieldName ) {
67 $this->assertEquals( $data[$fieldName], $row->getField( $fieldName ) );
71 /**
72 * @since 1.20
73 * @param array $data
74 * @param bool $loadDefaults
75 * @return IORMRow
77 protected function getRowInstance( array $data, $loadDefaults ) {
78 $class = $this->getRowClass();
80 return new $class( $this->getTableInstance(), $data, $loadDefaults );
83 /**
84 * @since 1.20
85 * @return array
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()
99 /**
100 * @since 1.20
101 * @return array
103 protected function getMockFields() {
104 $mockValues = $this->getMockValues();
105 $mockFields = array();
107 foreach ( $this->getTableInstance()->getFields() as $name => $type ) {
108 if ( $name !== 'id' ) {
109 $mockFields[$name] = $mockValues[$type];
113 return $mockFields;
117 * @since 1.20
118 * @return array Array of IORMRow
120 public function instanceProvider() {
121 $instances = array();
123 foreach ( $this->constructorTestProvider() as $arguments ) {
124 $instances[] = array( call_user_func_array( array( $this, 'getRowInstance' ), $arguments ) );
127 return $instances;
131 * @dataProvider constructorTestProvider
133 public function testConstructor( array $data, $loadDefaults ) {
134 $this->verifyFields( $this->getRowInstance( $data, $loadDefaults ), $data );
138 * @dataProvider constructorTestProvider
140 public function testSaveAndRemove( array $data, $loadDefaults ) {
141 $item = $this->getRowInstance( $data, $loadDefaults );
143 $this->assertTrue( $item->save() );
145 $this->assertTrue( $item->hasIdField() );
146 $this->assertTrue( is_integer( $item->getId() ) );
148 $id = $item->getId();
150 $this->assertTrue( $item->save() );
152 $this->assertEquals( $id, $item->getId() );
154 $this->verifyFields( $item, $data );
156 $this->assertTrue( $item->remove() );
158 $this->assertFalse( $item->hasIdField() );
160 $this->assertTrue( $item->save() );
162 $this->verifyFields( $item, $data );
164 $this->assertTrue( $item->remove() );
166 $this->assertFalse( $item->hasIdField() );
168 $this->verifyFields( $item, $data );
172 * @dataProvider instanceProvider
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 ) );
182 * @since 1.20
183 * @param array $expected
184 * @param IORMRow $item
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 ) );
195 * @dataProvider instanceProvider
197 public function testSetFields( IORMRow $item ) {
198 $originalValues = $item->getFields();
200 $item->setFields( array(), false );
202 foreach ( $item->getTable()->getFields() as $name => $type ) {
203 $originalHas = array_key_exists( $name, $originalValues );
204 $newHas = $item->hasField( $name );
206 $this->assertEquals( $originalHas, $newHas );
208 if ( $originalHas && $newHas ) {
209 $this->assertEquals( $originalValues[$name], $item->getField( $name ) );
213 $mockFields = $this->getMockFields();
215 $item->setFields( $mockFields, false );
217 $this->assertFieldValues( $originalValues, $item );
219 $item->setFields( $mockFields, true );
221 $this->assertFieldValues( $mockFields, $item );
224 // TODO: test all of the methods!