Test Failed
Push — master ( eef45f...e0c5e8 )
by P.R.
04:35
created

QuoteTest::genericInvalid()   D

Complexity

Conditions 29
Paths 5

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 4.1666
c 0
b 0
f 0
cc 29
nc 5
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\Test;
5
6
/**
7
 * Test cases for quoting variables.
8
 */
9
class QuoteTest extends DataLayerTestCase
10
{
11
  //--------------------------------------------------------------------------------------------------------------------
12
  public function genericInvalid($column, $value)
13
  {
14
    try
15
    {
16
      $this->dataLayer->tstTest01(($column=='int') ? $value : null,
17
                                  ($column=='smallint') ? $value : null,
18
                                  ($column=='tinyint') ? $value : null,
19
                                  ($column=='mediumint') ? $value : null,
20
                                  ($column=='bigint') ? $value : null,
21
                                  ($column=='int_unsigned') ? $value : null,
22
                                  ($column=='smallint_unsigned') ? $value : null,
23
                                  ($column=='tinyint_unsigned') ? $value : null,
24
                                  ($column=='mediumint_unsigned') ? $value : null,
25
                                  ($column=='bigint_unsigned') ? $value : null,
26
                                  ($column=='decimal') ? $value : null,
27
                                  ($column=='decimal0') ? $value : null,
28
                                  ($column=='float') ? $value : null,
29
                                  ($column=='double') ? $value : null,
30
                                  ($column=='bit') ? $value : null,
31
                                  ($column=='date') ? $value : null,
32
                                  ($column=='datetime') ? $value : null,
33
                                  ($column=='timestamp') ? $value : null,
34
                                  ($column=='time') ? $value : null,
35
                                  ($column=='year') ? $value : null,
36
                                  ($column=='char') ? $value : null,
37
                                  ($column=='varchar') ? $value : null,
38
                                  ($column=='binary') ? $value : null,
39
                                  ($column=='varbinary') ? $value : null,
40
                                  ($column=='enum') ? $value : null,
41
                                  ($column=='set') ? $value : null);
42
      self::assertTrue(false, "column: $column, value: $value");
43
    }
44
    catch (\TypeError $e)
45
    {
46
      self::assertTrue(true);
47
    }
48
    catch (\RuntimeException $e)
49
    {
50
      self::assertTrue(true);
51
    }
52
  }
53
54
  //--------------------------------------------------------------------------------------------------------------------
55
  public function genericValid($column, $value)
56
  {
57
    $n = $this->dataLayer->tstTest01(($column=='int') ? $value : null,
58
                                     ($column=='smallint') ? $value : null,
59
                                     ($column=='tinyint') ? $value : null,
60
                                     ($column=='mediumint') ? $value : null,
61
                                     ($column=='bigint') ? $value : null,
62
                                     ($column=='int_unsigned') ? $value : null,
63
                                     ($column=='smallint_unsigned') ? $value : null,
64
                                     ($column=='tinyint_unsigned') ? $value : null,
65
                                     ($column=='mediumint_unsigned') ? $value : null,
66
                                     ($column=='bigint_unsigned') ? $value : null,
67
                                     ($column=='decimal') ? $value : null,
68
                                     ($column=='decimal0') ? $value : null,
69
                                     ($column=='float') ? $value : null,
70
                                     ($column=='double') ? $value : null,
71
                                     ($column=='bit') ? $value : null,
72
                                     ($column=='date') ? $value : null,
73
                                     ($column=='datetime') ? $value : null,
74
                                     ($column=='timestamp') ? $value : null,
75
                                     ($column=='time') ? $value : null,
76
                                     ($column=='year') ? $value : null,
77
                                     ($column=='char') ? $value : null,
78
                                     ($column=='varchar') ? $value : null,
79
                                     ($column=='binary') ? $value : null,
80
                                     ($column=='varbinary') ? $value : null,
81
                                     ($column=='enum') ? $value : null,
82
                                     ($column=='set') ? $value : null);
83
    self::assertEquals(1, $n);
84
  }
85
86
  //--------------------------------------------------------------------------------------------------------------------
87
  /**
88
   * Test illegal values will raise an exception.
89
   */
90
  public function testInvalid()
91
  {
92
    $tests = [];
93
94
    $tests[] = ['column' => 'int', 'value' => 'abc'];
95
    $tests[] = ['column' => 'smallint', 'value' => 'abc'];
96
    $tests[] = ['column' => 'tinyint', 'value' => 'abc'];
97
    $tests[] = ['column' => 'mediumint', 'value' => 'abc'];
98
    $tests[] = ['column' => 'bigint', 'value' => 'abc'];
99
100
    $tests[] = ['column' => 'int_unsigned', 'value' => 'abc'];
101
    $tests[] = ['column' => 'smallint_unsigned', 'value' => 'abc'];
102
    $tests[] = ['column' => 'tinyint_unsigned', 'value' => 'abc'];
103
    $tests[] = ['column' => 'mediumint_unsigned', 'value' => 'abc'];
104
    $tests[] = ['column' => 'bigint_unsigned', 'value' => 'abc'];
105
106
    $tests[] = ['column' => 'decimal', 'value' => 'abc'];
107
    $tests[] = ['column' => 'float', 'value' => 'abc'];
108
    $tests[] = ['column' => 'double', 'value' => 'abc'];
109
110
    $tests[] = ['column' => 'bit', 'value' => 'abc'];
111
112
    $tests[] = ['column' => 'date', 'value' => 'qwerty'];
113
    $tests[] = ['column' => 'datetime', 'value' => 'qwerty'];
114
    $tests[] = ['column' => 'timestamp', 'value' => 'qwerty'];
115
    $tests[] = ['column' => 'time', 'value' => 'qwerty'];
116
117
    $tests[] = ['column' => 'year', 'value' => 'abc'];
118
119
    $tests[] = ['column' => 'enum', 'value' => 'c'];
120
    $tests[] = ['column' => 'set', 'value' => 'c'];
121
122
    foreach ($tests as $test)
123
    {
124
      $this->genericInvalid($test['column'], $test['value']);
125
    }
126
  }
127
128
  //--------------------------------------------------------------------------------------------------------------------
129
  /**
130
   * Test all column types are quoted properly.
131
   */
132
  public function testValid()
133
  {
134
    $tests = [];
135
136
    $tests[] = ['column' => 'int', 'value' => 1];
137
    $tests[] = ['column' => 'smallint', 'value' => 1];
138
    $tests[] = ['column' => 'tinyint', 'value' => 1];
139
    $tests[] = ['column' => 'mediumint', 'value' => 1];
140
    $tests[] = ['column' => 'bigint', 'value' => 1];
141
142
    $tests[] = ['column' => 'int_unsigned', 'value' => 1];
143
    $tests[] = ['column' => 'smallint_unsigned', 'value' => 1];
144
    $tests[] = ['column' => 'tinyint_unsigned', 'value' => 1];
145
    $tests[] = ['column' => 'mediumint_unsigned', 'value' => 1];
146
    $tests[] = ['column' => 'bigint_unsigned', 'value' => 1];
147
148
    $tests[] = ['column' => 'decimal', 'value' => '0.1'];
149
    $tests[] = ['column' => 'decimal0', 'value' => 1];
150
    $tests[] = ['column' => 'float', 'value' => 0.1];
151
    $tests[] = ['column' => 'double', 'value' => 0.1];
152
153
    $tests[] = ['column' => 'bit', 'value' => '1010'];
154
155
    $tests[] = ['column' => 'date', 'value' => date('Y-m-d')];
156
    $tests[] = ['column' => 'datetime', 'value' => date('Y-m-d H:i:s')];
157
    $tests[] = ['column' => 'timestamp', 'value' => date('Y-m-d H:i:s')];
158
    $tests[] = ['column' => 'time', 'value' => date('H:i:s')];
159
    $tests[] = ['column' => 'year', 'value' => 2000];
160
161
    $tests[] = ['column' => 'char', 'value' => '1234'];
162
    $tests[] = ['column' => 'char', 'value' => 'abc'];
163
    $tests[] = ['column' => 'char', 'value' => "0xC8 ' --"];
164
165
    $tests[] = ['column' => 'varchar', 'value' => 'abc'];
166
    $tests[] = ['column' => 'varchar', 'value' => "0xC8 ' --"];
167
168
    $tests[] = ['column' => 'binary', 'value' => '1010'];
169
    $tests[] = ['column' => 'binary', 'value' => "\xFF\x7F\x80\x5c\x00\x10"];
170
    $tests[] = ['column' => 'varbinary', 'value' => '1010'];
171
    $tests[] = ['column' => 'varbinary', 'value' => "\xFF\x7F\x80\x5c\x00\x10"];
172
173
    $tests[] = ['column' => 'enum', 'value' => 'a'];
174
    $tests[] = ['column' => 'enum', 'value' => 'b'];
175
176
    $tests[] = ['column' => 'set', 'value' => 'a'];
177
    $tests[] = ['column' => 'set', 'value' => 'b'];
178
179
    foreach ($tests as $test)
180
    {
181
      $this->genericValid($test['column'], $test['value']);
182
    }
183
  }
184
185
  //--------------------------------------------------------------------------------------------------------------------
186
}
187
188
//----------------------------------------------------------------------------------------------------------------------
189