WhereTraitTest::testBuildWhereQueryPart()
last analyzed

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 16
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\QueryBuilder\Traits;
4
5
use Janisbiz\LightOrm\Dms\MySQL\Enum\ConditionEnum;
6
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderException;
7
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\BindTrait;
8
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\WhereTrait;
9
10
class WhereTraitTest extends AbstractTraitTestCase
11
{
12
    const WHERE_CONDITION_DEFAULT = [
13
        'column1 = :value1Bind',
14
    ];
15
    const WHERE_CONDITION_BIND_DEFAULT = [
16
        'value1Bind' => 'value1',
17
    ];
18
    const WHERE_CONDITION_EMPTY = '';
19
    const WHERE_CONDITION = 'column2 = :value2Bind';
20
    const WHERE_CONDITION_BIND = [
21
        'value2Bind' => 'value2',
22
    ];
23
    const WHERE_IN_OR_NOT_IN_COLUMN = 'column3';
24
    const WHERE_IN_OR_NOT_IN_COLUMN_VALUES = [
25
        'value3',
26
        'value4',
27
    ];
28
29
    /**
30
     * @var BindTrait|WhereTrait
31
     */
32
    private $whereTraitClass;
33
    
34
    public function setUp()
35
    {
36
        $this->whereTraitClass = new class (
37
            WhereTraitTest::WHERE_CONDITION_BIND_DEFAULT,
38
            WhereTraitTest::WHERE_CONDITION_DEFAULT
39
        ) {
40
            use BindTrait;
41
            use WhereTrait;
42
43
            /**
44
             * @param array $bindDataDefault
45
             * @param array $whereDataDefault
46
             */
47
            public function __construct(array $bindDataDefault, array $whereDataDefault)
48
            {
49
                $this->bind = $bindDataDefault;
50
                $this->where = $whereDataDefault;
51
            }
52
53
            /**
54
             * @return array
55
             */
56
            public function whereData(): array
57
            {
58
                return $this->where;
59
            }
60
            
61
            public function clearWhereData()
62
            {
63
                $this->where = [];
64
            }
65
66
            /**
67
             * @return null|string
68
             */
69
            public function buildWhereQueryPartPublic(): ?string
70
            {
71
                return $this->buildWhereQueryPart();
72
            }
73
        };
74
    }
75
    
76
    public function testWhere()
77
    {
78
        $object = $this->whereTraitClass->where(static::WHERE_CONDITION, static::WHERE_CONDITION_BIND);
0 ignored issues
show
Bug introduced by
It seems like where() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

78
        /** @scrutinizer ignore-call */ 
79
        $object = $this->whereTraitClass->where(static::WHERE_CONDITION, static::WHERE_CONDITION_BIND);
Loading history...
79
        $this->assertObjectUsesTrait(BindTrait::class, $object);
80
        $this->assertObjectUsesTrait(WhereTrait::class, $object);
81
        $this->assertEquals(
82
            \array_merge(static::WHERE_CONDITION_DEFAULT, [static::WHERE_CONDITION]),
83
            $this->whereTraitClass->whereData()
0 ignored issues
show
Bug introduced by
It seems like whereData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

83
            $this->whereTraitClass->/** @scrutinizer ignore-call */ 
84
                                    whereData()
Loading history...
Bug introduced by
The method whereData() does not exist on Janisbiz\LightOrm\Dms\My...ilder\Traits\WhereTrait. Did you maybe mean where()? ( Ignorable by Annotation )

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

83
            $this->whereTraitClass->/** @scrutinizer ignore-call */ 
84
                                    whereData()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
        );
85
        $this->assertEquals(
86
            \array_merge(static::WHERE_CONDITION_BIND_DEFAULT, static::WHERE_CONDITION_BIND),
87
            $this->whereTraitClass->bindData()
0 ignored issues
show
Bug introduced by
It seems like bindData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

87
            $this->whereTraitClass->/** @scrutinizer ignore-call */ 
88
                                    bindData()
Loading history...
88
        );
89
    }
90
91
    public function testWhereWithEmptyCondition()
92
    {
93
        $this->expectException(QueryBuilderException::class);
94
        $this->expectExceptionMessage('You must pass $condition name to where method!');
95
96
        $this->whereTraitClass->where(static::WHERE_CONDITION_EMPTY);
97
    }
98
99
    public function testWhereIn()
100
    {
101
        $object = $this
102
            ->whereTraitClass
103
            ->whereIn(
0 ignored issues
show
Bug introduced by
It seems like whereIn() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

103
        /** @scrutinizer ignore-call */ 
104
        $object = $this
Loading history...
104
                static::WHERE_IN_OR_NOT_IN_COLUMN,
105
                static::WHERE_IN_OR_NOT_IN_COLUMN_VALUES
106
            )
107
        ;
108
        $this->assertObjectUsesTrait(BindTrait::class, $object);
109
        $this->assertObjectUsesTrait(WhereTrait::class, $object);
110
        $this->assertEquals(
111
            \array_merge(
112
                static::WHERE_CONDITION_DEFAULT,
113
                [
114
                    \sprintf(
115
                        '%s IN (%s)',
116
                        static::WHERE_IN_OR_NOT_IN_COLUMN,
117
                        \implode(', ', \array_map(
118
                            function ($i) {
119
                                return \sprintf(':%d_%s_In', $i, static::WHERE_IN_OR_NOT_IN_COLUMN);
120
                            },
121
                            \array_keys(static::WHERE_IN_OR_NOT_IN_COLUMN_VALUES)
122
                        ))
123
                    ),
124
                ]
125
            ),
126
            $this->whereTraitClass->whereData()
127
        );
128
        $this->assertEquals(
129
            \array_merge(
130
                static::WHERE_CONDITION_BIND_DEFAULT,
131
                \array_reduce(
132
                    \array_map(
133
                        function ($value, $i) {
134
                            return [
135
                                \sprintf('%d_%s_In', $i, static::WHERE_IN_OR_NOT_IN_COLUMN) => $value,
136
                            ];
137
                        },
138
                        static::WHERE_IN_OR_NOT_IN_COLUMN_VALUES,
139
                        \array_keys(static::WHERE_IN_OR_NOT_IN_COLUMN_VALUES)
140
                    ),
141
                    'array_merge',
142
                    []
143
                )
144
            ),
145
            $this->whereTraitClass->bindData()
146
        );
147
    }
148
149
    public function testWhereNotIn()
150
    {
151
        $object = $this
152
            ->whereTraitClass
153
            ->whereNotIn(
0 ignored issues
show
Bug introduced by
It seems like whereNotIn() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

153
        /** @scrutinizer ignore-call */ 
154
        $object = $this
Loading history...
154
                static::WHERE_IN_OR_NOT_IN_COLUMN,
155
                static::WHERE_IN_OR_NOT_IN_COLUMN_VALUES
156
            )
157
        ;
158
        $this->assertObjectUsesTrait(BindTrait::class, $object);
159
        $this->assertObjectUsesTrait(WhereTrait::class, $object);
160
        $this->assertEquals(
161
            \array_merge(
162
                static::WHERE_CONDITION_DEFAULT,
163
                [
164
                    \sprintf(
165
                        '%s NOT IN (%s)',
166
                        static::WHERE_IN_OR_NOT_IN_COLUMN,
167
                        \implode(', ', \array_map(
168
                            function ($i) {
169
                                return \sprintf(':%d_%s_NotIn', $i, static::WHERE_IN_OR_NOT_IN_COLUMN);
170
                            },
171
                            \array_keys(static::WHERE_IN_OR_NOT_IN_COLUMN_VALUES)
172
                        ))
173
                    ),
174
                ]
175
            ),
176
            $this->whereTraitClass->whereData()
177
        );
178
        $this->assertEquals(
179
            \array_merge(
180
                static::WHERE_CONDITION_BIND_DEFAULT,
181
                \array_reduce(
182
                    \array_map(
183
                        function ($value, $i) {
184
                            return [
185
                                \sprintf('%d_%s_NotIn', $i, static::WHERE_IN_OR_NOT_IN_COLUMN) => $value,
186
                            ];
187
                        },
188
                        static::WHERE_IN_OR_NOT_IN_COLUMN_VALUES,
189
                        \array_keys(static::WHERE_IN_OR_NOT_IN_COLUMN_VALUES)
190
                    ),
191
                    'array_merge',
192
                    []
193
                )
194
            ),
195
            $this->whereTraitClass->bindData()
196
        );
197
    }
198
199
    public function testBuildWhereQueryPart()
200
    {
201
        $this
202
            ->whereTraitClass
203
            ->where(static::WHERE_CONDITION, static::WHERE_CONDITION_BIND)
204
            ->whereIn(static::WHERE_IN_OR_NOT_IN_COLUMN, static::WHERE_IN_OR_NOT_IN_COLUMN_VALUES)
205
            ->whereNotIn(static::WHERE_IN_OR_NOT_IN_COLUMN, static::WHERE_IN_OR_NOT_IN_COLUMN_VALUES)
206
        ;
207
208
        $this->assertEquals(
209
            \sprintf(
210
                '%s %s',
211
                ConditionEnum::WHERE,
212
                \implode(' AND ', \array_unique($this->whereTraitClass->whereData()))
213
            ),
214
            $this->whereTraitClass->buildWhereQueryPartPublic()
0 ignored issues
show
Bug introduced by
The method buildWhereQueryPartPublic() does not exist on Janisbiz\LightOrm\Dms\My...ilder\Traits\WhereTrait. Did you maybe mean buildWhereQueryPart()? ( Ignorable by Annotation )

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

214
            $this->whereTraitClass->/** @scrutinizer ignore-call */ 
215
                                    buildWhereQueryPartPublic()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like buildWhereQueryPartPublic() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

214
            $this->whereTraitClass->/** @scrutinizer ignore-call */ 
215
                                    buildWhereQueryPartPublic()
Loading history...
215
        );
216
    }
217
218
    public function testBuildWhereQueryPartWhenEmpty()
219
    {
220
        $this->whereTraitClass->clearWhereData();
0 ignored issues
show
Bug introduced by
It seems like clearWhereData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

220
        $this->whereTraitClass->/** @scrutinizer ignore-call */ 
221
                                clearWhereData();
Loading history...
221
222
        $this->assertEquals(null, $this->whereTraitClass->buildWhereQueryPartPublic());
223
    }
224
}
225