MediaWiki  REL1_21
ORMRowTest.php
Go to the documentation of this file.
00001 <?php
00002 
00040 abstract class ORMRowTest extends \MediaWikiTestCase {
00041 
00046         abstract protected function getRowClass();
00047 
00052         abstract protected function getTableInstance();
00053 
00058         abstract public function constructorTestProvider();
00059 
00065         protected function verifyFields( IORMRow $row, array $data ) {
00066                 foreach ( array_keys( $data ) as $fieldName ) {
00067                         $this->assertEquals( $data[$fieldName], $row->getField( $fieldName ) );
00068                 }
00069         }
00070 
00077         protected function getRowInstance( array $data, $loadDefaults ) {
00078                 $class = $this->getRowClass();
00079                 return new $class( $this->getTableInstance(), $data, $loadDefaults );
00080         }
00081 
00086         protected function getMockValues() {
00087                 return array(
00088                         'id' => 1,
00089                         'str' => 'foobar4645645',
00090                         'int' => 42,
00091                         'float' => 4.2,
00092                         'bool' => true,
00093                         'array' => array( 42, 'foobar' ),
00094                         'blob' => new stdClass()
00095                 );
00096         }
00097 
00102         protected function getMockFields() {
00103                 $mockValues = $this->getMockValues();
00104                 $mockFields = array();
00105 
00106                 foreach ( $this->getTableInstance()->getFields() as $name => $type ) {
00107                         if ( $name !== 'id' ) {
00108                                 $mockFields[$name] = $mockValues[$type];
00109                         }
00110                 }
00111 
00112                 return $mockFields;
00113         }
00114 
00119         public function instanceProvider() {
00120                 $instances = array();
00121 
00122                 foreach ( $this->constructorTestProvider() as $arguments ) {
00123                         $instances[] = array( call_user_func_array( array( $this, 'getRowInstance' ), $arguments ) );
00124                 }
00125 
00126                 return $instances;
00127         }
00128 
00132         public function testConstructor( array $data, $loadDefaults ) {
00133                 $this->verifyFields( $this->getRowInstance( $data, $loadDefaults ), $data );
00134         }
00135 
00139         public function testSaveAndRemove( array $data, $loadDefaults ) {
00140                 $item = $this->getRowInstance( $data, $loadDefaults );
00141 
00142                 $this->assertTrue( $item->save() );
00143 
00144                 $this->assertTrue( $item->hasIdField() );
00145                 $this->assertTrue( is_integer( $item->getId() ) );
00146 
00147                 $id = $item->getId();
00148 
00149                 $this->assertTrue( $item->save() );
00150 
00151                 $this->assertEquals( $id, $item->getId() );
00152 
00153                 $this->verifyFields( $item, $data );
00154 
00155                 $this->assertTrue( $item->remove() );
00156 
00157                 $this->assertFalse( $item->hasIdField() );
00158 
00159                 $this->assertTrue( $item->save() );
00160 
00161                 $this->verifyFields( $item, $data );
00162 
00163                 $this->assertTrue( $item->remove() );
00164 
00165                 $this->assertFalse( $item->hasIdField() );
00166 
00167                 $this->verifyFields( $item, $data );
00168         }
00169 
00173         public function testSetField( IORMRow $item ) {
00174                 foreach ( $this->getMockFields() as $name => $value ) {
00175                         $item->setField( $name, $value );
00176                         $this->assertEquals( $value, $item->getField( $name ) );
00177                 }
00178         }
00179 
00185         protected function assertFieldValues( array $expected, IORMRow $item ) {
00186                 foreach ( $expected as $name => $type ) {
00187                         if ( $name !== 'id' ) {
00188                                 $this->assertEquals( $expected[$name], $item->getField( $name ) );
00189                         }
00190                 }
00191         }
00192 
00196         public function testSetFields( IORMRow $item ) {
00197                 $originalValues = $item->getFields();
00198 
00199                 $item->setFields( array(), false );
00200 
00201                 foreach ( $item->getTable()->getFields() as $name => $type ) {
00202                         $originalHas = array_key_exists( $name, $originalValues );
00203                         $newHas = $item->hasField( $name );
00204 
00205                         $this->assertEquals( $originalHas, $newHas );
00206 
00207                         if ( $originalHas && $newHas ) {
00208                                 $this->assertEquals( $originalValues[$name], $item->getField( $name ) );
00209                         }
00210                 }
00211 
00212                 $mockFields = $this->getMockFields();
00213 
00214                 $item->setFields( $mockFields, false );
00215 
00216                 $this->assertFieldValues( $originalValues, $item );
00217 
00218                 $item->setFields( $mockFields, true );
00219 
00220                 $this->assertFieldValues( $mockFields, $item );
00221         }
00222 
00223         // TODO: test all of the methods!
00224 
00225 }