ConditionTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 41
c 2
b 0
f 0
dl 0
loc 82
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testAndConditions() 0 6 1
A testParseString() 0 13 1
A testAndOrConditions() 0 13 1
A testWhereIn() 0 4 1
A setUp() 0 12 1
A testOrConditions() 0 4 1
A testNestedConditions() 0 10 1
1
<?php
2
3
namespace Nip\Database\Tests\Query\Condition;
4
5
use Mockery as m;
6
use Nip\Database\Connections\Connection;
7
use Nip\Database\Query\Select as SelectQuery;
8
use Nip\Database\Tests\AbstractTest;
9
10
/**
11
 * Class ConditionTest
12
 * @package Nip\Database\Tests\Query\Condition
13
 */
14
class ConditionTest extends AbstractTest
15
{
16
    /**
17
     * @var SelectQuery
18
     */
19
    protected $query;
20
21
    public function setUp(): void
22
    {
23
        parent::setUp();
24
25
        $adapterMock = m::mock('Nip\Database\Adapters\MySQLi')->shouldDeferMissing();
0 ignored issues
show
Deprecated Code introduced by
The function Mockery\LegacyMockInterface::shouldDeferMissing() has been deprecated: 2.0.0 Please use makePartial() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

25
        $adapterMock = /** @scrutinizer ignore-deprecated */ m::mock('Nip\Database\Adapters\MySQLi')->shouldDeferMissing();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
26
        $adapterMock->shouldReceive('cleanData')->andReturnUsing(function ($data) {
27
            return $data;
28
        });
29
30
        $connection = new Connection(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type Closure|PDO expected by parameter $pdo of Nip\Database\Connections\Connection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        $connection = new Connection(/** @scrutinizer ignore-type */ false);
Loading history...
31
        $connection->setAdapter($adapterMock);
32
        $this->query = $connection->newQuery();
33
    }
34
35
    public function testParseString()
36
    {
37
        $condition = $this->query->getCondition('name = value');
38
        static::assertEquals('name = value', $condition->getString());
39
40
        $condition = $this->query->getCondition('id = ?', 5);
0 ignored issues
show
Bug introduced by
5 of type integer is incompatible with the type array expected by parameter $values of Nip\Database\Query\AbstractQuery::getCondition(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $condition = $this->query->getCondition('id = ?', /** @scrutinizer ignore-type */ 5);
Loading history...
41
        static::assertEquals('id = 5', $condition->getString());
42
43
        $condition = $this->query->getCondition('MATCH title AGAINST (?)', 'lorem ipsum');
0 ignored issues
show
Bug introduced by
'lorem ipsum' of type string is incompatible with the type array expected by parameter $values of Nip\Database\Query\AbstractQuery::getCondition(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        $condition = $this->query->getCondition('MATCH title AGAINST (?)', /** @scrutinizer ignore-type */ 'lorem ipsum');
Loading history...
44
        static::assertEquals("MATCH title AGAINST ('lorem ipsum')", $condition->getString());
45
46
        $condition = $this->query->getCondition('pos BETWEEN ? AND ?', [1, 10]);
47
        static::assertEquals('pos BETWEEN 1 AND 10', $condition->getString());
48
    }
49
50
    public function testAndConditions()
51
    {
52
        $condition = $this->query->getCondition("name LIKE '%lorem%'");
53
        $condition = $condition->and_($this->query->getCondition("date > NOW()"));
54
55
        static::assertEquals("name LIKE '%lorem%' AND date > NOW()", $condition->getString());
56
    }
57
58
    public function testOrConditions()
59
    {
60
        $condition = $this->query->getCondition("name LIKE '%lorem%'")->or_($this->query->getCondition('date > NOW()'));
61
        static::assertEquals("name LIKE '%lorem%' OR date > NOW()", $condition->getString());
62
    }
63
64
    public function testAndOrConditions()
65
    {
66
        $condition1 = $this->query
67
            ->getCondition("name LIKE '%lorem%'")
68
            ->and_($this->query->getCondition("date > NOW()"));
69
        $condition2 = $this->query
70
            ->getCondition("name LIKE '%ipsum%'")
71
            ->and_($this->query->getCondition("date < NOW()"));
72
        $condition3 = $condition1->or_($condition2);
73
74
        static::assertEquals(
75
            "(name LIKE '%lorem%' AND date > NOW()) OR (name LIKE '%ipsum%' AND date < NOW())",
76
            $condition3->getString()
77
        );
78
    }
79
80
    public function testNestedConditions()
81
    {
82
        $condition2 = $this->query
83
            ->getCondition("date > NOW()")->
84
            or_($this->query->getCondition("date < '24.10.2008'"));
85
        $condition = $this->query
86
            ->getCondition("name LIKE '%lorem%'")
87
            ->and_($condition2);
88
89
        static::assertEquals("name LIKE '%lorem%' AND (date > NOW() OR date < '24.10.2008')", $condition->getString());
90
    }
91
92
    public function testWhereIn()
93
    {
94
        $condition = $this->query->getCondition("id in ?", [1, 2, 3]);
95
        static::assertEquals("id in (1, 2, 3)", $condition->getString());
96
    }
97
}
98