MediaWiki
master
|
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 00160 public function testRemove( array $data, $loadDefaults ) { 00161 $item = $this->getRowInstance( $data, $loadDefaults ); 00162 00163 $this->assertTrue( $item->remove() ); 00164 00165 $this->assertFalse( $item->hasIdField() ); 00166 00167 $this->assertTrue( $item->save() ); 00168 00169 $this->verifyFields( $item, $data ); 00170 00171 $this->assertTrue( $item->remove() ); 00172 00173 $this->assertFalse( $item->hasIdField() ); 00174 00175 $this->verifyFields( $item, $data ); 00176 } 00177 00181 public function testSetField( IORMRow $item ) { 00182 foreach ( $this->getMockFields() as $name => $value ) { 00183 $item->setField( $name, $value ); 00184 $this->assertEquals( $value, $item->getField( $name ) ); 00185 } 00186 } 00187 00193 protected function assertFieldValues( array $expected, IORMRow $item ) { 00194 foreach ( $expected as $name => $type ) { 00195 if ( $name !== 'id' ) { 00196 $this->assertEquals( $expected[$name], $item->getField( $name ) ); 00197 } 00198 } 00199 } 00200 00204 public function testSetFields( IORMRow $item ) { 00205 $originalValues = $item->getFields(); 00206 00207 $item->setFields( array(), false ); 00208 00209 foreach ( $item->getTable()->getFields() as $name => $type ) { 00210 $originalHas = array_key_exists( $name, $originalValues ); 00211 $newHas = $item->hasField( $name ); 00212 00213 $this->assertEquals( $originalHas, $newHas ); 00214 00215 if ( $originalHas && $newHas ) { 00216 $this->assertEquals( $originalValues[$name], $item->getField( $name ) ); 00217 } 00218 } 00219 00220 $mockFields = $this->getMockFields(); 00221 00222 $item->setFields( $mockFields, false ); 00223 00224 $this->assertFieldValues( $originalValues, $item ); 00225 00226 $item->setFields( $mockFields, true ); 00227 00228 $this->assertFieldValues( $mockFields, $item ); 00229 } 00230 00231 // TODO: test all of the methods! 00232 00233 }