Passed
Push — ci-build-matrix ( 1c3307...9a015f )
by Hung
02:21
created

MultiResultSetTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 175
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testIsMultiResultSet() 0 6 1
A testStore() 0 11 1
A createSphinxQL() 0 3 1
A testInvalidStore() 0 12 2
A testIteratorStored() 0 23 3
A testIterator() 0 12 2
A refill() 0 14 2
A testArrayAccess() 0 7 1
A setUpBeforeClass() 0 7 1
A testGetNextSetFalse() 0 8 1
A testGetNextSet() 0 18 1
1
<?php
2
3
use Foolz\SphinxQL\SphinxQL;
4
use Foolz\SphinxQL\Tests\TestUtil;
5
use Foolz\SphinxQL\Exception\DatabaseException;
6
use Foolz\SphinxQL\Drivers\MultiResultSetInterface;
7
use Foolz\Sphinxql\Drivers\ResultSetInterface;
8
9
class MultiResultSetTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    /**
12
     * @var Connection
13
     */
14
    public static $conn = null;
15
16
    public static $data = array(
17
        0 => array('id' => '10', 'gid' => '9003',
18
            'title' => 'modifying the same line again', 'content' => 'because i am that lazy'),
19
        1 => array('id' => '11', 'gid' => '201',
20
            'title' => 'replacing value by value', 'content' => 'i have no idea who would use this directly'),
21
        2 => array('id' => '12', 'gid' => '200',
22
            'title' => 'simple logic', 'content' => 'inside the box there was the content'),
23
        3 => array('id' => '13', 'gid' => '304',
24
            'title' => 'i am getting bored', 'content' => 'with all this CONTENT'),
25
        4 => array('id' => '14', 'gid' => '304',
26
            'title' => 'i want a vacation', 'content' => 'the code is going to break sometime'),
27
        5 => array('id' => '15', 'gid' => '304',
28
            'title' => 'there\'s no hope in this class', 'content' => 'just give up'),
29
        6 => array('id' => '16', 'gid' => '500',
30
            'title' => 'we need to test', 'content' => 'selecting the best result in groups'),
31
        7 => array('id' => '17', 'gid' => '500',
32
            'title' => 'what is there to do', 'content' => 'we need to create dummy data for tests'),
33
    );
34
35
    public static function setUpBeforeClass()
36
    {
37
        $conn = TestUtil::getConnectionDriver();
38
        $conn->setParam('port', 9307);
39
        self::$conn = $conn;
40
41
        (new SphinxQL(self::$conn))->getConnection()->query('TRUNCATE RTINDEX rt');
42
    }
43
44
    /**
45
     * @return SphinxQL
46
     */
47
    protected function createSphinxQL()
48
    {
49
        return new SphinxQL(self::$conn);
50
    }
51
52
    public function refill()
53
    {
54
        $this->createSphinxQL()->getConnection()->query('TRUNCATE RTINDEX rt');
55
56
        $sq = $this->createSphinxQL()
57
            ->insert()
58
            ->into('rt')
59
            ->columns('id', 'gid', 'title', 'content');
60
61
        foreach (static::$data as $row) {
62
            $sq->values($row['id'], $row['gid'], $row['title'], $row['content']);
63
        }
64
65
        $sq->execute();
66
    }
67
68
    public function testIsMultiResultSet()
69
    {
70
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
71
        $this->assertInstanceOf(MultiResultSetInterface::class, $res);
72
        $res->getNext();
73
        $res->getNext();
74
    }
75
76
    public function testGetNextSet()
77
    {
78
        $this->refill();
79
80
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
81
82
        $set = $res->getNext();
83
        $this->assertInstanceOf(ResultSetInterface::class, $set);
84
        $set = $res->getNext();
85
        $this->assertInstanceOf(ResultSetInterface::class, $set);
86
87
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
88
        $res->store();
89
        $set = $res->getNext();
90
        $this->assertInstanceOf(ResultSetInterface::class, $set);
91
        $set = $res->getNext();
92
        $this->assertInstanceOf(ResultSetInterface::class, $set);
93
        $this->assertFalse($res->getNext());
94
    }
95
96
97
    public function testGetNextSetFalse()
98
    {
99
        $this->refill();
100
101
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
102
        $res->getNext();
103
        $res->getNext();
104
        $this->assertFalse($res->getNext());
105
    }
106
107
    public function testStore()
108
    {
109
        $this->refill();
110
111
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
112
        $res->store();
113
        $stored = $res->getStored();
114
        $this->assertCount(2, $stored);
115
        $this->assertInstanceOf(ResultSetInterface::class, $stored[0]);
116
        $all = $stored[0]->fetchAllAssoc();
117
        $this->assertEquals(8, $all[0]['count(*)']);
118
    }
119
120
    /**
121
     * @expectedException Foolz\SphinxQL\Exception\DatabaseException
122
     */
123
    public function testInvalidStore()
124
    {
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