Passed
Branch master (51e64f)
by Hung
02:02
created

MultiResultSetTest::testInvalidStore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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
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...
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()
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());
0 ignored issues
show
Bug introduced by
It seems like $res->getNext() can also be of type Foolz\SphinxQL\Drivers\ResultSetInterface; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        $this->assertFalse(/** @scrutinizer ignore-type */ $res->getNext());
Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like $res->getNext() can also be of type Foolz\SphinxQL\Drivers\ResultSetInterface; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
        $this->assertFalse(/** @scrutinizer ignore-type */ $res->getNext());
Loading history...
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
    /**
123
     * @expectedException Foolz\SphinxQL\Exception\DatabaseException
124
     */
125
    public function testInvalidStore()
126
    {
127
        $this->refill();
128
129
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
130
        $res->getNext();
131
        try {
132
            $res->store();
133
        } catch (DatabaseException $e) {
134
            // we need to clean up
135
            self::setUpBeforeClass();
136
            throw $e;
137
        }
138
    }
139
140
    public function testArrayAccess()
141
    {
142
        $this->refill();
143
144
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
145
146
        $this->assertEquals(8, $res[0][0]['count(*)']);
147
    }
148
149
    public function testIterator()
150
    {
151
        $this->refill();
152
153
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
154
155
        $array = array();
156
        foreach($res as $key => $value) {
157
            $array[$key] = $value;
158
        }
159
160
        $this->assertCount(2, $array);
161
    }
162
163
    public function testIteratorStored()
164
    {
165
        $this->refill();
166
167
        $res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
168
        $res->store();
169
        $array = array();
170
        foreach($res as $key => $value) {
171
            $array[$key] = $value;
172
        }
173
174
        $this->assertCount(2, $array);
175
176
        foreach($res as $key => $value) {
177
            $array[$key] = $value;
178
        }
179
180
        $this->assertCount(2, $array);
181
182
        $this->assertCount(2, $res);
183
        $this->assertTrue(isset($res[0]));
184
        $this->assertFalse(isset($res[-1]));
185
        $this->assertFalse(isset($res[2]));
186
    }
187
}
188