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

ValueTraitTest::testBindValue()

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
nc 1
nop 0
dl 0
loc 16
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\ValueTrait;
7
8
class ValueTraitTest extends AbstractTraitTestCase
9
{
10
    const VALUE_DEFAULT = [
11
        'column1' => ':Column1_Value',
12
    ];
13
    const VALUE_BIND_DEFAULT = [
14
        'Column1_Value' => 'value1',
15
    ];
16
    const VALUE_BIND_OVERRIDE = [
17
        'Column1_Value' => 'value1_override',
18
    ];
19
    const VALUE_BIND = [
20
        'Column2_Value' => 'value2',
21
    ];
22
23
    /**
24
     * @var ValueTrait
25
     */
26
    private $valueTraitClass;
27
28
    public function setUp()
29
    {
30
        $this->valueTraitClass = new class (ValueTraitTest::VALUE_DEFAULT, ValueTraitTest::VALUE_BIND_DEFAULT)
31
        {
32
            use ValueTrait;
33
34
            /**
35
             * @param array $valueDataDefault
36
             * @param array $bindValueDataDefault
37
             */
38
            public function __construct(array $valueDataDefault, array $bindValueDataDefault)
39
            {
40
                $this->value = $valueDataDefault;
41
                $this->bindValue = $bindValueDataDefault;
42
            }
43
44
            /**
45
             * @return array
46
             */
47
            public function valueData(): array
48
            {
49
                return $this->value;
50
            }
51
52
            public function clearValueData()
53
            {
54
                $this->value = [];
55
            }
56
57
            /**
58
             * @return null|string
59
             */
60
            public function buildValueQueryPartPublic(): ?string
61
            {
62
                return $this->buildValueQueryPart();
63
            }
64
        };
65
    }
66
67
    /**
68
     * @dataProvider valueData
69
     *
70
     * @param string $column
71
     * @param null|int|string|double $value
72
     */
73
    public function testValue($column, $value)
74
    {
75
        $object = $this->valueTraitClass->value($column, $value);
76
        $this->assertObjectUsesTrait(ValueTrait::class, $object);
77
78
        $this->assertEquals(
79
            \array_merge(
80
                static::VALUE_DEFAULT,
81
                \array_reduce(
82
                    \array_map(
83
                        function ($column) {
84
                            return [
85
                                $column => \sprintf(
86
                                    ':%s_Value',
87
                                    \implode(
88
                                        '_',
89
                                        \array_map(
90
                                            function ($columnPart) {
91
                                                return \mb_convert_case($columnPart, MB_CASE_TITLE);
92
                                            },
93
                                            \explode('.', $column)
94
                                        )
95
                                    )
96
                                ),
97
                            ];
98
                        },
99
                        [
100
                            $column
101
                        ]
102
                    ),
103
                    'array_merge',
104
                    []
105
                )
106
            ),
107
            $this->valueTraitClass->valueData()
0 ignored issues
show
Bug introduced by
The method valueData() does not exist on Janisbiz\LightOrm\Dms\My...ilder\Traits\ValueTrait. Did you maybe mean value()? ( Ignorable by Annotation )

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

107
            $this->valueTraitClass->/** @scrutinizer ignore-call */ 
108
                                    valueData()

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...
108
        );
109
        $this->assertEquals(
110
            \array_merge(
111
                static::VALUE_BIND_DEFAULT,
112
                \array_reduce(
113
                    \array_map(
114
                        function ($column, $value) {
115
                            return [
116
                                \sprintf(
117
                                    '%s_Value',
118
                                    \implode(
119
                                        '_',
120
                                        \array_map(
121
                                            function ($columnPart) {
122
                                                return \mb_convert_case($columnPart, MB_CASE_TITLE);
123
                                            },
124
                                            \explode('.', $column)
125
                                        )
126
                                    )
127
                                ) => $value,
128
                            ];
129
                        },
130
                        [
131
                            $column
132
                        ],
133
                        [
134
                            $value
135
                        ]
136
                    ),
137
                    'array_merge',
138
                    []
139
                )
140
            ),
141
            $this->valueTraitClass->bindValueData()
142
        );
143
    }
144
145
    /**
146
     * @dataProvider valueData
147
     *
148
     * @param string $column
149
     * @param null|int|string|double $value
150
     */
151
    public function testBuildValueQueryPart($column, $value)
152
    {
153
        $this->valueTraitClass->value($column, $value);
154
155
        $this->assertEquals(
156
            \sprintf(
157
                '(%s) %s (%s)',
158
                \implode(', ', \array_keys($this->valueTraitClass->valueData())),
159
                ConditionEnum::VALUES,
160
                \implode(', ', $this->valueTraitClass->valueData())
161
            ),
162
            $this->valueTraitClass->buildValueQueryPartPublic()
0 ignored issues
show
Bug introduced by
The method buildValueQueryPartPublic() does not exist on Janisbiz\LightOrm\Dms\My...ilder\Traits\ValueTrait. Did you maybe mean buildValueQueryPart()? ( Ignorable by Annotation )

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

162
            $this->valueTraitClass->/** @scrutinizer ignore-call */ 
163
                                    buildValueQueryPartPublic()

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...
163
        );
164
    }
165
166
    public function testBuildValueQueryPartWhenEmpty()
167
    {
168
        $this->valueTraitClass->clearValueData();
0 ignored issues
show
Bug introduced by
It seems like clearValueData() 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

168
        $this->valueTraitClass->/** @scrutinizer ignore-call */ 
169
                                clearValueData();
Loading history...
169
170
        $this->assertEquals(null, $this->valueTraitClass->buildValueQueryPartPublic());
171
    }
172
173
    /**
174
     *
175
     * @return array
176
     */
177
    public function valueData()
178
    {
179
        return [
180
            [
181
                'column2',
182
                'value2'
183
            ],
184
            [
185
                'column3',
186
                3
187
            ],
188
            [
189
                'column4',
190
                4.4
191
            ],
192
            [
193
                'column5',
194
                null
195
            ],
196
            [
197
                'table1.column2',
198
                'value2'
199
            ],
200
            [
201
                'table1.column3',
202
                3
203
            ],
204
            [
205
                'table1.column4',
206
                4.4
207
            ],
208
            [
209
                'table1.column5',
210
                null
211
            ],
212
        ];
213
    }
214
215
    public function testBindValue()
216
    {
217
        $this->assertEquals(static::VALUE_BIND_DEFAULT, $this->valueTraitClass->bindValueData());
218
219
        $object = $this->valueTraitClass->bindValue(static::VALUE_BIND);
220
        $this->assertObjectUsesTrait(ValueTrait::class, $object);
221
        $this->assertEquals(
222
            \array_merge(static::VALUE_BIND_DEFAULT, static::VALUE_BIND),
223
            $this->valueTraitClass->bindValueData()
224
        );
225
226
        $object = $this->valueTraitClass->bindValue(static::VALUE_BIND_OVERRIDE);
227
        $this->assertObjectUsesTrait(ValueTrait::class, $object);
228
        $this->assertEquals(
229
            \array_merge(static::VALUE_BIND_DEFAULT, static::VALUE_BIND, static::VALUE_BIND_OVERRIDE),
230
            $this->valueTraitClass->bindValueData()
231
        );
232
    }
233
234
    public function testBindValueData()
235
    {
236
        $this->assertEquals(static::VALUE_BIND_DEFAULT, $this->valueTraitClass->bindValueData());
237
    }
238
}
239