|
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\Condition\LikeCondition; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @group db |
|
13
|
|
|
* |
|
14
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
|
15
|
|
|
*/ |
|
16
|
|
|
final class LikeConditionTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function testConstructor(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$likeCondition = new LikeCondition('id', 'LIKE', 'test'); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertSame('id', $likeCondition->getColumn()); |
|
23
|
|
|
$this->assertSame('LIKE', $likeCondition->getOperator()); |
|
24
|
|
|
$this->assertSame('test', $likeCondition->getValue()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testFromArrayDefinition(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$likeCondition = LikeCondition::fromArrayDefinition('LIKE', ['id', 'test']); |
|
30
|
|
|
|
|
31
|
|
|
$this->assertSame('id', $likeCondition->getColumn()); |
|
32
|
|
|
$this->assertSame('LIKE', $likeCondition->getOperator()); |
|
33
|
|
|
$this->assertSame('test', $likeCondition->getValue()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testFromArrayDefinitionException(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
39
|
|
|
$this->expectExceptionMessage("Operator 'LIKE' requires two operands."); |
|
40
|
|
|
|
|
41
|
|
|
LikeCondition::fromArrayDefinition('LIKE', []); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testFromArrayDefinitionExceptionColumn(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
47
|
|
|
$this->expectExceptionMessage("Operator 'LIKE' requires column to be string or Expression."); |
|
48
|
|
|
|
|
49
|
|
|
LikeCondition::fromArrayDefinition('LIKE', [false, 'test']); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testFromArrayDefinitionExceptionValue(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
55
|
|
|
$this->expectExceptionMessage( |
|
56
|
|
|
"Operator 'LIKE' requires value to be string, array, Iterator or ExpressionInterface." |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
LikeCondition::fromArrayDefinition('LIKE', ['id', false]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testFromArrayDefinitionSetEscapingReplacements(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$likeCondition = LikeCondition::fromArrayDefinition('LIKE', ['id', 'test', ['%' => '\%', '_' => '\_']]); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertSame('id', $likeCondition->getColumn()); |
|
67
|
|
|
$this->assertSame('LIKE', $likeCondition->getOperator()); |
|
68
|
|
|
$this->assertSame('test', $likeCondition->getValue()); |
|
69
|
|
|
$this->assertSame(['%' => '\%', '_' => '\_'], $likeCondition->getEscapingReplacements()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testSetEscapingReplacements(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$likeCondition = new LikeCondition('id', 'LIKE', 'test'); |
|
75
|
|
|
$likeCondition->setEscapingReplacements(['%' => '\%', '_' => '\_']); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertSame(['%' => '\%', '_' => '\_'], $likeCondition->getEscapingReplacements()); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|