1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests\SQLStore\TableBuilder; |
4
|
|
|
|
5
|
|
|
use SMW\SQLStore\TableBuilder\SQLiteTableBuilder; |
6
|
|
|
use SMW\Tests\TestEnvironment; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \SMW\SQLStore\TableBuilder\SQLiteTableBuilder |
10
|
|
|
* @group semantic-mediawiki |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 2.5 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class SQLiteTableBuilderTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
private $testEnvironment; |
20
|
|
|
|
21
|
|
|
protected function setUp() { |
22
|
|
|
$this->testEnvironment = new TestEnvironment(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testCanConstruct() { |
26
|
|
|
|
27
|
|
|
$connection = $this->getMockBuilder( '\DatabaseBase' ) |
28
|
|
|
->disableOriginalConstructor() |
29
|
|
|
->getMockForAbstractClass(); |
30
|
|
|
|
31
|
|
|
$connection->expects( $this->any() ) |
32
|
|
|
->method( 'getType' ) |
33
|
|
|
->will( $this->returnValue( 'sqlite' ) ); |
34
|
|
|
|
35
|
|
|
$this->assertInstanceOf( |
36
|
|
|
'\SMW\SQLStore\TableBuilder\SQLiteTableBuilder', |
37
|
|
|
SQLiteTableBuilder::factory( $connection ) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCreateTableOnNewTable() { |
42
|
|
|
|
43
|
|
|
$connection = $this->getMockBuilder( '\DatabaseBase' ) |
44
|
|
|
->disableOriginalConstructor() |
45
|
|
|
->setMethods( array( 'tableExists', 'query' ) ) |
46
|
|
|
->getMockForAbstractClass(); |
47
|
|
|
|
48
|
|
|
$connection->expects( $this->any() ) |
49
|
|
|
->method( 'getType' ) |
50
|
|
|
->will( $this->returnValue( 'sqlite' ) ); |
51
|
|
|
|
52
|
|
|
$connection->expects( $this->any() ) |
53
|
|
|
->method( 'tableExists' ) |
54
|
|
|
->will( $this->returnValue( false ) ); |
55
|
|
|
|
56
|
|
|
$connection->expects( $this->once() ) |
57
|
|
|
->method( 'query' ) |
58
|
|
|
->with( $this->stringContains( 'CREATE TABLE' ) ); |
59
|
|
|
|
60
|
|
|
$instance = SQLiteTableBuilder::factory( $connection ); |
61
|
|
|
|
62
|
|
|
$tableOptions = array( |
63
|
|
|
'fields' => array( 'bar' => 'text' ) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$instance->createTable( 'foo', $tableOptions ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testUpdateTableOnOldTable() { |
70
|
|
|
|
71
|
|
|
$connection = $this->getMockBuilder( '\DatabaseBase' ) |
72
|
|
|
->disableOriginalConstructor() |
73
|
|
|
->setMethods( array( 'tableExists', 'query' ) ) |
74
|
|
|
->getMockForAbstractClass(); |
75
|
|
|
|
76
|
|
|
$connection->expects( $this->any() ) |
77
|
|
|
->method( 'getType' ) |
78
|
|
|
->will( $this->returnValue( 'sqlite' ) ); |
79
|
|
|
|
80
|
|
|
$connection->expects( $this->any() ) |
81
|
|
|
->method( 'tableExists' ) |
82
|
|
|
->will( $this->returnValue( true ) ); |
83
|
|
|
|
84
|
|
|
$connection->expects( $this->at( 2 ) ) |
85
|
|
|
->method( 'query' ) |
86
|
|
|
->with( $this->stringContains( 'PRAGMA table_info("foo")' ) ) |
87
|
|
|
->will( $this->returnValue( array() ) ); |
88
|
|
|
|
89
|
|
|
$connection->expects( $this->at( 3 ) ) |
90
|
|
|
->method( 'query' ) |
91
|
|
|
->with( $this->stringContains( 'ALTER TABLE "foo" ADD `bar` text' ) ); |
92
|
|
|
|
93
|
|
|
$instance = SQLiteTableBuilder::factory( $connection ); |
94
|
|
|
|
95
|
|
|
$tableOptions = array( |
96
|
|
|
'fields' => array( 'bar' => 'text' ) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$instance->createTable( 'foo', $tableOptions ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testCreateIndex() { |
103
|
|
|
|
104
|
|
|
$connection = $this->getMockBuilder( '\DatabaseBase' ) |
105
|
|
|
->disableOriginalConstructor() |
106
|
|
|
->setMethods( array( 'tableExists', 'query' ) ) |
107
|
|
|
->getMockForAbstractClass(); |
108
|
|
|
|
109
|
|
|
$connection->expects( $this->any() ) |
110
|
|
|
->method( 'getType' ) |
111
|
|
|
->will( $this->returnValue( 'sqlite' ) ); |
112
|
|
|
|
113
|
|
|
$connection->expects( $this->any() ) |
114
|
|
|
->method( 'tableExists' ) |
115
|
|
|
->will( $this->returnValue( false ) ); |
116
|
|
|
|
117
|
|
|
$connection->expects( $this->at( 1 ) ) |
118
|
|
|
->method( 'query' ) |
119
|
|
|
->with( $this->stringContains( 'PRAGMA index_list("foo")' ) ) |
120
|
|
|
->will( $this->returnValue( array() ) ); |
121
|
|
|
|
122
|
|
|
$connection->expects( $this->at( 2 ) ) |
123
|
|
|
->method( 'query' ) |
124
|
|
|
->with( $this->stringContains( 'CREATE INDEX "foo"_index0' ) ); |
125
|
|
|
|
126
|
|
|
$instance = SQLiteTableBuilder::factory( $connection ); |
127
|
|
|
|
128
|
|
|
$indexOptions = array( |
129
|
|
|
'indicies' => array( 'bar' ) |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$instance->createIndex( 'foo', $indexOptions ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testDropTable() { |
136
|
|
|
|
137
|
|
|
$connection = $this->getMockBuilder( '\DatabaseBase' ) |
138
|
|
|
->disableOriginalConstructor() |
139
|
|
|
->setMethods( array( 'tableExists', 'query' ) ) |
140
|
|
|
->getMockForAbstractClass(); |
141
|
|
|
|
142
|
|
|
$connection->expects( $this->any() ) |
143
|
|
|
->method( 'getType' ) |
144
|
|
|
->will( $this->returnValue( 'sqlite' ) ); |
145
|
|
|
|
146
|
|
|
$connection->expects( $this->once() ) |
147
|
|
|
->method( 'tableExists' ) |
148
|
|
|
->will( $this->returnValue( true ) ); |
149
|
|
|
|
150
|
|
|
$connection->expects( $this->once() ) |
151
|
|
|
->method( 'query' ) |
152
|
|
|
->with( $this->stringContains( 'DROP TABLE "foo"' ) ); |
153
|
|
|
|
154
|
|
|
$instance = SQLiteTableBuilder::factory( $connection ); |
155
|
|
|
|
156
|
|
|
$instance->dropTable( 'foo' ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|