Completed
Push — master ( 153b23...be6e12 )
by Gabriel
04:14
created

SelectTest::testNested()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Nip\Database\Tests\Query;
4
5
use Mockery as m;
6
use Nip\Database\Connections\Connection;
7
use Nip\Database\Query\Select;
8
use Nip\Database\Tests\AbstractTest;
9
10
/**
11
 * Class SelectTest
12
 * @package Nip\Database\Tests\Query
13
 */
14
class SelectTest extends AbstractTest
15
{
16
    /**
17
     * @var \UnitTester
18
     */
19
    protected $tester;
20
21
    /**
22
     * @var Connection
23
     */
24
    protected $connection;
25
26
    /**
27
     * @var Select
28
     */
29
    protected $selectQuery;
30
31
    public function testSelectSimple()
32
    {
33
        $array = ['id, name as new_name', 'table2.test', 'MAX(pos) as pos'];
34
        call_user_func_array([$this->selectQuery, 'cols'], $array);
35
        $this->selectQuery->from('table x')->where('id = 5');
0 ignored issues
show
Unused Code introduced by
The call to Select::from() has too many arguments starting with 'table x'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
36
37
        static::assertEquals(
38
            'SELECT id, name as new_name, table2.test, MAX(pos) as pos FROM table x WHERE id = 5',
39
            $this->selectQuery->assemble()
40
        );
41
    }
42
43 View Code Duplication
    public function testSimpleSelectDistinct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $this->selectQuery->cols('id, name')->options('distinct')->from('table x')->where('id = 5');
0 ignored issues
show
Unused Code introduced by
The call to Select::cols() has too many arguments starting with 'id, name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Documentation Bug introduced by
The method options does not exist on object<Nip\Database\Query\AbstractQuery>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
46
        static::assertEquals(
47
            "SELECT DISTINCT id, name FROM table x WHERE id = 5",
48
            $this->selectQuery->assemble()
49
        );
50
    }
51
52 View Code Duplication
    public function testWhereAndWhere()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $this->selectQuery->cols('id, name')->from('table x');
0 ignored issues
show
Unused Code introduced by
The call to Select::cols() has too many arguments starting with 'id, name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to AbstractQuery::from() has too many arguments starting with 'table x'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
55
        $this->selectQuery->where('id = 5')->where("active = 'yes'");
56
        static::assertEquals(
57
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes'",
58
            $this->selectQuery->assemble()
59
        );
60
    }
61
62 View Code Duplication
    public function testWhereOrWhere()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        $this->selectQuery->cols('id, name')->from('table x');
0 ignored issues
show
Unused Code introduced by
The call to Select::cols() has too many arguments starting with 'id, name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to AbstractQuery::from() has too many arguments starting with 'table x'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
65
        $this->selectQuery->where('id = 5')->orWhere('id = 7');
66
        static::assertEquals(
67
            "SELECT id, name FROM table x WHERE id = 5 OR id = 7",
68
            $this->selectQuery->assemble()
69
        );
70
    }
71
72
    public function testInitializeCondition()
73
    {
74
        $condition = $this->selectQuery->getCondition("lorem ipsum");
75
        static::assertThat($condition, $this->isInstanceOf("Nip\Database\Query\Condition\Condition"));
76
    }
77
78 View Code Duplication
    public function testNested()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        $this->selectQuery->from("table1");
0 ignored issues
show
Unused Code introduced by
The call to Select::from() has too many arguments starting with 'table1'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
81
82
        $query = $this->connection->newQuery();
83
        $query->from("table2");
0 ignored issues
show
Unused Code introduced by
The call to AbstractQuery::from() has too many arguments starting with 'table2'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
84
        $query->where("id != 5");
85
86
        $this->selectQuery->where("id NOT IN ?", $query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Nip\Database\Query\AbstractQuery>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
88
        static::assertEquals(
89
            "SELECT * FROM `table1` WHERE id NOT IN (SELECT * FROM `table2` WHERE id != 5)",
90
            $this->selectQuery->assemble()
91
        );
92
    }
93
94 View Code Duplication
    public function testUnion()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $this->selectQuery->from("table1");
0 ignored issues
show
Unused Code introduced by
The call to Select::from() has too many arguments starting with 'table1'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
97
98
        $query = $this->connection->newQuery();
99
        $query->from("table2");
0 ignored issues
show
Unused Code introduced by
The call to AbstractQuery::from() has too many arguments starting with 'table2'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
101
        $union = $this->selectQuery->union($query);
102
103
        static::assertEquals("SELECT * FROM `table1` UNION SELECT * FROM `table2`", $union->assemble());
104
    }
105
106
    public function testHasPart()
107
    {
108
        $this->selectQuery->cols('id, name');
0 ignored issues
show
Unused Code introduced by
The call to Select::cols() has too many arguments starting with 'id, name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
109
        self::assertTrue($this->selectQuery->hasPart('cols'));
110
111
        $this->selectQuery->setCols('id, name');
0 ignored issues
show
Unused Code introduced by
The call to Select::setCols() has too many arguments starting with 'id, name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
112
        self::assertTrue($this->selectQuery->hasPart('cols'));
113
114
        $this->selectQuery->limit('');
115
        self::assertFalse($this->selectQuery->hasPart('limit'));
116
117
        $this->selectQuery->limit('6');
118
        self::assertTrue($this->selectQuery->hasPart('limit'));
119
120
        self::assertFalse($this->selectQuery->hasPart('where'));
121
    }
122
123
    public function testLimit()
124
    {
125
        $this->selectQuery->cols('id, name')->from('table x');
0 ignored issues
show
Unused Code introduced by
The call to Select::cols() has too many arguments starting with 'id, name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to AbstractQuery::from() has too many arguments starting with 'table x'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
126
        $this->selectQuery->where('id = 5')->where("active = 'yes'");
127
        $this->selectQuery->limit(5);
128
129
        static::assertEquals(
130
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5",
131
            $this->selectQuery->assemble()
132
        );
133
134
        $this->selectQuery->limit(5, 10);
0 ignored issues
show
Documentation introduced by
10 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
        static::assertEquals(
136
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5,10",
137
            $this->selectQuery->assemble()
138
        );
139
    }
140
141 View Code Duplication
    protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        parent::setUp();
144
        $this->selectQuery = new Select();
145
146
        $adapterMock = m::mock('Nip\Database\Adapters\MySQLi')->shouldDeferMissing();
147
        $adapterMock->shouldReceive('cleanData')->andReturnUsing(function ($data) {
148
            return $data;
149
        });
150
        $this->connection = new Connection(false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a object<PDO>|object<Closure>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
151
        $this->connection->setAdapter($adapterMock);
152
        $this->selectQuery->setManager($this->connection);
153
    }
154
}
155