MediaWiki  REL1_20
ORMRowTest.php
Go to the documentation of this file.
00001 <?php
00002 
00040 abstract class ORMRowTest extends \MediaWikiTestCase {
00041 
00046         protected abstract function getRowClass();
00047 
00052         protected abstract function getTableInstance();
00053 
00058         public abstract 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 testSave( 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 
00159         public function testRemove( array $data, $loadDefaults ) {
00160                 $item = $this->getRowInstance( $data, $loadDefaults );
00161 
00162                 $this->assertTrue( $item->save() );
00163 
00164                 $this->assertTrue( $item->remove() );
00165 
00166                 $this->assertFalse( $item->hasIdField() );
00167 
00168                 $this->assertTrue( $item->save() );
00169 
00170                 $this->verifyFields( $item, $data );
00171 
00172                 $this->assertTrue( $item->remove() );
00173 
00174                 $this->assertFalse( $item->hasIdField() );
00175 
00176                 $this->verifyFields( $item, $data );
00177         }
00178 
00182         public function testSetField( IORMRow $item ) {
00183                 foreach ( $this->getMockFields() as $name => $value ) {
00184                         $item->setField( $name, $value );
00185                         $this->assertEquals( $value, $item->getField( $name ) );
00186                 }
00187         }
00188 
00194         protected function assertFieldValues( array $expected, IORMRow $item ) {
00195                 foreach ( $expected as $name => $type ) {
00196                         if ( $name !== 'id' ) {
00197                                 $this->assertEquals( $expected[$name], $item->getField( $name ) );
00198                         }
00199                 }
00200         }
00201 
00205         public function testSetFields( IORMRow $item ) {
00206                 $originalValues = $item->getFields();
00207 
00208                 $item->setFields( array(), false );
00209 
00210                 foreach ( $item->getTable()->getFields() as $name => $type ) {
00211                         $originalHas = array_key_exists( $name, $originalValues );
00212                         $newHas = $item->hasField( $name );
00213 
00214                         $this->assertEquals( $originalHas, $newHas );
00215 
00216                         if ( $originalHas && $newHas ) {
00217                                 $this->assertEquals( $originalValues[$name], $item->getField( $name ) );
00218                         }
00219                 }
00220 
00221                 $mockFields = $this->getMockFields();
00222 
00223                 $item->setFields( $mockFields, false );
00224 
00225                 $this->assertFieldValues( $originalValues, $item );
00226 
00227                 $item->setFields( $mockFields, true );
00228 
00229                 $this->assertFieldValues( $mockFields, $item );
00230         }
00231 
00232         // TODO: test all of the methods!
00233 
00234 }