Commit d3c428ba authored by Виктор Волков's avatar Виктор Волков
Browse files

Merge branch 'develop' into 'master'

test: add test of count, limit and offset

See merge request !9
parents 1d32ca89 55bab0d9
......@@ -7,6 +7,7 @@
"ext-json": "*"
},
"require-dev": {
"doctrine/coding-standard": "^6.0",
"squizlabs/php_codesniffer": "3.*",
"phpunit/phpunit": "^8",
"ext-tokenizer" : "*",
......
<?xml version="1.0"?>
<ruleset name="PSR12">
<description>The ASH2 coding standard.</description>
<rule ref="PSR12"/>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint"/>
<file>src/</file>
<exclude-pattern>vendor</exclude-pattern>
</ruleset>
\ No newline at end of file
......@@ -61,7 +61,7 @@ class ApiQL extends AbstractLanguage
/**
* Execute specification root expression
*/
public function execute()
public function execute(): void
{
$this->executeFields();
$this->executeOffset();
......@@ -73,7 +73,7 @@ class ApiQL extends AbstractLanguage
/**
* Execute fields part of expression
*/
private function executeFields()
private function executeFields(): void
{
$isProjection = false;
$isDistinct = false;
......@@ -119,7 +119,7 @@ class ApiQL extends AbstractLanguage
/**
* Execute offset part of expression
*/
private function executeOffset()
private function executeOffset(): void
{
if (array_key_exists('offset', $this->expression)) {
self::$builder->setFirstResult($this->expression['offset']);
......@@ -130,7 +130,7 @@ class ApiQL extends AbstractLanguage
/**
* Execute limit part of expression
*/
private function executeLimit()
private function executeLimit(): void
{
if (array_key_exists('limit', $this->expression)) {
self::$builder->setMaxResults($this->expression['limit']);
......@@ -141,7 +141,7 @@ class ApiQL extends AbstractLanguage
/**
* Execute fields part of expression
*/
private function executeOrder()
private function executeOrder(): void
{
if (array_key_exists('order', $this->expression)) {
$fields = !is_array($this->expression['order']) ? explode(',', $this->expression['order']) :
......@@ -161,7 +161,7 @@ class ApiQL extends AbstractLanguage
/**
* Execute where part of expression
*/
private function executeWhere()
private function executeWhere(): void
{
if (!empty($this->expression)) {
$expression = $this->expression;
......@@ -186,7 +186,7 @@ class ApiQL extends AbstractLanguage
/**
* Execute explicit specification
*/
private function executeSpec(array $expression)
private function executeSpec(array $expression): void
{
$specName = $expression[self::SPEC];
unset($expression[self::SPEC]);
......@@ -198,7 +198,7 @@ class ApiQL extends AbstractLanguage
/**
* Execute where part of expression when operator is not set explicitly with rel or logic key
*/
private function executeKeyWhere(array $expression)
private function executeKeyWhere(array $expression): void
{
$logic = self::getLogicOperator($expression);
$rel = self::getRelationOperator($expression);
......@@ -223,14 +223,14 @@ class ApiQL extends AbstractLanguage
} else {
$expression = [$rel => $this->expression];
$interpreter = LanguageFactory::build($expression);
return self::$builder->andWhere($interpreter->execute());
self::$builder->andWhere($interpreter->execute());
}
}
/**
* Execute where part of expression when operator is not set explicitly with rel or logic key
*/
private function executeNoKeyWhere(array $expression)
private function executeNoKeyWhere(array $expression): void
{
$operator = array_key_first($expression);
$data_ = $expression[$operator];
......@@ -238,14 +238,14 @@ class ApiQL extends AbstractLanguage
$interpreters = array_map(function ($item) {
return LanguageFactory::build($item);
}, $data_);
return self::$builder->andWhere($this->method($operator, ...$interpreters));
self::$builder->andWhere($this->method($operator, ...$interpreters));
} elseif (self::isRelationOperator($operator) || self::isSpecification($operator)) {
$interpreter = LanguageFactory::build($expression);
return self::$builder->andWhere($interpreter->execute());
self::$builder->andWhere($interpreter->execute());
} elseif (count($this->expression) == 1) {
$expression = ["eq" => $this->expression];
$interpreter = LanguageFactory::build($expression);
return self::$builder->andWhere($interpreter->execute());
self::$builder->andWhere($interpreter->execute());
} else {
$logic = "and";
$rel = "eq";
......
......@@ -46,7 +46,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
$stmt->execute();
}
public function testEquals()
public function testEquals(): void
{
//arrange
$payload = ["where" => ["eq" => ["a" => 1]]];
......@@ -77,7 +77,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testNotEquals()
public function testNotEquals(): void
{
//arrange
$payload = ["where" => ["neq" => ["c" => 7]]];
......@@ -94,7 +94,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testLessThan()
public function testLessThan(): void
{
//arrange
$payload = ["where" => ["lt" => ["b" => 7]]];
......@@ -111,7 +111,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testLessThanOrEqual()
public function testLessThanOrEqual(): void
{
//arrange
$payload = ["where" => ["lte" => ["b" => 7]]];
......@@ -128,7 +128,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testGreaterThan()
public function testGreaterThan(): void
{
//arrange
$payload = ["where" => ["gt" => ["c" => 3]]];
......@@ -145,7 +145,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testGreaterThanOrEqual()
public function testGreaterThanOrEqual(): void
{
//arrange
$payload = ["where" => ["gte" => ["c" => 7]]];
......@@ -162,7 +162,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testLike()
public function testLike(): void
{
//arrange
$payload = ["where" => ["like" => ["d" => "one"]]];
......@@ -193,7 +193,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testContainsString()
public function testContainsString(): void
{
//arrange
$payload = ["spec" => "ContainsString", "data" => ["d" => "one"]];
......@@ -235,7 +235,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testStartsWith()
public function testStartsWith(): void
{
//arrange
$payload = ["spec" => "StartsWith", "data" => ["d" => "one"]];
......@@ -277,7 +277,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testNotLike()
public function testNotLike(): void
{
//arrange
$payload = ["where" => ["not_like" => ["d" => "one"]]];
......@@ -308,7 +308,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testIsNull()
public function testIsNull(): void
{
//arrange
$payload = ["where" => ["is_null" => "e"]];
......@@ -354,7 +354,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testIsNotNull()
public function testIsNotNull(): void
{
//arrange
$payload = ["where" => ["is_not_null" => "e"]];
......@@ -371,7 +371,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testIn()
public function testIn(): void
{
//arrange
$payload = ["where" => ["in" => ["d" => ["one", "none"]]]];
......@@ -402,7 +402,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testNotIn()
public function testNotIn(): void
{
//arrange
$payload = ["where" => ["not_in" => ["b" => [7, 2, 5]]]];
......@@ -419,7 +419,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testSimpleAnd()
public function testSimpleAnd(): void
{
//arrange
$payload = ["where" => ["and" => [["eq" => ["a" => 1]], ["eq" => ["b" => 2]]]]];
......@@ -436,7 +436,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testSimpleOr()
public function testSimpleOr(): void
{
//arrange
$payload = ["where" => ["or" => [["eq" => ["a" => 1]], ["eq" => ["b" => 8]]]]];
......@@ -453,7 +453,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testAndOr()
public function testAndOr(): void
{
//arrange WHERE a = 1 AND (b = 6 OR c = 10)
$payload = ["where" => ["and" => [["eq" => ["a" => 1]],["or" => [["eq" => ["b" => 6]],["eq" =>
......@@ -471,7 +471,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testSpecOr()
public function testSpecOr(): void
{
//arrange WHERE Equals(a = 1) AND (b = 6 OR c = 10)
$payload = ["where" => ["and" => [["spec" => "Equals", "data" => ["a" => 1]],
......@@ -489,7 +489,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testMultiOr()
public function testMultiOr(): void
{
//arrange
$payload = ["where" => ["or" => [["eq" => ["a" => 1]], ["eq" => ["b" => 8]], ["eq" => ["c" => 7]]]]];
......@@ -520,7 +520,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testMultiAnd()
public function testMultiAnd(): void
{
//arrange
$payload = ["where" => ["and" => [["eq" => ["a" => 1]], ["eq" => ["b" => 6]], ["eq" => ["c" => 6]]]]];
......@@ -552,7 +552,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testMultiAndWithSpec()
public function testMultiAndWithSpec(): void
{
//arrange
$payload = ["where" => ["and" => [["spec" => "Equals", "data" => ["a" => 1]], ["eq" => ["b" => 6]],
......@@ -570,7 +570,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testMultiAndWithSpecPlusNestedOr()
public function testMultiAndWithSpecPlusNestedOr(): void
{
//arrange
$payload = ["where" => ["and" => [["spec" => "Equals", "data" => ["a" => 1]], ["eq" => ["b" => 6]],
......@@ -649,7 +649,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testRepoWithSpec()
public function testRepoWithSpec(): void
{
//arrange
$payload = ["where" => ["spec" => "Equals", "data" => ["a" => 1]]];
......@@ -694,7 +694,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testEmptyResultset()
public function testEmptyResultset(): void
{
//arrange
$payload = ["where" => ["eq" => ["b" => 1000]]];
......@@ -711,7 +711,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testShortForm()
public function testShortForm(): void
{
//arrange
$payload = ["a" => 1, "rel" => "eq"];
......@@ -770,7 +770,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testSimpleGetQuery()
public function testSimpleGetQuery(): void
{
//arrange
$payload = ["a" => 1];
......@@ -844,7 +844,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testRepoWithSimpleGet()
public function testRepoWithSimpleGet(): void
{
//arrange ?a=1&b=8
$payload = ["a" => 1, "b" => 6];
......@@ -858,7 +858,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testSpecs()
public function testSpecs(): void
{
//arrange
$payload = ["where" => ["spec" => "NotEquals", "data" => ["a" => 1]]];
......@@ -1075,7 +1075,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testDynamicFields()
public function testDynamicFields(): void
{
//arrange
$payload = ["spec" => "not_in", "b" => [2, 5, 7], "fields" => ["a", "c"]];
......@@ -1142,7 +1142,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testLimitOffset()
public function testLimitOffset(): void
{
//arrange
$payload = ["spec" => "in", "b" => [2, 5, 7], "fields" => ["b"], "limit" => 1];
......@@ -1167,7 +1167,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testPartsOnly()
public function testPartsOnly(): void
{
//arrange ?spec=in&b=2,5,7&fields=b&order=b
$payload = ["fields" => "a,b"];
......@@ -1272,7 +1272,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testOrderBy()
public function testOrderBy(): void
{
//arrange ?spec=in&b=2,5,7&fields=b&order=b
$payload = ["spec" => "in", "b" => "2,5,7", "fields" => "b", "order" => "b"];
......@@ -1340,7 +1340,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testArrayFields()
public function testArrayFields(): void
{
//arrange
......@@ -1472,7 +1472,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testJsonFieldSpecs()
public function testJsonFieldSpecs(): void
{
//arrange
$payload = ["spec" => "Equals", "f" => ['is_fixed' => 'true']];
......@@ -1551,7 +1551,7 @@ final class ApiQLPostgresPlatformTest extends TestCase
);
}
public function testDistinct()
public function testDistinct(): void
{
//arrange ?a=1&distinct=false
$payload = ["a" => 1, "distinct" => "false"];
......@@ -1597,4 +1597,18 @@ final class ApiQLPostgresPlatformTest extends TestCase
$data
);
}
public function testDefaultCountWithOffsetLimit(): void
{
//arrange ?fields=a&distinct=false
$payload = ["limit" => 30, "offset" => 0, "a" => 1];
$repo = new ApiRepository(self::$cnx);
//act
$data = $repo->count($payload);
//assert
$this->assertEquals(
2,
$data[0]["count"]
);
}
}
......@@ -16,10 +16,20 @@ class ApiRepository
$this->connection = $connection;
}
public function get(array $payload = [])
public function get(array $payload = []): array
{
$qb = $this->connection->createQueryBuilder()
->from("test_table");
->from("test_table");
$api = new ApiQL($payload, $qb);
$api->execute();
return $qb->execute()->fetchAll(\PDO::FETCH_ASSOC);
}
public function count(array $payload = []): array
{
$qb = $this->connection->createQueryBuilder()
->select("count(*)")
->from("test_table");
$api = new ApiQL($payload, $qb);
$api->execute();
return $qb->execute()->fetchAll(\PDO::FETCH_ASSOC);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment