Passed
Push — master ( 9b7912...982aac )
by Janis
02:15
created

SetTraitTest::testBuildSetQueryPart()

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
nc 1
nop 2
dl 0
loc 7
c 0
b 0
f 0
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\Traits\BindTrait;
7
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\SetTrait;
8
9
class SetTraitTest extends AbstractTraitTestCase
10
{
11
    const SET_DEFAULT = [
12
        'column1' => 'column1 = :Column1_Update',
13
    ];
14
    const SET_BIND_DEFAULT = [
15
        'Column1_Update' => 'value1',
16
    ];
17
18
    /**
19
     * @var BindTrait|SetTrait
20
     */
21
    private $setTraitClass;
22
23
    public function setUp()
24
    {
25
        $this->setTraitClass = new class (SetTraitTest::SET_BIND_DEFAULT, SetTraitTest::SET_DEFAULT)
26
        {
27
            use BindTrait;
28
            use SetTrait;
29
30
            /**
31
             * @param array $bindDataDefault
32
             * @param array $setDataDefault
33
             */
34
            public function __construct(array $bindDataDefault, array $setDataDefault)
35
            {
36
                $this->bind = $bindDataDefault;
37
                $this->set = $setDataDefault;
38
            }
39
40
            /**
41
             * @return array
42
             */
43
            public function setData(): array
44
            {
45
                return $this->set;
46
            }
47
48
            public function clearSetData()
49
            {
50
                $this->set = [];
51
            }
52
53
            public function buildSetQueryPartPublic(): ?string
54
            {
55
                return $this->buildSetQueryPart();
56
            }
57
        };
58
    }
59
60
    /**
61
     * @dataProvider setData
62
     *
63
     * @param string $column
64
     * @param string|int|double|null $value
65
     */
66
    public function testSet($column, $value)
67
    {
68
        $object = $this->setTraitClass->set($column, $value);
0 ignored issues
show
Bug introduced by
It seems like set() 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

68
        /** @scrutinizer ignore-call */ 
69
        $object = $this->setTraitClass->set($column, $value);
Loading history...
69
        $this->assertObjectUsesTrait(BindTrait::class, $object);
70
        $this->assertObjectUsesTrait(SetTrait::class, $object);
71
        $this->assertEquals(
72
            \array_merge(
73
                static::SET_DEFAULT,
74
                \array_reduce(
75
                    \array_map(
76
                        function ($column) {
77
                            return [
78
                                $column => \sprintf(
79
                                    '%s = :%s_Update',
80
                                    $column,
81
                                    \implode(
82
                                        '_',
83
                                        \array_map(
84
                                            function ($columnPart) {
85
                                                return \mb_convert_case($columnPart, MB_CASE_TITLE);
86
                                            },
87
                                            \explode('.', $column)
88
                                        )
89
                                    )
90
                                ),
91
                            ];
92
                        },
93
                        [
94
                            $column
95
                        ]
96
                    ),
97
                    'array_merge',
98
                    []
99
                )
100
            ),
101
            $this->setTraitClass->setData()
0 ignored issues
show
Bug introduced by
It seems like setData() 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

101
            $this->setTraitClass->/** @scrutinizer ignore-call */ 
102
                                  setData()
Loading history...
102
        );
103
        $this->assertEquals(
104
            \array_merge(
105
                static::SET_BIND_DEFAULT,
106
                \array_reduce(
107
                    \array_map(
108
                        function ($column, $value) {
109
                            return [
110
                                \sprintf(
111
                                    '%s_Update',
112
                                    \implode(
113
                                        '_',
114
                                        \array_map(
115
                                            function ($columnPart) {
116
                                                return \mb_convert_case($columnPart, MB_CASE_TITLE);
117
                                            },
118
                                            \explode('.', $column)
119
                                        )
120
                                    )
121
                                ) => $value,
122
                            ];
123
                        },
124
                        [
125
                            $column
126
                        ],
127
                        [
128
                            $value
129
                        ]
130
                    ),
131
                    'array_merge',
132
                    []
133
                )
134
            ),
135
            $this->setTraitClass->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

135
            $this->setTraitClass->/** @scrutinizer ignore-call */ 
136
                                  bindData()
Loading history...
136
        );
137
    }
138
139
    /**
140
     * @dataProvider setData
141
     *
142
     * @param string $column
143
     * @param string|int|double|null $value
144
     */
145
    public function testBuildSetQueryPart($column, $value)
146
    {
147
        $this->setTraitClass->set($column, $value);
148
149
        $this->assertEquals(
150
            \sprintf('%s %s', ConditionEnum::SET, \implode(', ', \array_unique($this->setTraitClass->setData()))),
151
            $this->setTraitClass->buildSetQueryPartPublic()
0 ignored issues
show
Bug introduced by
It seems like buildSetQueryPartPublic() 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

151
            $this->setTraitClass->/** @scrutinizer ignore-call */ 
152
                                  buildSetQueryPartPublic()
Loading history...
Bug introduced by
The method buildSetQueryPartPublic() does not exist on Janisbiz\LightOrm\Dms\My...Builder\Traits\SetTrait. Did you maybe mean buildSetQueryPart()? ( Ignorable by Annotation )

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

151
            $this->setTraitClass->/** @scrutinizer ignore-call */ 
152
                                  buildSetQueryPartPublic()

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...
152
        );
153
    }
154
155
    public function testBuildSetQueryPartWhenEmpty()
156
    {
157
        $this->setTraitClass->clearSetData();
0 ignored issues
show
Bug introduced by
It seems like clearSetData() 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

157
        $this->setTraitClass->/** @scrutinizer ignore-call */ 
158
                              clearSetData();
Loading history...
158
159
        $this->assertEquals(null, $this->setTraitClass->buildSetQueryPartPublic());
160
    }
161
162
    /**
163
     *
164
     * @return array
165
     */
166
    public function setData()
167
    {
168
        return [
169
            [
170
                'column2',
171
                'value2'
172
            ],
173
            [
174
                'column3',
175
                3
176
            ],
177
            [
178
                'column4',
179
                4.4
180
            ],
181
            [
182
                'column5',
183
                null
184
            ],
185
            [
186
                'table1.column2',
187
                'value2'
188
            ],
189
            [
190
                'table1.column3',
191
                3
192
            ],
193
            [
194
                'table1.column4',
195
                4.4
196
            ],
197
            [
198
                'table1.column5',
199
                null
200
            ],
201
        ];
202
    }
203
}
204