Skip to content

Commit 597f826

Browse files
authored
Merge pull request #80 from os-cillation/master
Fix String IDs enclosed in Quotes
2 parents 995c9d8 + 87ab21d commit 597f826

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ test:
1919
php -S 127.0.0.1:3000 -t Tests/app 2>1 & ./vendor/phpunit/phpunit/phpunit -c phpunit.xml --coverage-html $(LOGDIR)/coverage
2020
ps -eaf | awk '/ph[p] -S/{ print $$2 }' | xargs kill
2121
rm 1
22+
unit:
23+
php -S 127.0.0.1:3000 -t Tests/app 2>1 & ./vendor/phpunit/phpunit/phpunit -c phpunit.xml --coverage-html $(LOGDIR)/coverage
24+
ps -eaf | awk '/ph[p] -S/{ print $$2 }' | xargs kill
25+
rm 1
26+
clean:
27+
ps -eaf | awk '/ph[p] -S/{ print $$2 }' | xargs kill
28+
rm 1
2229
docs:
2330

24-
install:
31+
install:

Tests/Factory/ResponseExceptionFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* @author Rob Treacy <[email protected]>
3030
*/
31-
class ResponseExceptionFactoryTest extends \PHPUnit_Framework_TestCase {
31+
class ResponseExceptionFactoryTest extends \PHPUnit\Framework\TestCase {
3232
private $responseExceptionFactory;
3333

3434
public function setUp() {
@@ -42,8 +42,8 @@ public function setUp() {
4242
* @dataProvider createDbalExceptionProvider
4343
*/
4444
public function createDbalException(Response $response, $expectedExceptionClass) {
45-
$return = $this->responseExceptionFactory->createDbalException($response, $this->createMock(DriverExceptionInterface::class));
46-
$this->assertInstanceOf($expectedExceptionClass, $return);
45+
$return = $this->responseExceptionFactory->createDbalException($response, $this->createMock($expectedExceptionClass));
46+
$this->assertInstanceOf(DriverExceptionInterface::class, $return);
4747
}
4848

4949
/**

Tests/Types/IdentifierTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,48 @@ public function create() {
4545
$this->assertSame('1', Identifier::create($tokens));
4646
}
4747

48+
/**
49+
* @test
50+
* @group unit
51+
* @covers ::create
52+
*
53+
* @SuppressWarnings("PHPMD.StaticAccess")
54+
*/
55+
public function createWithStringId() {
56+
$parser = new PHPSQLParser();
57+
$tokens = $parser->parse('SELECT name FROM products WHERE id="test"');
58+
59+
$this->assertSame('test', Identifier::create($tokens));
60+
}
61+
62+
/**
63+
* @test
64+
* @group unit
65+
* @covers ::create
66+
*
67+
* @SuppressWarnings("PHPMD.StaticAccess")
68+
*/
69+
public function createWithStringIdSingleQuotes() {
70+
$parser = new PHPSQLParser();
71+
$tokens = $parser->parse('SELECT name FROM products WHERE id=\'test\'');
72+
73+
$this->assertSame('test', Identifier::create($tokens));
74+
}
75+
76+
/**
77+
* @test
78+
* @group unit
79+
* @covers ::create
80+
*
81+
* @SuppressWarnings("PHPMD.StaticAccess")
82+
*/
83+
public function createWithStringIdNoQuotes() {
84+
$parser = new PHPSQLParser();
85+
$tokens = $parser->parse('SELECT name FROM products WHERE id=test');
86+
87+
$this->assertSame('test', Identifier::create($tokens));
88+
}
89+
4890
/**
4991
* @test
5092
* @group unit

Types/HttpQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function createConditionals(array $tokens) {
7070

7171
// Get WHERE conditions as string including table alias and primary key column if present
7272
$sqlWhereString = array_reduce($tokens['WHERE'], function($query, $token) use ($tableAlias) {
73-
return $query . str_replace('"', '', str_replace('OR', '|', str_replace('AND', '&', $token['base_expr'])));
73+
return $query . str_replace(['"', '\''], '', str_replace('OR', '|', str_replace('AND', '&', $token['base_expr'])));
7474
});
7575

7676
// Remove primary key column before removing table alias and returning

Types/Identifier.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
namespace Circle\DoctrineRestDriver\Types;
2020

2121
use Circle\DoctrineRestDriver\MetaData;
22-
use Circle\DoctrineRestDriver\Validation\Assertions;
2322

2423
/**
2524
* Extracts id information from a sql token array
@@ -45,10 +44,10 @@ public static function create(array $tokens) {
4544
$idAlias = self::alias($tokens);
4645

4746
return array_reduce($tokens['WHERE'], function($carry, $token) use ($tokens, $idAlias) {
48-
if (!is_int($carry)) return $carry;
47+
if ($carry !== null) return (string)Value::create($carry);
4948
if ($token['expr_type'] === 'colref' && $token['base_expr'] === $idAlias) return $tokens['WHERE'][$carry+2]['base_expr'];
5049
if (!isset($tokens[$carry+1])) return '';
51-
}, 0);
50+
}, null);
5251
}
5352

5453
/**
@@ -85,4 +84,4 @@ public static function column(array $tokens, MetaData $metaData) {
8584

8685
return !empty($idColumns) ? end($idColumns) : 'id';
8786
}
88-
}
87+
}

Types/Value.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Value {
4040
*/
4141
public static function create($value) {
4242
Str::assert($value, 'value');
43-
43+
4444
if($value === 'true') return true;
4545
if($value === 'false') return false;
4646
if($value === 'null') return null;

0 commit comments

Comments
 (0)