1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class FetchTest extends BridgeTestCase |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
public function testFetchArray() |
6
|
|
|
{ |
7
|
|
|
$query = 'SELECT * FROM test_table WHERE id <= 3'; |
8
|
|
|
$result = $this->bridge->query($query); |
9
|
|
|
|
10
|
|
|
$this->assertEquals( |
11
|
|
|
[ |
12
|
|
|
0 => "1", |
13
|
|
|
1 => "test 1", |
14
|
|
|
"id" => "1", |
15
|
|
|
"testfield" => "test 1", |
16
|
|
|
], |
17
|
|
|
$this->bridge->fetchArray($result) |
|
|
|
|
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
$this->assertEquals( |
21
|
|
|
[ |
22
|
|
|
"id" => "2", |
23
|
|
|
"testfield" => "test 2", |
24
|
|
|
], |
25
|
|
|
$this->bridge->fetchArray($result, MYSQL_ASSOC) |
|
|
|
|
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$this->assertEquals( |
29
|
|
|
[ |
30
|
|
|
0 => "3", |
31
|
|
|
1 => "test 3", |
32
|
|
|
], |
33
|
|
|
$this->bridge->fetchArray($result, MYSQL_NUM) |
|
|
|
|
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$this->assertFalse($this->bridge->fetchArray($result)); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testFetchAssoc() |
40
|
|
|
{ |
41
|
|
|
$query = 'SELECT * FROM test_table WHERE id = 4'; |
42
|
|
|
$result = $this->bridge->query($query); |
43
|
|
|
|
44
|
|
|
$this->assertEquals( |
45
|
|
|
[ |
46
|
|
|
'id' => '4', |
47
|
|
|
'testfield' => 'test 4' |
48
|
|
|
], |
49
|
|
|
$this->bridge->fetchAssoc($result) |
|
|
|
|
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testFetchField() |
54
|
|
|
{ |
55
|
|
|
$query = 'SELECT * FROM test_table'; |
56
|
|
|
$result = $this->bridge->query($query); |
57
|
|
|
|
58
|
|
|
$this->assertAttributeEquals(1, 'primary_key', $this->bridge->fetchField($result)); |
|
|
|
|
59
|
|
|
$this->assertAttributeEquals('id', 'name', $this->bridge->fetchField($result)); |
|
|
|
|
60
|
|
|
$this->assertAttributeEquals('testfield', 'name', $this->bridge->fetchField($result, 1)); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testFetchLengths() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$query = 'SELECT * FROM test_table WHERE id = 10'; |
66
|
|
|
$result = $this->bridge->query($query); |
67
|
|
|
$this->bridge->fetchAssoc($result); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$this->assertEquals([0 => 2, 1 => 7], $this->bridge->fetchLengths($result)); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testFetchObject() |
73
|
|
|
{ |
74
|
|
|
$query = 'SELECT * FROM test_table WHERE id = 5'; |
75
|
|
|
$result = $this->bridge->query($query); |
76
|
|
|
|
77
|
|
|
$object = $this->bridge->fetchObject($result); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->assertInstanceOf(stdClass::class, $object); |
80
|
|
|
$this->assertEquals('5', $object->id); |
81
|
|
|
$this->assertEquals('test 5', $object->testfield); |
82
|
|
|
|
83
|
|
|
$query = 'SELECT * FROM test_table WHERE id = 5'; |
84
|
|
|
$result = $this->bridge->query($query); |
85
|
|
|
|
86
|
|
|
$object = $this->bridge->fetchObject($result, DummyObject::class, [1, 2]); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$this->assertInstanceOf(DummyObject::class, $object); |
89
|
|
|
$this->assertEquals('5', $object->id); |
90
|
|
|
$this->assertEquals('test 5', $object->testfield); |
91
|
|
|
$this->assertEquals(1, $object->one); |
92
|
|
|
$this->assertEquals(2, $object->two); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function testFetchRow() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$query = 'SELECT * FROM test_table WHERE id = 6'; |
98
|
|
|
$result = $this->bridge->query($query); |
99
|
|
|
|
100
|
|
|
$this->assertEquals( |
101
|
|
|
[ |
102
|
|
|
0 => '6', |
103
|
|
|
1 => 'test 6' |
104
|
|
|
], |
105
|
|
|
$this->bridge->fetchRow($result) |
|
|
|
|
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** @expectedException \Mattbit\MysqlCompat\Exception\NotSupportedException */ |
110
|
|
|
public function testDataSeek() |
111
|
|
|
{ |
112
|
|
|
$result = $this->bridge->query('SELECT * FROM test_table'); |
113
|
|
|
|
114
|
|
|
$this->bridge->dataSeek($result, 5); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
class DummyObject { |
|
|
|
|
119
|
|
|
public $one; |
120
|
|
|
public $two; |
121
|
|
|
public function __construct($one, $two) |
122
|
|
|
{ |
123
|
|
|
$this->one = $one; |
124
|
|
|
$this->two = $two; |
125
|
|
|
} |
126
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.