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