Issues (137)

tests/SphinxQL/ResultSetTest.php (1 issue)

1
<?php
2
3
use Foolz\SphinxQL\Drivers\Mysqli\Connection;
4
use Foolz\Sphinxql\Drivers\ResultSetInterface;
5
use Foolz\SphinxQL\SphinxQL;
6
use Foolz\SphinxQL\Tests\TestUtil;
7
8
class ResultSetTest extends \PHPUnit\Framework\TestCase
9
{
10
    /**
11
     * @var Connection
12
     */
13
    public static $conn = null;
14
15
    public static $data = array(
16
        0 => array('id' => '10', 'gid' => '9003',
17
            'title' => 'modifying the same line again', 'content' => 'because i am that lazy'),
18
        1 => array('id' => '11', 'gid' => '201',
19
            'title' => 'replacing value by value', 'content' => 'i have no idea who would use this directly'),
20
        2 => array('id' => '12', 'gid' => '200',
21
            'title' => 'simple logic', 'content' => 'inside the box there was the content'),
22
        3 => array('id' => '13', 'gid' => '304',
23
            'title' => 'i am getting bored', 'content' => 'with all this CONTENT'),
24
        4 => array('id' => '14', 'gid' => '304',
25
            'title' => 'i want a vacation', 'content' => 'the code is going to break sometime'),
26
        5 => array('id' => '15', 'gid' => '304',
27
            'title' => 'there\'s no hope in this class', 'content' => 'just give up'),
28
        6 => array('id' => '16', 'gid' => '500',
29
            'title' => 'we need to test', 'content' => 'selecting the best result in groups'),
30
        7 => array('id' => '17', 'gid' => '500',
31
            'title' => 'what is there to do', 'content' => 'we need to create dummy data for tests'),
32
    );
33
34
    public static function setUpBeforeClass(): void
35
    {
36
        $conn = TestUtil::getConnectionDriver();
37
        $conn->setParam('port', 9307);
38
        self::$conn = $conn;
0 ignored issues
show
Documentation Bug introduced by
It seems like $conn can also be of type Foolz\SphinxQL\Drivers\Pdo\Connection. However, the property $conn is declared as type Foolz\SphinxQL\Drivers\Mysqli\Connection. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
39
40
        (new SphinxQL(self::$conn))->getConnection()->query('TRUNCATE RTINDEX rt');
41
    }
42
43
    /**
44
     * @return SphinxQL
45
     */
46
    protected function createSphinxQL()
47
    {
48
        return new SphinxQL(self::$conn);
49
    }
50
51
    public function refill()
52
    {
53
        $this->createSphinxQL()->getConnection()->query('TRUNCATE RTINDEX rt');
54
55
        $sq = $this->createSphinxQL()
56
            ->insert()
57
            ->into('rt')
58
            ->columns('id', 'gid', 'title', 'content');
59
60
        foreach (static::$data as $row) {
61
            $sq->values($row['id'], $row['gid'], $row['title'], $row['content']);
62
        }
63
64
        $sq->execute();
65
    }
66
67
    public function testIsResultSet()
68
    {
69
        $res = self::$conn->query('SELECT * FROM rt');
70
        $this->assertInstanceOf(ResultSetInterface::class, $res);
71
    }
72
73
    public function testStore()
74
    {
75
        $this->refill();
76
        $res = self::$conn->query('SELECT * FROM rt');
77
        $res->store()->store();
78
        $this->assertCount(8, $res->fetchAllNum());
79
80
        $res = self::$conn->query('UPDATE rt SET gid = 202 WHERE gid < 202');
81
        $this->assertEquals(2, $res->store()->getStored());
82
    }
83
84
    public function testHasRow()
85
    {
86
        $this->refill();
87
        $res = self::$conn->query('SELECT * FROM rt');
88
        $this->assertTrue($res->hasRow(2));
89
        $this->assertTrue(isset($res[2]));
90
        $this->assertFalse($res->hasRow(1000));
91
        $this->assertFalse(isset($res[1000]));
92
        $res->freeResult();
93
    }
94
95
    public function testToRow()
96
    {
97
        $this->refill();
98
        $res = self::$conn->query('SELECT * FROM rt');
99
        $res->toRow(2);
100
        $row = $res->fetchAssoc();
101
        $this->assertEquals(12, $row['id']);
102
        $res->freeResult();
103
    }
104
105
    public function testToRowThrows()
106
    {
107
        $this->expectException(Foolz\SphinxQL\Exception\ResultSetException::class);
108
        $this->expectExceptionMessage("The row does not exist.");
109
        $this->refill();
110
        $res = self::$conn->query('SELECT * FROM rt');
111
        $res->toRow(8);
112
    }
113
114
    public function testHasNextRow()
115
    {
116
        $this->refill();
117
        $res = self::$conn->query('SELECT * FROM rt');
118
        $this->assertTrue($res->hasNextRow());
119
        $res->freeResult();
120
        $res = self::$conn->query('SELECT * FROM rt WHERE id = 9000');
121
        $this->assertFalse($res->hasNextRow());
122
        $res->freeResult();
123
    }
124
125
    public function testToNextRow()
126
    {
127
        $this->refill();
128
        $res = self::$conn->query('SELECT * FROM rt');
129
        $res->toNextRow()->toNextRow()->toNextRow();
130
        $row = $res->fetchAssoc();
131
        $this->assertEquals(13, $row['id']);
132
        $res->freeResult();
133
    }
134
135
    public function testToNextRowThrows()
136
    {
137
        $this->expectException(Foolz\SphinxQL\Exception\ResultSetException::class);
138
        $this->expectExceptionMessage("The row does not exist.");
139
        $this->refill();
140
        $res = self::$conn->query('SELECT * FROM rt WHERE id = 10');
141
        $res->toNextRow()->toNextRow();
142
    }
143
144
    public function testCount()
145
    {
146
        $this->refill();
147
        $res = self::$conn->query('SELECT * FROM rt');
148
        $this->assertEquals(8, $res->count());
149
    }
150
151
    public function testFetchAllAssoc()
152
    {
153
        $expect = array(
154
            0 => array(
155
                'id' => '10',
156
                'gid' => '9003'
157
            ),
158
            1 => array(
159
                'id' => '11',
160
                'gid' => '201'
161
            )
162
        );
163
164
165
        $this->refill();
166
        $res = self::$conn->query('SELECT * FROM rt');
167
        $array = $res->fetchAllAssoc();
168
        $this->assertSame($expect[0], $array[0]);
169
        $this->assertSame($expect[1], $array[1]);
170
    }
171
172
    public function testFetchAssoc()
173
    {
174
        $expect = array(
175
            0 => array(
176
                'id' => '10',
177
                'gid' => '9003'
178
            ),
179
            1 => array(
180
                'id' => '11',
181
                'gid' => '201'
182
            )
183
        );
184
185
186
        $this->refill();
187
        $res = self::$conn->query('SELECT * FROM rt');
188
        $this->assertSame($expect[0], $res->fetchAssoc());
189
        $this->assertSame($expect[1], $res->fetchAssoc());
190
        $res->fetchAssoc();
191
        $res->fetchAssoc();
192
        $res->fetchAssoc();
193
        $res->fetchAssoc();
194
        $res->fetchAssoc();
195
        $res->fetchAssoc();
196
        $this->assertNull($res->fetchAssoc());
197
198
        $res = self::$conn->query('SELECT * FROM rt')->store();
199
        $this->assertSame($expect[0], $res->fetchAssoc());
200
        $this->assertSame($expect[1], $res->fetchAssoc());
201
        $res->fetchAssoc();
202
        $res->fetchAssoc();
203
        $res->fetchAssoc();
204
        $res->fetchAssoc();
205
        $res->fetchAssoc();
206
        $res->fetchAssoc();
207
        $this->assertNull($res->fetchAssoc());
208
    }
209
210
    public function testFetchAllNum()
211
    {
212
        $expect = array(
213
            0 => array(
214
                0 => '10',
215
                1 => '9003'
216
            ),
217
            1 => array(
218
                0 => '11',
219
                1 => '201'
220
            )
221
        );
222
223
        $this->refill();
224
        $res = self::$conn->query('SELECT * FROM rt LIMIT 2');
225
        $array = $res->fetchAllNum();
226
        $this->assertSame($expect, $array);
227
228
        $res = self::$conn->query('SELECT * FROM rt LIMIT 2');
229
        $array = $res->store()->fetchAllNum();
230
        $this->assertSame($expect, $array);
231
    }
232
233
    public function testFetchNum()
234
    {
235
        $expect = array(
236
            0 => array(
237
                0 => '10',
238
                1 => '9003'
239
            ),
240
            1 => array(
241
                0 => '11',
242
                1 => '201'
243
            )
244
        );
245
246
        $this->refill();
247
        $res = self::$conn->query('SELECT * FROM rt');
248
        $this->assertSame($expect[0], $res->fetchNum());
249
        $this->assertSame($expect[1], $res->fetchNum());
250
        $res->fetchNum();
251
        $res->fetchNum();
252
        $res->fetchNum();
253
        $res->fetchNum();
254
        $res->fetchNum();
255
        $res->fetchNum();
256
        $this->assertNull($res->fetchNum());
257
258
        $res = self::$conn->query('SELECT * FROM rt')->store();
259
        $this->assertSame($expect[0], $res->fetchNum());
260
        $this->assertSame($expect[1], $res->fetchNum());
261
        $res->fetchNum();
262
        $res->fetchNum();
263
        $res->fetchNum();
264
        $res->fetchNum();
265
        $res->fetchNum();
266
        $res->fetchNum();
267
        $this->assertNull($res->fetchNum());
268
    }
269
270
    public function testGetAffectedRows()
271
    {
272
        $this->refill();
273
        $res = self::$conn->query('UPDATE rt SET gid=0 WHERE id > 0');
274
        $this->assertSame(8, $res->getAffectedRows());
275
    }
276
277
    public function testArrayAccess()
278
    {
279
        $expect = array(
280
            0 => array(
281
                'id' => '10',
282
                'gid' => '9003'
283
            ),
284
            1 => array(
285
                'id' => '11',
286
                'gid' => '201'
287
            )
288
        );
289
290
291
        $this->refill();
292
        $res = self::$conn->query('SELECT * FROM rt');
293
        $this->assertSame($expect[0], $res[0]);
294
        $this->assertSame($expect[1], $res[1]);
295
    }
296
297
    public function testCountable()
298
    {
299
        $this->refill();
300
        $res = self::$conn->query('SELECT * FROM rt');
301
        $this->assertEquals($res->count(), count($res));
302
    }
303
304
    public function testIterator()
305
    {
306
        $expect = array(
307
            0 => array(
308
                'id' => '10',
309
                'gid' => '9003'
310
            ),
311
            1 => array(
312
                'id' => '11',
313
                'gid' => '201'
314
            )
315
        );
316
317
        $this->refill();
318
        $res = self::$conn->query('SELECT * FROM rt');
319
        $array = array();
320
        foreach ($res as $key => $value) {
321
            $array[$key] = $value;
322
        }
323
324
        $this->assertSame($expect[0], $array[0]);
325
        $this->assertSame($expect[1], $array[1]);
326
327
        $res = self::$conn->query('SELECT * FROM rt WHERE id = 404');
328
        $array = array();
329
        foreach ($res as $key => $value) {
330
            $array[$key] = $value;
331
        }
332
        $this->assertEmpty($array);
333
    }
334
}
335