Merge branch 'Wikidata' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[lhc/web/wiklou.git] / tests / phpunit / includes / db / ORMRowTest.php
1 <?php
2
3 /**
4 * Abstract class to construct tests for ORMRow deriving classes.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @since 1.20
23 *
24 * @ingroup Test
25 *
26 * @group ORM
27 *
28 * The database group has as a side effect that temporal database tables are created. This makes
29 * it possible to test without poisoning a production database.
30 * @group Database
31 *
32 * Some of the tests takes more time, and needs therefor longer time before they can be aborted
33 * as non-functional. The reason why tests are aborted is assumed to be set up of temporal databases
34 * that hold the first tests in a pending state awaiting access to the database.
35 * @group medium
36 *
37 * @licence GNU GPL v2+
38 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
39 */
40 abstract class ORMRowTest extends \MediaWikiTestCase {
41
42 /**
43 * @since 1.20
44 * @return string
45 */
46 protected abstract function getRowClass();
47
48 /**
49 * @since 1.20
50 * @return IORMTable
51 */
52 protected abstract function getTableInstance();
53
54 /**
55 * @since 1.20
56 * @return array
57 */
58 public abstract function constructorTestProvider();
59
60 /**
61 * @since 1.20
62 * @param IORMRow $row
63 * @param array $data
64 */
65 protected function verifyFields( IORMRow $row, array $data ) {
66 foreach ( array_keys( $data ) as $fieldName ) {
67 $this->assertEquals( $data[$fieldName], $row->getField( $fieldName ) );
68 }
69 }
70
71 /**
72 * @since 1.20
73 * @param array $data
74 * @param boolean $loadDefaults
75 * @return IORMRow
76 */
77 protected function getRowInstance( array $data, $loadDefaults ) {
78 $class = $this->getRowClass();
79 return new $class( $this->getTableInstance(), $data, $loadDefaults );
80 }
81
82 /**
83 * @dataProvider constructorTestProvider
84 */
85 public function testConstructor( array $data, $loadDefaults ) {
86 $this->verifyFields( $this->getRowInstance( $data, $loadDefaults ), $data );
87 }
88
89 /**
90 * @dataProvider constructorTestProvider
91 */
92 public function testSave( array $data, $loadDefaults ) {
93 $item = $this->getRowInstance( $data, $loadDefaults );
94
95 $this->assertTrue( $item->save() );
96
97 $this->assertTrue( $item->hasIdField() );
98 $this->assertTrue( is_integer( $item->getId() ) );
99
100 $id = $item->getId();
101
102 $this->assertTrue( $item->save() );
103
104 $this->assertEquals( $id, $item->getId() );
105
106 $this->verifyFields( $item, $data );
107 }
108
109 /**
110 * @dataProvider constructorTestProvider
111 */
112 public function testRemove( array $data, $loadDefaults ) {
113 $item = $this->getRowInstance( $data, $loadDefaults );
114
115 $this->assertTrue( $item->save() );
116
117 $this->assertTrue( $item->remove() );
118
119 $this->assertFalse( $item->hasIdField() );
120
121 $this->assertTrue( $item->save() );
122
123 $this->verifyFields( $item, $data );
124
125 $this->assertTrue( $item->remove() );
126
127 $this->assertFalse( $item->hasIdField() );
128
129 $this->verifyFields( $item, $data );
130 }
131
132 // TODO: test all of the methods!
133
134 }