MediaWiki  REL1_22
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 
00080         return new $class( $this->getTableInstance(), $data, $loadDefaults );
00081     }
00082 
00087     protected function getMockValues() {
00088         return array(
00089             'id' => 1,
00090             'str' => 'foobar4645645',
00091             'int' => 42,
00092             'float' => 4.2,
00093             'bool' => true,
00094             'array' => array( 42, 'foobar' ),
00095             'blob' => new stdClass()
00096         );
00097     }
00098 
00103     protected function getMockFields() {
00104         $mockValues = $this->getMockValues();
00105         $mockFields = array();
00106 
00107         foreach ( $this->getTableInstance()->getFields() as $name => $type ) {
00108             if ( $name !== 'id' ) {
00109                 $mockFields[$name] = $mockValues[$type];
00110             }
00111         }
00112 
00113         return $mockFields;
00114     }
00115 
00120     public function instanceProvider() {
00121         $instances = array();
00122 
00123         foreach ( $this->constructorTestProvider() as $arguments ) {
00124             $instances[] = array( call_user_func_array( array( $this, 'getRowInstance' ), $arguments ) );
00125         }
00126 
00127         return $instances;
00128     }
00129 
00133     public function testConstructor( array $data, $loadDefaults ) {
00134         $this->verifyFields( $this->getRowInstance( $data, $loadDefaults ), $data );
00135     }
00136 
00140     public function testSaveAndRemove( array $data, $loadDefaults ) {
00141         $item = $this->getRowInstance( $data, $loadDefaults );
00142 
00143         $this->assertTrue( $item->save() );
00144 
00145         $this->assertTrue( $item->hasIdField() );
00146         $this->assertTrue( is_integer( $item->getId() ) );
00147 
00148         $id = $item->getId();
00149 
00150         $this->assertTrue( $item->save() );
00151 
00152         $this->assertEquals( $id, $item->getId() );
00153 
00154         $this->verifyFields( $item, $data );
00155 
00156         $this->assertTrue( $item->remove() );
00157 
00158         $this->assertFalse( $item->hasIdField() );
00159 
00160         $this->assertTrue( $item->save() );
00161 
00162         $this->verifyFields( $item, $data );
00163 
00164         $this->assertTrue( $item->remove() );
00165 
00166         $this->assertFalse( $item->hasIdField() );
00167 
00168         $this->verifyFields( $item, $data );
00169     }
00170 
00174     public function testSetField( IORMRow $item ) {
00175         foreach ( $this->getMockFields() as $name => $value ) {
00176             $item->setField( $name, $value );
00177             $this->assertEquals( $value, $item->getField( $name ) );
00178         }
00179     }
00180 
00186     protected function assertFieldValues( array $expected, IORMRow $item ) {
00187         foreach ( $expected as $name => $type ) {
00188             if ( $name !== 'id' ) {
00189                 $this->assertEquals( $expected[$name], $item->getField( $name ) );
00190             }
00191         }
00192     }
00193 
00197     public function testSetFields( IORMRow $item ) {
00198         $originalValues = $item->getFields();
00199 
00200         $item->setFields( array(), false );
00201 
00202         foreach ( $item->getTable()->getFields() as $name => $type ) {
00203             $originalHas = array_key_exists( $name, $originalValues );
00204             $newHas = $item->hasField( $name );
00205 
00206             $this->assertEquals( $originalHas, $newHas );
00207 
00208             if ( $originalHas && $newHas ) {
00209                 $this->assertEquals( $originalValues[$name], $item->getField( $name ) );
00210             }
00211         }
00212 
00213         $mockFields = $this->getMockFields();
00214 
00215         $item->setFields( $mockFields, false );
00216 
00217         $this->assertFieldValues( $originalValues, $item );
00218 
00219         $item->setFields( $mockFields, true );
00220 
00221         $this->assertFieldValues( $mockFields, $item );
00222     }
00223 
00224     // TODO: test all of the methods!
00225 
00226 }