OnDuplicateKeyUpdateTraitTest::setUp()
last analyzed

Size

Total Lines 38
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
nc 1
nop 0
dl 0
loc 38
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A OnDuplicateKeyUpdateTraitTest.php$0 ➔ onDuplicateKeyUpdateData() 0 3 1
A OnDuplicateKeyUpdateTraitTest.php$0 ➔ clearOnDuplicateKeyUpdateData() 0 3 1
A OnDuplicateKeyUpdateTraitTest.php$0 ➔ buildOnDuplicateKeyUpdateQueryPartPublic() 0 3 1
A OnDuplicateKeyUpdateTraitTest.php$0 ➔ __construct() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\QueryBuilder\Traits;
4
5
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderException;
6
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\OnDuplicateKeyUpdateTrait;
7
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\ValueTrait;
8
9
class OnDuplicateKeyUpdateTraitTest extends AbstractTraitTestCase
10
{
11
    const ON_DUPLICATE_KEY_UPDATE_DEFAULT = [
12
        'column1' => ':Column1_OnDuplicateKeyUpdate',
13
    ];
14
    const ON_DUPLICATE_KEY_UPDATE_BIND_VALUE_DEFAULT = [
15
        'Column1_OnDuplicateKeyUpdate' => 'value1',
16
    ];
17
    const ON_DUPLICATE_KEY_UPDATE_BIND_VALUE_OVERRIDE = [
18
        'Column1_OnDuplicateKeyUpdate' => 'value1_override',
19
    ];
20
    const ON_DUPLICATE_KEY_UPDATE_BIND_VALUE = [
21
        'Column2_OnDuplicateKeyUpdate' => 'value2',
22
    ];
23
24
    /**
25
     * @var ValueTrait|OnDuplicateKeyUpdateTrait
26
     */
27
    private $onDuplicateKeyUpdateTraitClass;
28
    
29
    public function setUp()
30
    {
31
        $this->onDuplicateKeyUpdateTraitClass = new class (
32
            OnDuplicateKeyUpdateTraitTest::ON_DUPLICATE_KEY_UPDATE_BIND_VALUE_DEFAULT,
33
            OnDuplicateKeyUpdateTraitTest::ON_DUPLICATE_KEY_UPDATE_DEFAULT
34
        ) {
35
            use ValueTrait;
36
            use OnDuplicateKeyUpdateTrait;
37
38
            /**
39
             * @param array $bindValueDataDefault
40
             * @param array $onDuplicateKeyUpdateDataDefault
41
             */
42
            public function __construct(array $bindValueDataDefault, array $onDuplicateKeyUpdateDataDefault)
43
            {
44
                $this->bindValue = $bindValueDataDefault;
45
                $this->onDuplicateKeyUpdate = $onDuplicateKeyUpdateDataDefault;
46
            }
47
48
            /**
49
             * @return array
50
             */
51
            public function onDuplicateKeyUpdateData(): array
52
            {
53
                return $this->onDuplicateKeyUpdate;
54
            }
55
56
            public function clearOnDuplicateKeyUpdateData()
57
            {
58
                $this->onDuplicateKeyUpdate = [];
59
            }
60
61
            /**
62
             * @return null|string
63
             */
64
            public function buildOnDuplicateKeyUpdateQueryPartPublic(): ?string
65
            {
66
                return $this->buildOnDuplicateKeyUpdateQueryPart();
67
            }
68
        };
69
    }
70
71
    /**
72
     * @dataProvider onDuplicateKeyUpdateData
73
     *
74
     * @param string $column
75
     * @param null|int|string|double $value
76
     */
77
    public function testOnDuplicateKeyUpdate(string $column, $value)
78
    {
79
        $object = $this->onDuplicateKeyUpdateTraitClass->onDuplicateKeyUpdate($column, $value);
0 ignored issues
show
Bug introduced by
It seems like onDuplicateKeyUpdate() 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

79
        /** @scrutinizer ignore-call */ 
80
        $object = $this->onDuplicateKeyUpdateTraitClass->onDuplicateKeyUpdate($column, $value);
Loading history...
80
        $this->assertObjectUsesTrait(ValueTrait::class, $object);
81
        $this->assertObjectUsesTrait(OnDuplicateKeyUpdateTrait::class, $object);
82
83
        $this->assertEquals(
84
            \array_merge(
85
                static::ON_DUPLICATE_KEY_UPDATE_DEFAULT,
86
                \array_reduce(
87
                    \array_map(
88
                        function ($column) {
89
                            return [
90
                                $column => \sprintf(
91
                                    '%s = :%s_OnDuplicateKeyUpdate',
92
                                    $column,
93
                                    \implode(
94
                                        '_',
95
                                        \array_map(
96
                                            function ($columnPart) {
97
                                                return \mb_convert_case($columnPart, MB_CASE_TITLE);
98
                                            },
99
                                            \explode('.', $column)
100
                                        )
101
                                    )
102
                                ),
103
                            ];
104
                        },
105
                        [
106
                            $column
107
                        ]
108
                    ),
109
                    'array_merge',
110
                    []
111
                )
112
            ),
113
            $this->onDuplicateKeyUpdateTraitClass->onDuplicateKeyUpdateData()
0 ignored issues
show
Bug introduced by
The method onDuplicateKeyUpdateData() does not exist on Janisbiz\LightOrm\Dms\My...DuplicateKeyUpdateTrait. Did you maybe mean onDuplicateKeyUpdate()? ( Ignorable by Annotation )

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

113
            $this->onDuplicateKeyUpdateTraitClass->/** @scrutinizer ignore-call */ 
114
                                                   onDuplicateKeyUpdateData()

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 onDuplicateKeyUpdateData() 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

113
            $this->onDuplicateKeyUpdateTraitClass->/** @scrutinizer ignore-call */ 
114
                                                   onDuplicateKeyUpdateData()
Loading history...
114
        );
115
        $this->assertEquals(
116
            \array_merge(
117
                static::ON_DUPLICATE_KEY_UPDATE_BIND_VALUE_DEFAULT,
118
                \array_reduce(
119
                    \array_map(
120
                        function ($column, $value) {
121
                            return [
122
                                \sprintf(
123
                                    '%s_OnDuplicateKeyUpdate',
124
                                    \implode(
125
                                        '_',
126
                                        \array_map(
127
                                            function ($columnPart) {
128
                                                return \mb_convert_case($columnPart, MB_CASE_TITLE);
129
                                            },
130
                                            \explode('.', $column)
131
                                        )
132
                                    )
133
                                ) => $value,
134
                            ];
135
                        },
136
                        [
137
                            $column
138
                        ],
139
                        [
140
                            $value
141
                        ]
142
                    ),
143
                    'array_merge',
144
                    []
145
                )
146
            ),
147
            $this->onDuplicateKeyUpdateTraitClass->bindValueData()
0 ignored issues
show
Bug introduced by
It seems like bindValueData() 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

147
            $this->onDuplicateKeyUpdateTraitClass->/** @scrutinizer ignore-call */ 
148
                                                   bindValueData()
Loading history...
148
        );
149
    }
150
151
    public function testBuildOnDuplicateKeyUpdateWhenEmptyColumnPassed()
152
    {
153
        $this->expectException(QueryBuilderException::class);
154
        $this->expectExceptionMessage('You must pass $column to onDuplicateKeyUpdate function!');
155
156
        $this->onDuplicateKeyUpdateTraitClass->onDuplicateKeyUpdate('', null);
157
    }
158
159
    /**
160
     * @dataProvider onDuplicateKeyUpdateData
161
     *
162
     * @param string $column
163
     * @param null|int|string|double $value
164
     */
165
    public function testBuildOnDuplicateKeyUpdateQueryPart(string $column, $value)
166
    {
167
        $this->onDuplicateKeyUpdateTraitClass->onDuplicateKeyUpdate($column, $value);
168
169
        $this->assertEquals(
170
            \sprintf(
171
                'ON DUPLICATE KEY UPDATE %s',
172
                \implode(', ', $this->onDuplicateKeyUpdateTraitClass->onDuplicateKeyUpdateData())
173
            ),
174
            $this->onDuplicateKeyUpdateTraitClass->buildOnDuplicateKeyUpdateQueryPartPublic()
0 ignored issues
show
Bug introduced by
It seems like buildOnDuplicateKeyUpdateQueryPartPublic() 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

174
            $this->onDuplicateKeyUpdateTraitClass->/** @scrutinizer ignore-call */ 
175
                                                   buildOnDuplicateKeyUpdateQueryPartPublic()
Loading history...
Bug introduced by
The method buildOnDuplicateKeyUpdateQueryPartPublic() does not exist on Janisbiz\LightOrm\Dms\My...DuplicateKeyUpdateTrait. Did you maybe mean buildOnDuplicateKeyUpdateQueryPart()? ( Ignorable by Annotation )

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

174
            $this->onDuplicateKeyUpdateTraitClass->/** @scrutinizer ignore-call */ 
175
                                                   buildOnDuplicateKeyUpdateQueryPartPublic()

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...
175
        );
176
    }
177
178
    public function testBuildOnDuplicateKeyUpdateQueryPartWhenEmpty()
179
    {
180
        $this->onDuplicateKeyUpdateTraitClass->clearOnDuplicateKeyUpdateData();
0 ignored issues
show
Bug introduced by
It seems like clearOnDuplicateKeyUpdateData() 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

180
        $this->onDuplicateKeyUpdateTraitClass->/** @scrutinizer ignore-call */ 
181
                                               clearOnDuplicateKeyUpdateData();
Loading history...
181
182
        $this->assertEquals(null, $this->onDuplicateKeyUpdateTraitClass->buildOnDuplicateKeyUpdateQueryPartPublic());
183
    }
184
185
    /**
186
     *
187
     * @return array
188
     */
189
    public function onDuplicateKeyUpdateData()
190
    {
191
        return [
192
            [
193
                'column2',
194
                'value2'
195
            ],
196
            [
197
                'column3',
198
                3
199
            ],
200
            [
201
                'column4',
202
                4.4
203
            ],
204
            [
205
                'column5',
206
                null
207
            ],
208
            [
209
                'table1.column2',
210
                'value2'
211
            ],
212
            [
213
                'table1.column3',
214
                3
215
            ],
216
            [
217
                'table1.column4',
218
                4.4
219
            ],
220
            [
221
                'table1.column5',
222
                null
223
            ],
224
        ];
225
    }
226
}
227