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
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.
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.
37 * @licence GNU GPL v2+
38 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
40 abstract class ORMRowTest
extends \MediaWikiTestCase
{
46 abstract protected function getRowClass();
52 abstract protected function getTableInstance();
58 abstract public function constructorTestProvider();
65 protected function verifyFields( IORMRow
$row, array $data ) {
66 foreach ( array_keys( $data ) as $fieldName ) {
67 $this->assertEquals( $data[$fieldName], $row->getField( $fieldName ) );
74 * @param bool $loadDefaults
77 protected function getRowInstance( array $data, $loadDefaults ) {
78 $class = $this->getRowClass();
80 return new $class( $this->getTableInstance(), $data, $loadDefaults );
87 protected function getMockValues() {
90 'str' => 'foobar4645645',
94 'array' => array( 42, 'foobar' ),
95 'blob' => new stdClass()
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];
118 * @return 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 ) );
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 ) );
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!