Skip to content

Commit

Permalink
Add tests to ensure that injections fail #56
Browse files Browse the repository at this point in the history
  • Loading branch information
timgws committed Jul 5, 2022
1 parent 7147214 commit 269ea47
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
33 changes: 13 additions & 20 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
verbose="true">
<testsuites>
<testsuite name="MySQL">
<directory suffix="Test.php">tests</directory>
</testsuite>

<testsuite name="MongoDB">
<directory suffix="MongoDB.php">tests/mongodb</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" verbose="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</include>
</coverage>
<testsuites>
<testsuite name="MySQL">
<directory suffix="Test.php">tests</directory>
</testsuite>
<testsuite name="MongoDB">
<directory suffix="MongoDB.php">tests/mongodb</directory>
</testsuite>
</testsuites>
</phpunit>

1 change: 1 addition & 0 deletions tests/CommonQueryBuilderTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class CommonQueryBuilderTests extends TestCase
{
protected $simpleQuery = '{"condition":"AND","rules":[{"id":"price","field":"price","type":"double","operator":"less","value":"10.25"}]}';
protected $simpleQueryInjection = '{"condition":"ALSO","rules":[{"id":"price","field":"price","type":"double","operator":"less","value":"10.25"},{"id":"price","field":"price","type":"double","operator":"greater","value":"9.25"}]}';
protected $json1 = '{
"condition":"AND",
"rules":[
Expand Down
13 changes: 13 additions & 0 deletions tests/QueryBuilderParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ public function testSimpleQuery()
$this->assertEquals('select * where `price` < ?', $builder->toSql());
}

public function testSimpleQueryNoInjection()
{
$builder = $this->createQueryBuilder();
$qb = $this->getParserUnderTest();

$this->expectException('timgws\QBParseException');
$this->expectExceptionMessage("Condition can only be one of");

$test = $qb->parse($this->simpleQueryInjection, $builder);

$this->assertEquals('select * where `price` < ?', $builder->toSql());
}

public function testMoreComplexQuery()
{
$builder = $this->createQueryBuilder();
Expand Down

0 comments on commit 269ea47

Please sign in to comment.