|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\QueryBuilder\Condition; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
|
9
|
|
|
use Yiisoft\Db\QueryBuilder\Conditions\SimpleCondition; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @group db |
|
13
|
|
|
*/ |
|
14
|
|
|
final class SimpleConditionTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
public function testConstructor(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$simpleCondition = new SimpleCondition('id', '=', 1); |
|
19
|
|
|
|
|
20
|
|
|
$this->assertSame('id', $simpleCondition->getColumn()); |
|
21
|
|
|
$this->assertSame('=', $simpleCondition->getOperator()); |
|
22
|
|
|
$this->assertSame(1, $simpleCondition->getValue()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testFromArrayDefinition(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$simpleCondition = SimpleCondition::fromArrayDefinition('=', ['id', 1]); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertSame('id', $simpleCondition->getColumn()); |
|
30
|
|
|
$this->assertSame('=', $simpleCondition->getOperator()); |
|
31
|
|
|
$this->assertSame(1, $simpleCondition->getValue()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testFromArrayDefinitionColumnException(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
37
|
|
|
$this->expectExceptionMessage("Operator '=' requires two operands."); |
|
38
|
|
|
SimpleCondition::fromArrayDefinition('=', []); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testFromArrayDefinitionValueException(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
44
|
|
|
$this->expectExceptionMessage("Operator 'IN' requires two operands."); |
|
45
|
|
|
SimpleCondition::fromArrayDefinition('IN', ['column']); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testFromArrayDefinitionExceptionColumn(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
51
|
|
|
$this->expectExceptionMessage( |
|
52
|
|
|
"Operator '=' requires column to be string, ExpressionInterface or QueryInterface." |
|
53
|
|
|
); |
|
54
|
|
|
SimpleCondition::fromArrayDefinition('=', [1, 1]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testNullSecondOperand(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$condition = SimpleCondition::fromArrayDefinition('=', ['id', null]); |
|
60
|
|
|
$this->assertNull($condition->getValue()); |
|
61
|
|
|
|
|
62
|
|
|
$condition2 = new SimpleCondition('name', 'IS NOT', null); |
|
63
|
|
|
$this->assertSame('IS NOT', $condition2->getOperator()); |
|
64
|
|
|
$this->assertNull($condition2->getValue()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|