From: jeroendedauw Date: Thu, 7 Jun 2012 11:18:49 +0000 (+0200) Subject: Added some extra tests for ORMRow class X-Git-Tag: 1.31.0-rc.0~23312^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/categories/modifier.php?a=commitdiff_plain;h=7127713c7820be29424e212a1fc00ace7decaa1a;p=lhc%2Fweb%2Fwiklou.git Added some extra tests for ORMRow class Change-Id: I672c88d42168a3daf405e6f0a4f204c15a4489ad --- diff --git a/includes/db/IORMTable.php b/includes/db/IORMTable.php index 853e8cd253..619faf5aa9 100644 --- a/includes/db/IORMTable.php +++ b/includes/db/IORMTable.php @@ -62,6 +62,9 @@ interface IORMTable { * * array * * blob * + * TODO: get rid of the id field. Every row instance needs to have + * one so this is just causing hassle at various locations by requiring an extra check for field name. + * * @since 1.20 * * @return array diff --git a/tests/phpunit/includes/db/ORMRowTest.php b/tests/phpunit/includes/db/ORMRowTest.php index 9a275be381..9dcaf2b372 100644 --- a/tests/phpunit/includes/db/ORMRowTest.php +++ b/tests/phpunit/includes/db/ORMRowTest.php @@ -79,6 +79,53 @@ abstract class ORMRowTest extends \MediaWikiTestCase { return new $class( $this->getTableInstance(), $data, $loadDefaults ); } + /** + * @since 1.20 + * @return array + */ + protected function getMockValues() { + return array( + 'id' => 1, + 'str' => 'foobar4645645', + 'int' => 42, + 'float' => 4.2, + 'bool' => true, + 'array' => array( 42, 'foobar' ), + 'blob' => new stdClass() + ); + } + + /** + * @since 1.20 + * @return array + */ + protected function getMockFields() { + $mockValues = $this->getMockValues(); + $mockFields = array(); + + foreach ( $this->getTableInstance()->getFields() as $name => $type ) { + if ( $name !== 'id' ) { + $mockFields[$name] = $mockValues[$type]; + } + } + + return $mockFields; + } + + /** + * @since 1.20 + * @return array of IORMRow + */ + public function instanceProvider() { + $instances = array(); + + foreach ( $this->constructorTestProvider() as $arguments ) { + $instances[] = array( call_user_func_array( array( $this, 'getRowInstance' ), $arguments ) ); + } + + return $instances; + } + /** * @dataProvider constructorTestProvider */ @@ -129,6 +176,59 @@ abstract class ORMRowTest extends \MediaWikiTestCase { $this->verifyFields( $item, $data ); } + /** + * @dataProvider instanceProvider + */ + public function testSetField( IORMRow $item ) { + foreach ( $this->getMockFields() as $name => $value ) { + $item->setField( $name, $value ); + $this->assertEquals( $value, $item->getField( $name ) ); + } + } + + /** + * @since 1.20 + * @param array $expected + * @param IORMRow $item + */ + protected function assertFieldValues( array $expected, IORMRow $item ) { + foreach ( $expected as $name => $type ) { + if ( $name !== 'id' ) { + $this->assertEquals( $expected[$name], $item->getField( $name ) ); + } + } + } + + /** + * @dataProvider instanceProvider + */ + public function testSetFields( IORMRow $item ) { + $originalValues = $item->getFields(); + + $item->setFields( array(), false ); + + foreach ( $item->getTable()->getFields() as $name => $type ) { + $originalHas = array_key_exists( $name, $originalValues ); + $newHas = $item->hasField( $name ); + + $this->assertEquals( $originalHas, $newHas ); + + if ( $originalHas && $newHas ) { + $this->assertEquals( $originalValues[$name], $item->getField( $name ) ); + } + } + + $mockFields = $this->getMockFields(); + + $item->setFields( $mockFields, false ); + + $this->assertFieldValues( $originalValues, $item ); + + $item->setFields( $mockFields, true ); + + $this->assertFieldValues( $mockFields, $item ); + } + // TODO: test all of the methods! } \ No newline at end of file