Skip to content

Commit

Permalink
Rework tests #1, add tests src/QueryBuilder/Conditions. (#376)
Browse files Browse the repository at this point in the history
Rework tests #1, add tests src/QueryBuilder/Conditions.
  • Loading branch information
terabytesoftw committed Nov 2, 2022
1 parent db6191e commit 93aa2d1
Show file tree
Hide file tree
Showing 20 changed files with 614 additions and 33 deletions.
5 changes: 4 additions & 1 deletion phpunit.xml.dist
Expand Up @@ -5,7 +5,7 @@
<directory>./</directory>
</include>
<exclude>
<directory>./src/TestSupport</directory>
<directory>./src/TestSupport</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
Expand All @@ -14,6 +14,9 @@
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="db">
<directory>./tests</directory>
</testsuite>
<testsuite name="ActiveRecord">
<directory>./vendor/yiisoft/active-record/tests</directory>
</testsuite>
Expand Down
56 changes: 53 additions & 3 deletions src/QueryBuilder/Conditions/BetweenColumnsCondition.php
Expand Up @@ -66,15 +66,65 @@ public function getValue(): array|int|string|Iterator|ExpressionInterface

/**
* @throws InvalidArgumentException
*
* @psalm-suppress MixedArgument
*/
public static function fromArrayDefinition(string $operator, array $operands): self
{
if (!isset($operands[0], $operands[1], $operands[2])) {
throw new InvalidArgumentException("Operator '$operator' requires three operands.");
}

return new self($operands[0], $operator, $operands[1], $operands[2]);
return new self(
self::validateValue($operator, $operands[0]),
$operator,
self::validateIntervalStartColumn($operator, $operands[1]),
self::validateIntervalEndColumn($operator, $operands[2]),
);
}

private static function validateValue(
string $operator,
mixed $operand
): array|int|string|Iterator|ExpressionInterface {
if (
!is_array($operand) &&
!is_int($operand) &&
!is_string($operand) &&
!($operand instanceof Iterator) &&
!($operand instanceof ExpressionInterface)
) {
throw new InvalidArgumentException(
"Operator '$operator' requires value to be array, int, string, Iterator or ExpressionInterface."
);
}

return $operand;
}

private static function validateIntervalStartColumn(string $operator, mixed $operand): string|ExpressionInterface
{
if (
!is_string($operand) &&
!($operand instanceof ExpressionInterface)
) {
throw new InvalidArgumentException(
"Operator '$operator' requires interval start column to be string or ExpressionInterface."
);
}

return $operand;
}

private static function validateIntervalEndColumn(string $operator, mixed $operand): string|ExpressionInterface
{
if (
!is_string($operand) &&
!($operand instanceof ExpressionInterface)
) {
throw new InvalidArgumentException(
"Operator '$operator' requires interval end column to be string or ExpressionInterface."
);
}

return $operand;
}
}
15 changes: 12 additions & 3 deletions src/QueryBuilder/Conditions/BetweenCondition.php
Expand Up @@ -43,15 +43,24 @@ public function getOperator(): string

/**
* @throws InvalidArgumentException
*
* @psalm-suppress MixedArgument
*/
public static function fromArrayDefinition(string $operator, array $operands): self
{
if (!isset($operands[0], $operands[1], $operands[2])) {
throw new InvalidArgumentException("Operator '$operator' requires three operands.");
}

return new self($operands[0], $operator, $operands[1], $operands[2]);
return new self(self::validateColumn($operator, $operands[0]), $operator, $operands[1], $operands[2]);
}

private static function validateColumn(string $operator, mixed $operand): string|Expression
{
if (!is_string($operand) && !($operand instanceof Expression)) {
throw new InvalidArgumentException(
"Operator '$operator' requires column to be string or ExpressionInterface."
);
}

return $operand;
}
}
3 changes: 0 additions & 3 deletions src/QueryBuilder/Conditions/ConjunctionCondition.php
Expand Up @@ -20,9 +20,6 @@ public function getExpressions(): array
return $this->expressions;
}

/**
* @psalm-suppress MixedArgument
*/
public static function fromArrayDefinition(string $operator, array $operands): self
{
return new static($operands);
Expand Down
2 changes: 0 additions & 2 deletions src/QueryBuilder/Conditions/ExistsCondition.php
Expand Up @@ -29,8 +29,6 @@ public function getQuery(): QueryInterface

/**
* @throws InvalidArgumentException
*
* @psalm-suppress MixedArgument
*/
public static function fromArrayDefinition(string $operator, array $operands): self
{
Expand Down
3 changes: 0 additions & 3 deletions src/QueryBuilder/Conditions/HashCondition.php
Expand Up @@ -20,9 +20,6 @@ public function getHash(): array|null
return $this->hash;
}

/**
* @psalm-suppress MixedArgument
*/
public static function fromArrayDefinition(string $operator, array $operands): self
{
return new self($operands);
Expand Down
28 changes: 25 additions & 3 deletions src/QueryBuilder/Conditions/InCondition.php
Expand Up @@ -38,15 +38,37 @@ public function getValues(): int|iterable|Iterator|QueryInterface

/**
* @throws InvalidArgumentException
*
* @psalm-suppress MixedArgument
*/
public static function fromArrayDefinition(string $operator, array $operands): self
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidArgumentException("Operator '$operator' requires two operands.");
}

return new self($operands[0], $operator, $operands[1]);
return new self(
self::validateColumn($operator, $operands[0]),
$operator,
self::validateValues($operator, $operands[1]),
);
}

private static function validateColumn(string $operator, mixed $operand): array|string|Iterator
{
if (!is_string($operand) && !is_array($operand) && !$operand instanceof Iterator) {
throw new InvalidArgumentException("Operator '$operator' requires column to be string, array or Iterator.");
}

return $operand;
}

private static function validateValues(string $operator, mixed $operand): int|iterable|Iterator|QueryInterface
{
if (!is_array($operand) && !$operand instanceof Iterator && !is_int($operand) && !$operand instanceof QueryInterface) {
throw new InvalidArgumentException(
"Operator '$operator' requires values to be array, Iterator, int or QueryInterface."
);
}

return $operand;
}
}
39 changes: 35 additions & 4 deletions src/QueryBuilder/Conditions/LikeCondition.php
Expand Up @@ -51,21 +51,52 @@ public function setEscapingReplacements(array|null $escapingReplacements): void

/**
* @throws InvalidArgumentException
*
* @psalm-suppress MixedArgument
*/
public static function fromArrayDefinition(string $operator, array $operands): self
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidArgumentException("Operator '$operator' requires two operands.");
}

$condition = new self($operands[0], $operator, $operands[1]);
$condition = new self(
self::validateColumn($operator, $operands[0]),
$operator,
self::validateValue($operator, $operands[1]),
);

if (array_key_exists(2, $operands)) {
if (array_key_exists(2, $operands) && (is_array($operands[2]) || $operands[2] === null)) {
$condition->setEscapingReplacements($operands[2]);
}

return $condition;
}

private static function validateColumn(string $operator, mixed $operand): string|Expression
{
if (!is_string($operand) && !$operand instanceof Expression) {
throw new InvalidArgumentException("Operator '$operator' requires column to be string or Expression.");
}

return $operand;
}

private static function validateValue(
string $operator,
mixed $operand
): array|int|string|Iterator|ExpressionInterface|null {
if (
!is_string($operand) &&
!is_array($operand) &&
!is_int($operand) &&
!$operand instanceof Iterator &&
!$operand instanceof ExpressionInterface &&
$operand !== null
) {
throw new InvalidArgumentException(
"Operator '$operator' requires value to be string, array, Iterator or ExpressionInterface."
);
}

return $operand;
}
}
23 changes: 20 additions & 3 deletions src/QueryBuilder/Conditions/NotCondition.php
Expand Up @@ -27,15 +27,32 @@ public function getCondition(): ExpressionInterface|array|null|string

/**
* @throws InvalidArgumentException
*
* @psalm-suppress MixedArgument
*/
public static function fromArrayDefinition(string $operator, array $operands): self
{
return new self(self::validateCondition($operator, $operands));
}

private static function validateCondition(string $operator, array $operands): ExpressionInterface|array|null|string
{
if (count($operands) !== 1) {
throw new InvalidArgumentException("Operator '$operator' requires exactly one operand.");
}

return new self(array_shift($operands));
/** @var mixed $operands */
$operands = array_shift($operands);

if (
!is_array($operands) &&
!($operands instanceof ExpressionInterface) &&
!is_string($operands) &&
$operands !== null
) {
throw new InvalidArgumentException(
"Operator '$operator' requires condition to be array, string, null or ExpressionInterface."
);
}

return $operands;
}
}
2 changes: 0 additions & 2 deletions src/QueryBuilder/Conditions/OrCondition.php
Expand Up @@ -11,8 +11,6 @@ final class OrCondition extends ConjunctionCondition
{
/**
* Returns the operator that is represented by this condition class, e.g. `AND`, `OR`.
*
* @psalm-return 'OR'
*/
public function getOperator(): string
{
Expand Down
23 changes: 17 additions & 6 deletions src/QueryBuilder/Conditions/SimpleCondition.php
Expand Up @@ -9,8 +9,6 @@
use Yiisoft\Db\QueryBuilder\Conditions\Interface\SimpleConditionInterface;
use Yiisoft\Db\Query\QueryInterface;

use function count;

/**
* Class SimpleCondition represents a simple condition like `"column" operator value`.
*/
Expand Down Expand Up @@ -40,15 +38,28 @@ public function getValue(): mixed

/**
* @throws InvalidArgumentException
*
* @psalm-suppress MixedArgument
*/
public static function fromArrayDefinition(string $operator, array $operands): self
{
if (count($operands) !== 2) {
if (!isset($operands[0], $operands[1])) {
throw new InvalidArgumentException("Operator '$operator' requires two operands.");
}

return new self($operands[0], $operator, $operands[1]);
return new self(self::validateColumn($operator, $operands[0]), $operator, $operands[1]);
}

private static function validateColumn(string $operator, mixed $operands): string|Expression|QueryInterface
{
if (
!is_string($operands) &&
!($operands instanceof Expression) &&
!($operands instanceof QueryInterface)
) {
throw new InvalidArgumentException(
"Operator '$operator' requires column to be string, ExpressionInterface or QueryInterface."
);
}

return $operands;
}
}
28 changes: 28 additions & 0 deletions tests/QueryBuilder/Condition/AndConditionsTest.php
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Tests\QueryBuilder\Conditions;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\QueryBuilder\Conditions\AndCondition;

/**
* @group db
*/
final class AndConditionsTest extends TestCase
{
public function testConstructor(): void
{
$andCondition = new AndCondition(['a' => 1, 'b' => 2]);

$this->assertSame(['a' => 1, 'b' => 2], $andCondition->getExpressions());
}

public function testGetOperator(): void
{
$andCondition = new AndCondition(['a' => 1, 'b' => 2]);

$this->assertSame('AND', $andCondition->getOperator());
}
}

0 comments on commit 93aa2d1

Please sign in to comment.