MultiResultSetTest   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 94
dl 0
loc 173
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A createSphinxQL() 0 3 1
A testIsMultiResultSet() 0 6 1
A testStore() 0 11 1
A refill() 0 14 2
A setUpBeforeClass() 0 7 1
A testGetNextSetFalse() 0 8 1
A testGetNextSet() 0 18 1
A testInvalidStore() 0 13 2
A testIteratorStored() 0 23 3
A testIterator() 0 12 2
A testArrayAccess() 0 7 1
1
<?php
2
3
use Foolz\SphinxQL\Drivers\MultiResultSetInterface;
4
use Foolz\SphinxQL\Drivers\Mysqli\Connection as MysqliConnection;
5
use Foolz\SphinxQL\Drivers\Pdo\Connection as PdoConnection;
6
use Foolz\Sphinxql\Drivers\ResultSetInterface;
7
use Foolz\SphinxQL\Exception\DatabaseException;
8
use Foolz\SphinxQL\SphinxQL;
9
use Foolz\SphinxQL\Tests\TestUtil;
10
11
class MultiResultSetTest extends \PHPUnit\Framework\TestCase
12
{
13
    /**
14
     * @var MysqliConnection|PdoConnection
15
     */
16
    public static $conn = null;
17
18
    public static $data = array(
19
        0 => array('id' => '10', 'gid' => '9003',
20
            'title' => 'modifying the same line again', 'content' => 'because i am that lazy'),
21
        1 => array('id' => '11', 'gid' => '201',
22
            'title' => 'replacing value by value', 'content' => 'i have no idea who would use this directly'),
23
        2 => array('id' => '12', 'gid' => '200',
24
            'title' => 'simple logic', 'content' => 'inside the box there was the content'),
25
        3 => array('id' => '13', 'gid' => '304',
26
            'title' => 'i am getting bored', 'content' => 'with all this CONTENT'),
27
        4 => array('id' => '14', 'gid' => '304',
28
            'title' => 'i want a vacation', 'content' => 'the code is going to break sometime'),
29
        5 => array('id' => '15', 'gid' => '304',
30
            'title' => 'there\'s no hope in this class', 'content' => 'just give up'),
31
        6 => array('id' => '16', 'gid' => '500',
32
            'title' => 'we need to test', 'content' => 'selecting the best result in groups'),
33
        7 => array('id' => '17', 'gid' => '500',
34
            'title' => 'what is there to do', 'content' => 'we need to create dummy data for tests'),
35
    );
36
37
    public static function setUpBeforeClass(): void
38
    {
39
        $conn = TestUtil::getConnectionDriver();
40
        $conn->setParam('port', 9307);
41
        self::$conn = $conn;
42
43
        (new SphinxQL(self::$conn))->getConnection()->query('TRUNCATE RTINDEX rt');
44
    }
45
46
    /**
47
     * @return SphinxQL
48
     */
49
    protected function createSphinxQL()
50
    {
51
        return new SphinxQL(self::$conn);
52
    }
53
54
    public function refill()
55
    {
56
        $this->createSphinxQL()->getConnection()->query('TRUNCATE RTINDEX rt');
57
58
        $sq = $this->createSphinxQL()
59
            ->insert()
60
            ->into('rt')
61
            ->columns('id', 'gid', 'title', 'content');
62
63
        foreach (static::$data as $row) {
64
            $sq->values($row['id'], $row['gid'], $row['title'], $row['content']);
65
        }
66
67
        $sq->execute();
68
    }
69
70
    public function testIsMultiResultSet()
71
    {
72
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
73
        $this->assertInstanceOf(MultiResultSetInterface::class, $res);
74
        $res->getNext();
75
        $res->getNext();
76
    }
77
78
    public function testGetNextSet()
79
    {
80
        $this->refill();
81
82
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
83
84
        $set = $res->getNext();
85
        $this->assertInstanceOf(ResultSetInterface::class, $set);
86
        $set = $res->getNext();
87
        $this->assertInstanceOf(ResultSetInterface::class, $set);
88
89
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
90
        $res->store();
91
        $set = $res->getNext();
92
        $this->assertInstanceOf(ResultSetInterface::class, $set);
93
        $set = $res->getNext();
94
        $this->assertInstanceOf(ResultSetInterface::class, $set);
95
        $this->assertFalse($res->getNext());
96
    }
97
98
99
    public function testGetNextSetFalse()
100
    {
101
        $this->refill();
102
103
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
104
        $res->getNext();
105
        $res->getNext();
106
        $this->assertFalse($res->getNext());
107
    }
108
109
    public function testStore()
110
    {
111
        $this->refill();
112
113
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
114
        $res->store();
115
        $stored = $res->getStored();
116
        $this->assertCount(2, $stored);
117
        $this->assertInstanceOf(ResultSetInterface::class, $stored[0]);
118
        $all = $stored[0]->fetchAllAssoc();
119
        $this->assertEquals(8, $all[0]['count(*)']);
120
    }
121
122
    public function testInvalidStore()
123
    {
124
        $this->expectException(Foolz\SphinxQL\Exception\DatabaseException::class);
125
        $this->refill();
126
127
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
128
        $res->getNext();
129
        try {
130
            $res->store();
131
        } catch (DatabaseException $e) {
132
            // we need to clean up
133
            self::setUpBeforeClass();
134
            throw $e;
135
        }
136
    }
137
138
    public function testArrayAccess()
139
    {
140
        $this->refill();
141
142
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
143
144
        $this->assertEquals(8, $res[0][0]['count(*)']);
145
    }
146
147
    public function testIterator()
148
    {
149
        $this->refill();
150
151
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
152
153
        $array = array();
154
        foreach($res as $key => $value) {
155
            $array[$key] = $value;
156
        }
157
158
        $this->assertCount(2, $array);
159
    }
160
161
    public function testIteratorStored()
162
    {
163
        $this->refill();
164
165
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
166
        $res->store();
167
        $array = array();
168
        foreach($res as $key => $value) {
169
            $array[$key] = $value;
170
        }
171
172
        $this->assertCount(2, $array);
173
174
        foreach($res as $key => $value) {
175
            $array[$key] = $value;
176
        }
177
178
        $this->assertCount(2, $array);
179
180
        $this->assertCount(2, $res);
181
        $this->assertTrue(isset($res[0]));
182
        $this->assertFalse(isset($res[-1]));
183
        $this->assertFalse(isset($res[2]));
184
    }
185
}
186