Passed
Push — master ( 763c33...debe09 )
by Wilmer
29:07 queued 26:37
created

LikeConditionTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 27
c 1
b 0
f 1
dl 0
loc 59
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testFromArrayDefinitionExceptionColumn() 0 5 1
A testFromArrayDefinitionException() 0 5 1
A testFromArrayDefinitionExceptionValue() 0 7 1
A testFromArrayDefinition() 0 7 1
A testFromArrayDefinitionSetEscapingReplacements() 0 8 1
A testConstructor() 0 7 1
A testSetEscapingReplacements() 0 6 1
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