DBALQueryInterfaceTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 182
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 6

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A newTable() 0 20 1
A testWhenPreviousQueryWasNotInsert_getInsertIdReturnsZero() 0 3 1
A testCanInsertAndObtainInsertId() 0 4 1
A testCanSelectInsertedRow() 0 14 1
A testSelectingNonExistentRowResultsInEmptyIterator() 0 14 1
A testUpdateChangesExistingValues() 0 20 1
A testCanSelectWithoutWhereConditions() 0 15 1
A testCanSelectWithMultipleWhereConditions() 0 15 1
A testTableExists() 0 4 1
A testCanDeleteExistingRows() 0 20 1
1
<?php
2
3
namespace Wikibase\Database\Tests\MediaWiki;
4
5
use Doctrine\DBAL\DriverManager;
6
use Doctrine\DBAL\Schema\Table;
7
use Doctrine\DBAL\Types\Type;
8
use Wikibase\Database\Doctrine\DBALQueryInterface;
9
use Wikibase\Database\QueryInterface;
10
11
/**
12
 * @covers Wikibase\Database\Doctrine\DBALQueryInterface
13
 *
14
 * @group Wikibase
15
 * @group WikibaseDatabase
16
 * @group WikibaseDatabaseDoctrine
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class DBALQueryInterfaceTest extends \PHPUnit_Framework_TestCase {
22
23
	/**
24
	 * @var QueryInterface
25
	 */
26
	private $queryInterface;
27
28
	const TABLE_NAME = 'kittens';
29
30
	public function setUp() {
31
		$connection = DriverManager::getConnection( array(
32
			'driver' => 'pdo_sqlite',
33
			'memory' => true,
34
		) );
35
36
		$connection->getSchemaManager()->createTable( $this->newTable() );
37
38
		$this->queryInterface = new DBALQueryInterface( $connection );
39
	}
40
41
	private function newTable() {
42
		$table = new Table( self::TABLE_NAME );
43
44
		$table->addColumn(
45
			'row_id',
46
			Type::INTEGER,
47
			array(
48
				'autoincrement' => true,
49
				'unsigned' => true,
50
			)
51
		);
52
53
		$table->addColumn( 'name', Type::STRING, array( 'length' => 255 ) );
54
		$table->addColumn( 'awesomeness', Type::INTEGER, array( 'default' => 9001 ) );
55
56
		$table->setPrimaryKey( array( 'row_id' ) );
57
		$table->addIndex( array( 'name' ) );
58
59
		return $table;
60
	}
61
62
	public function testWhenPreviousQueryWasNotInsert_getInsertIdReturnsZero() {
63
		$this->assertSame( 0, $this->queryInterface->getInsertId() );
64
	}
65
66
	public function testCanInsertAndObtainInsertId() {
67
		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'maru' ) );
68
		$this->assertSame( 1, $this->queryInterface->getInsertId() );
69
	}
70
71
	public function testCanSelectInsertedRow() {
72
		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'maru' ) );
73
74
		$results = $this->queryInterface->select(
75
			self::TABLE_NAME,
76
			array( 'name' ),
77
			array( 'row_id' => 1 )
78
		);
79
80
		$this->assertSame(
81
			array( array( 'name' => 'maru' ) ),
82
			iterator_to_array( $results )
83
		);
84
	}
85
86
	public function testSelectingNonExistentRowResultsInEmptyIterator() {
87
		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'maru' ) );
88
89
		$results = $this->queryInterface->select(
90
			self::TABLE_NAME,
91
			array( 'name' ),
92
			array( 'name' => 'Ceiling cat' )
93
		);
94
95
		$this->assertSame(
96
			array(),
97
			iterator_to_array( $results )
98
		);
99
	}
100
101
	public function testUpdateChangesExistingValues() {
102
		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'maru' ) );
103
104
		$this->queryInterface->update(
105
			self::TABLE_NAME,
106
			array( 'name' => 'Maru' ),
107
			array( 'name' => 'maru' )
108
		);
109
110
		$results = $this->queryInterface->select(
111
			self::TABLE_NAME,
112
			array( 'name' ),
113
			array( 'row_id' => 1 )
114
		);
115
116
		$this->assertSame(
117
			array( array( 'name' => 'Maru' ) ),
118
			iterator_to_array( $results )
119
		);
120
	}
121
122
	public function testCanSelectWithoutWhereConditions() {
123
		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'maru' ) );
124
		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'Ceiling cat' ) );
125
126
		$results = $this->queryInterface->select(
127
			self::TABLE_NAME,
128
			array( 'name' ),
129
			array()
130
		);
131
132
		$this->assertSame(
133
			array( array( 'name' => 'maru' ), array( 'name' => 'Ceiling cat' ) ),
134
			iterator_to_array( $results )
135
		);
136
	}
137
138
	public function testCanSelectWithMultipleWhereConditions() {
139
		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'maru' ) );
140
		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'maru' ) );
141
142
		$results = $this->queryInterface->select(
143
			self::TABLE_NAME,
144
			array( 'row_id' ),
145
			array( 'name' => 'maru', 'row_id' => 1 )
146
		);
147
148
		$this->assertSame(
149
			array( array( 'row_id' => '1' ) ),
150
			iterator_to_array( $results )
151
		);
152
	}
153
154
	public function testTableExists() {
155
		$this->assertTrue( $this->queryInterface->tableExists( self::TABLE_NAME ) );
156
		$this->assertFalse( $this->queryInterface->tableExists( 'non_existent_table' ) );
157
	}
158
159
	public function testCanDeleteExistingRows() {
160
		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'maru' ) );
161
		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'Ceiling cat' ) );
162
163
		$this->queryInterface->delete(
164
			self::TABLE_NAME,
165
			array( 'name' => 'maru' )
166
		);
167
168
		$results = $this->queryInterface->select(
169
			self::TABLE_NAME,
170
			array( 'name' ),
171
			array()
172
		);
173
174
		$this->assertSame(
175
			array( array( 'name' => 'Ceiling cat' ) ),
176
			iterator_to_array( $results )
177
		);
178
	}
179
180
	// https://github.com/doctrine/dbal/pull/722
181
//	public function testCanDeleteWithoutWhereConditions() {
182
//		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'maru' ) );
183
//		$this->queryInterface->insert( self::TABLE_NAME, array( 'name' => 'Ceiling cat' ) );
184
//
185
//		$this->queryInterface->delete(
186
//			self::TABLE_NAME,
187
//			array()
188
//		);
189
//
190
//		$results = $this->queryInterface->select(
191
//			self::TABLE_NAME,
192
//			array( 'name' ),
193
//			array()
194
//		);
195
//
196
//		$this->assertSame(
197
//			array(),
198
//			iterator_to_array( $results )
199
//		);
200
//	}
201
202
}
203