Skip to content

Commit 4312814

Browse files
committed
Improve test coverage
classes 33% -> 54%, methods 62% -> 81%, lines 75% -> 93%
1 parent ded765b commit 4312814

12 files changed

Lines changed: 170 additions & 26 deletions

src/Drivers/Mysqli/Connection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ public function escape($value)
255255
$this->ping();
256256

257257
if (($value = $this->getConnection()->real_escape_string((string) $value)) === false) {
258+
// @codeCoverageIgnoreStart
258259
throw new DatabaseException($this->getConnection()->error, $this->getConnection()->errno);
260+
// @codeCoverageIgnoreEnd
259261
}
260262

261263
return "'".$value."'";

src/Drivers/Mysqli/MultiResultSet.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function store()
3939
}
4040

4141
// don't let users mix storage and mysqli cursors
42-
if ($this->cursor > 0) {
42+
if ($this->cursor !== null) {
4343
throw new DatabaseException('The MultiResultSet is using the mysqli cursors, store() can\'t fetch all the data');
4444
}
4545

@@ -65,16 +65,16 @@ public function getStored()
6565
public function getNext()
6666
{
6767
if ($this->stored !== null) {
68-
if ($this->cursor >= count($this->stored)) {
69-
return false;
70-
}
71-
7268
if ($this->cursor === null) {
7369
$this->cursor = 0;
7470
} else {
7571
$this->cursor++;
7672
}
7773

74+
if ($this->cursor >= count($this->stored)) {
75+
return false;
76+
}
77+
7878
return $this->stored[$this->cursor];
7979
} else {
8080
// the first result is always already loaded
@@ -141,6 +141,8 @@ public function offsetGet($offset)
141141
* The value to set.
142142
* </p>
143143
* @return void
144+
*
145+
* @codeCoverageIgnore
144146
*/
145147
public function offsetSet($offset, $value)
146148
{
@@ -155,6 +157,8 @@ public function offsetSet($offset, $value)
155157
* The offset to unset.
156158
* </p>
157159
* @return void
160+
*
161+
* @codeCoverageIgnore
158162
*/
159163
public function offsetUnset($offset)
160164
{

src/Drivers/Mysqli/ResultSet.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ public function offsetGet($offset)
337337
* The value to set.
338338
* </p>
339339
* @return void
340+
*
341+
* @codeCoverageIgnore
340342
*/
341343
public function offsetSet($offset, $value)
342344
{
@@ -351,6 +353,8 @@ public function offsetSet($offset, $value)
351353
* The offset to unset.
352354
* </p>
353355
* @return void
356+
*
357+
* @codeCoverageIgnore
354358
*/
355359
public function offsetUnset($offset)
356360
{

src/Drivers/Pdo/MultiResultSet.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function store()
4343
}
4444

4545
// don't let users mix storage and pdo cursors
46-
if ($this->cursor > 0) {
46+
if ($this->cursor !== null) {
4747
throw new DatabaseException('The MultiResultSet is using the pdo cursors, store() can\'t fetch all the data');
4848
}
4949

@@ -141,6 +141,8 @@ public function offsetGet($offset)
141141
* The value to set.
142142
* </p>
143143
* @return void
144+
*
145+
* @codeCoverageIgnore
144146
*/
145147
public function offsetSet($offset, $value)
146148
{
@@ -155,6 +157,8 @@ public function offsetSet($offset, $value)
155157
* The offset to unset.
156158
* </p>
157159
* @return void
160+
*
161+
* @codeCoverageIgnore
158162
*/
159163
public function offsetUnset($offset)
160164
{

src/Drivers/Pdo/ResultSet.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ public function offsetGet($offset)
321321
* The value to set.
322322
* </p>
323323
* @return void
324+
*
325+
* @codeCoverageIgnore
324326
*/
325327
public function offsetSet($offset, $value)
326328
{
@@ -335,6 +337,8 @@ public function offsetSet($offset, $value)
335337
* The offset to unset.
336338
* </p>
337339
* @return void
340+
*
341+
* @codeCoverageIgnore
338342
*/
339343
public function offsetUnset($offset)
340344
{

src/SphinxQL.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Foolz\SphinxQL;
44
use Foolz\SphinxQL\Drivers\ConnectionInterface;
5-
use Foolz\SphinxQL\Drivers\SphinxQLException;
5+
use Foolz\SphinxQL\Exception\SphinxQLException;
66
use Foolz\SphinxQL\Drivers\MultiResultSetInterface;
77
use Foolz\SphinxQL\Drivers\ResultSetInterface;
88

@@ -1314,11 +1314,7 @@ public function compileEscapeChars($array = array())
13141314
{
13151315
$result = array();
13161316
foreach ($array as $character) {
1317-
if ($character === '\\') {
1318-
$result[$character] = '\\\\';
1319-
} else {
1320-
$result[$character] = '\\'.$character;
1321-
}
1317+
$result[$character] = '\\'.$character;
13221318
}
13231319

13241320
return $result;

tests/SphinxQL/ConnectionTest.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ public function test()
2828
TestUtil::getConnectionDriver();
2929
}
3030

31-
/**
32-
* @covers \Foolz\SphinxQL\Connection::setParam
33-
* @covers \Foolz\SphinxQL\Connection::setParams
34-
*/
3531
public function testGetParams()
3632
{
3733
$this->assertSame(
@@ -47,6 +43,12 @@ public function testGetParams()
4743
$this->connection->getParams()
4844
);
4945

46+
$this->connection->setParam('host', 'localhost');
47+
$this->assertSame(
48+
array('host' => '127.0.0.1', 'port' => 9308, 'socket' => null),
49+
$this->connection->getParams()
50+
);
51+
5052
// create a unix socket connection with host param
5153
$this->connection->setParam('host', 'unix:/var/run/sphinx.sock');
5254
$this->assertSame(
@@ -63,10 +65,6 @@ public function testGetParams()
6365
);
6466
}
6567

66-
/**
67-
* @covers \Foolz\SphinxQL\Connection::setParams
68-
* @covers \Foolz\SphinxQL\Connection::getParams
69-
*/
7068
public function testGetConnectionParams()
7169
{
7270
// verify that (deprecated) getConnectionParams continues to work
@@ -153,6 +151,24 @@ public function testMultiQuery()
153151
), $query->getNext()->fetchAllAssoc());
154152
}
155153

154+
/**
155+
* @expectedException Foolz\SphinxQL\Exception\SphinxQLException
156+
* @expectedExceptionMessage The Queue is empty.
157+
*/
158+
public function testEmptyMultiQuery()
159+
{
160+
$this->connection->connect();
161+
$this->connection->multiQuery(array());
162+
}
163+
164+
/**
165+
* @expectedException Foolz\SphinxQL\Exception\DatabaseException
166+
*/
167+
public function testMultiQueryThrowsException()
168+
{
169+
$this->connection->multiQuery(array('SHOW METAL'));
170+
}
171+
156172
/**
157173
* @expectedException Foolz\SphinxQL\Exception\DatabaseException
158174
*/

tests/SphinxQL/MatchTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ public function testMatch()
3030
$m->match('a')->orMatch('b');
3131
});
3232
$this->assertEquals('(a | b)', $match->compile()->getCompiled());
33+
34+
$sub = new Match(self::$sphinxql);
35+
$sub->match('a')->orMatch('b');
36+
$match = Match::create(self::$sphinxql)
37+
->match($sub);
38+
$this->assertEquals('(a | b)', $match->compile()->getCompiled());
39+
40+
$match = Match::create(self::$sphinxql)
41+
->match('test|case');
42+
$this->assertEquals('test\|case', $match->compile()->getCompiled());
43+
44+
$match = Match::create(self::$sphinxql)
45+
->match(SphinxQL::expr('test|case'));
46+
$this->assertEquals('test|case', $match->compile()->getCompiled());
3347
}
3448

3549
public function testOrMatch()
@@ -275,8 +289,7 @@ public function testCompile()
275289
->orMatch('blue');
276290
$this->assertEquals('(bag of words) << "exact phrase" << red | green | blue', $match->compile()->getCompiled());
277291

278-
$match = new Match(self::$sphinxql);
279-
$match
292+
$match = Match::create(self::$sphinxql)
280293
->match('aaa')
281294
->not(function ($m) {
282295
$m->match('bbb')

tests/SphinxQL/MultiResultSetTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Foolz\SphinxQL\SphinxQL;
44
use Foolz\SphinxQL\Tests\TestUtil;
5+
use Foolz\SphinxQL\Exception\DatabaseException;
56

67
class MultiResultSetTest extends PHPUnit_Framework_TestCase
78
{
@@ -71,6 +72,14 @@ public function testGetNextSet()
7172
$this->assertInstanceOf('\Foolz\Sphinxql\Drivers\ResultSetInterface', $set);
7273
$set = $res->getNext();
7374
$this->assertInstanceOf('\Foolz\Sphinxql\Drivers\ResultSetInterface', $set);
75+
76+
$res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
77+
$res->store();
78+
$set = $res->getNext();
79+
$this->assertInstanceOf('\Foolz\Sphinxql\Drivers\ResultSetInterface', $set);
80+
$set = $res->getNext();
81+
$this->assertInstanceOf('\Foolz\Sphinxql\Drivers\ResultSetInterface', $set);
82+
$this->assertFalse($res->getNext());
7483
}
7584

7685

@@ -97,6 +106,24 @@ public function testStore()
97106
$this->assertEquals(8, $all[0]['count(*)']);
98107
}
99108

109+
/**
110+
* @expectedException Foolz\SphinxQL\Exception\DatabaseException
111+
*/
112+
public function testInvalidStore()
113+
{
114+
$this->refill();
115+
116+
$res = self::$conn->multiQuery(array('SELECT COUNT(*) FROM rt', 'SHOW META'));
117+
$res->getNext();
118+
try {
119+
$res->store();
120+
} catch (DatabaseException $e) {
121+
// we need to clean up
122+
self::setUpBeforeClass();
123+
throw $e;
124+
}
125+
}
126+
100127
public function testArrayAccess()
101128
{
102129
$this->refill();
@@ -138,5 +165,10 @@ public function testIteratorStored()
138165
}
139166

140167
$this->assertCount(2, $array);
168+
169+
$this->assertCount(2, $res);
170+
$this->assertTrue(isset($res[0]));
171+
$this->assertFalse(isset($res[-1]));
172+
$this->assertFalse(isset($res[2]));
141173
}
142174
}

0 commit comments

Comments
 (0)