Completed
Push — master ( b19bf6...648477 )
by Gabriel
04:47
created

SelectTest::testJoinTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/** @noinspection SqlResolve */
4
5
/** @noinspection SqlNoDataSourceInspection */
6
7
namespace Nip\Database\Tests\Query;
8
9
use Mockery as m;
10
use Nip\Database\Connections\Connection;
11
use Nip\Database\Query\Select;
12
use Nip\Database\Tests\AbstractTest;
13
14
/**
15
 * Class SelectTest
16
 * @package Nip\Database\Tests\Query
17
 */
18
class SelectTest extends AbstractTest
19
{
20
    /**
21
     * @var \UnitTester
22
     */
23
    protected $tester;
24
25
    /**
26
     * @var Connection
27
     */
28
    protected $connection;
29
30
    /**
31
     * @var Select
32
     */
33
    protected $selectQuery;
34
35
    public function testSelectSimple()
36
    {
37
        $array = ['id, name as new_name', 'table2.test', 'MAX(pos) as pos'];
38
        call_user_func_array([$this->selectQuery, 'cols'], $array);
39
        $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...
40
41
        static::assertEquals(
42
            'SELECT id, name as new_name, table2.test, MAX(pos) as pos FROM table x WHERE id = 5',
43
            $this->selectQuery->assemble()
44
        );
45
    }
46
47 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...
48
    {
49
        $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...
50
        static::assertEquals(
51
            "SELECT DISTINCT id, name FROM table x WHERE id = 5",
52
            $this->selectQuery->assemble()
53
        );
54
    }
55
56 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...
57
    {
58
        $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...
59
        $this->selectQuery->where('id = 5')->where("active = 'yes'");
60
        static::assertEquals(
61
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes'",
62
            $this->selectQuery->assemble()
63
        );
64
    }
65
66 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...
67
    {
68
        $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...
69
        $this->selectQuery->where('id = 5')->orWhere('id = 7');
70
        static::assertEquals(
71
            "SELECT id, name FROM table x WHERE id = 5 OR id = 7",
72
            $this->selectQuery->assemble()
73
        );
74
    }
75
76
    public function testInitializeCondition()
77
    {
78
        $condition = $this->selectQuery->getCondition("lorem ipsum");
79
        static::assertThat($condition, $this->isInstanceOf("Nip\Database\Query\Condition\Condition"));
80
    }
81
82 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...
83
    {
84
        $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...
85
86
        $query = $this->connection->newQuery();
87
        $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...
88
        $query->where("id != 5");
89
90
        $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...
91
92
        static::assertEquals(
93
            "SELECT * FROM `table1` WHERE id NOT IN (SELECT * FROM `table2` WHERE id != 5)",
94
            $this->selectQuery->assemble()
95
        );
96
    }
97
98 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...
99
    {
100
        $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...
101
102
        $query = $this->connection->newQuery();
103
        $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...
104
105
        $union = $this->selectQuery->union($query);
106
107
        static::assertEquals("SELECT * FROM `table1` UNION SELECT * FROM `table2`", $union->assemble());
108
    }
109
110 View Code Duplication
    public function testJoinTableName()
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...
111
    {
112
        $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...
113
        $this->selectQuery->join("table2", ['id', 'id_table1']);
0 ignored issues
show
Documentation introduced by
array('id', 'id_table1') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|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...
114
115
        static::assertEquals(
116
            "SELECT * FROM `table1` JOIN `table2` ON `table1`.`id` = `table2`.`id_table1`",
117
            $this->selectQuery->assemble()
118
        );
119
    }
120
121 View Code Duplication
    public function testJoinTableNameWithAlias()
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...
122
    {
123
        $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...
124
        $this->selectQuery->join(["table2", "alias"], ['id', 'id_table1']);
0 ignored issues
show
Documentation introduced by
array('id', 'id_table1') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|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...
125
126
        static::assertEquals(
127
            "SELECT * FROM `table1` JOIN `table2` AS `alias` ON `table1`.`id` = `table2`.`id_table1`",
128
            $this->selectQuery->assemble()
129
        );
130
    }
131
132
    public function testJoinSubQuery()
133
    {
134
        $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...
135
136
        $query = $this->connection->newQuery();
137
        $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...
138
139
        $this->selectQuery->join([$query, "alias"], ['id', 'id_table1']);
0 ignored issues
show
Documentation introduced by
array('id', 'id_table1') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|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...
140
141
        static::assertEquals(
142
            'SELECT * FROM `table1` JOIN (SELECT * FROM `table2`) AS `alias` ON `table1`.`id` = `alias`.`id_table1`',
143
            $this->selectQuery->assemble()
144
        );
145
    }
146
147
    public function testHasPart()
148
    {
149
        $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...
150
        self::assertTrue($this->selectQuery->hasPart('cols'));
151
152
        $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...
153
        self::assertTrue($this->selectQuery->hasPart('cols'));
154
155
        $this->selectQuery->limit('');
156
        self::assertFalse($this->selectQuery->hasPart('limit'));
157
158
        $this->selectQuery->limit('6');
159
        self::assertTrue($this->selectQuery->hasPart('limit'));
160
161
        self::assertFalse($this->selectQuery->hasPart('where'));
162
    }
163
164
    public function testLimit()
165
    {
166
        $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...
167
        $this->selectQuery->where('id = 5')->where("active = 'yes'");
168
        $this->selectQuery->limit(5);
169
170
        static::assertEquals(
171
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5",
172
            $this->selectQuery->assemble()
173
        );
174
175
        $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...
176
        static::assertEquals(
177
            "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5,10",
178
            $this->selectQuery->assemble()
179
        );
180
    }
181
182 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...
183
    {
184
        parent::setUp();
185
        $this->selectQuery = new Select();
186
187
        $adapterMock = m::mock('Nip\Database\Adapters\MySQLi')->shouldDeferMissing();
188
        $adapterMock->shouldReceive('cleanData')->andReturnUsing(function ($data) {
189
            return $data;
190
        });
191
        $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...
192
        $this->connection->setAdapter($adapterMock);
193
        $this->selectQuery->setManager($this->connection);
194
    }
195
}
196