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

QuoteWithLobTest::genericInvalid()   D

Complexity

Conditions 32
Paths 5

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 4.1666
c 0
b 0
f 0
cc 32
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 with LOBs.
8
 */
9
class QuoteWithLobTest extends DataLayerTestCase
10
{
11
  //--------------------------------------------------------------------------------------------------------------------
12
  public function genericInvalid($column, $value)
13
  {
14
    try
15
    {
16
      $this->dataLayer->tstTest02(($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=='decimal') ? $value : null,
22
                                  ($column=='decimal0') ? $value : null,
23
                                  ($column=='float') ? $value : null,
24
                                  ($column=='double') ? $value : null,
25
                                  ($column=='bit') ? $value : null,
26
                                  ($column=='date') ? $value : null,
27
                                  ($column=='datetime') ? $value : null,
28
                                  ($column=='timestamp') ? $value : null,
29
                                  ($column=='time') ? $value : null,
30
                                  ($column=='year') ? $value : null,
31
                                  ($column=='char') ? $value : null,
32
                                  ($column=='varchar') ? $value : null,
33
                                  ($column=='binary') ? $value : null,
34
                                  ($column=='varbinary') ? $value : null,
35
                                  ($column=='tinyblob') ? $value : null,
36
                                  ($column=='blob') ? $value : null,
37
                                  ($column=='mediumblob') ? $value : null,
38
                                  ($column=='longblob') ? $value : null,
39
                                  ($column=='tinytext') ? $value : null,
40
                                  ($column=='text') ? $value : null,
41
                                  ($column=='mediumtext') ? $value : null,
42
                                  ($column=='longtext') ? $value : null,
43
                                  ($column=='enum') ? $value : null,
44
                                  ($column=='set') ? $value : null);
45
      self::assertTrue(false, "column: $column, value: $value");
46
    }
47
    catch (\TypeError $e)
48
    {
49
      self::assertTrue(true);
50
    }
51
    catch (\RuntimeException $e)
52
    {
53
      self::assertTrue(true);
54
    }
55
  }
56
57
  //--------------------------------------------------------------------------------------------------------------------
58
  public function genericValid($column, $value)
59
  {
60
    $n = $this->dataLayer->tstTest02(($column=='int') ? $value : null,
61
                                     ($column=='smallint') ? $value : null,
62
                                     ($column=='tinyint') ? $value : null,
63
                                     ($column=='mediumint') ? $value : null,
64
                                     ($column=='bigint') ? $value : null,
65
                                     ($column=='decimal') ? $value : null,
66
                                     ($column=='decimal0') ? $value : null,
67
                                     ($column=='float') ? $value : null,
68
                                     ($column=='double') ? $value : null,
69
                                     ($column=='bit') ? $value : null,
70
                                     ($column=='date') ? $value : null,
71
                                     ($column=='datetime') ? $value : null,
72
                                     ($column=='timestamp') ? $value : null,
73
                                     ($column=='time') ? $value : null,
74
                                     ($column=='year') ? $value : null,
75
                                     ($column=='char') ? $value : null,
76
                                     ($column=='varchar') ? $value : null,
77
                                     ($column=='binary') ? $value : null,
78
                                     ($column=='varbinary') ? $value : null,
79
                                     ($column=='tinyblob') ? $value : null,
80
                                     ($column=='blob') ? $value : null,
81
                                     ($column=='mediumblob') ? $value : null,
82
                                     ($column=='longblob') ? $value : null,
83
                                     ($column=='tinytext') ? $value : null,
84
                                     ($column=='text') ? $value : null,
85
                                     ($column=='mediumtext') ? $value : null,
86
                                     ($column=='longtext') ? $value : null,
87
                                     ($column=='enum') ? $value : null,
88
                                     ($column=='set') ? $value : null);
89
    self::assertEquals(1, $n);
90
  }
91
92
  //--------------------------------------------------------------------------------------------------------------------
93
  /**
94
   * Test illegal values will raise an exception.
95
   */
96
  public function testInvalid()
97
  {
98
    $tests = [];
99
100
    $tests[] = ['column' => 'int', 'value' => 'abc'];
101
    $tests[] = ['column' => 'smallint', 'value' => 'abc'];
102
    $tests[] = ['column' => 'tinyint', 'value' => 'abc'];
103
    $tests[] = ['column' => 'mediumint', 'value' => 'abc'];
104
    $tests[] = ['column' => 'bigint', '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' => 'decimal', 'value' => '0.1'];
143
    $tests[] = ['column' => 'float', 'value' => 0.1];
144
    $tests[] = ['column' => 'double', 'value' => 0.1];
145
146
    $tests[] = ['column' => 'bit', 'value' => '1010'];
147
148
    $tests[] = ['column' => 'date', 'value' => date('Y-m-d')];
149
    $tests[] = ['column' => 'datetime', 'value' => date('Y-m-d H:i:s')];
150
    $tests[] = ['column' => 'timestamp', 'value' => date('Y-m-d H:i:s')];
151
    $tests[] = ['column' => 'time', 'value' => date('H:i:s')];
152
    $tests[] = ['column' => 'year', 'value' => 2000];
153
154
    $tests[] = ['column' => 'char', 'value' => '1234'];
155
    $tests[] = ['column' => 'char', 'value' => 'abc'];
156
    $tests[] = ['column' => 'char', 'value' => "0xC8 ' --"];
157
158
    $tests[] = ['column' => 'varchar', 'value' => '1234'];
159
    $tests[] = ['column' => 'varchar', 'value' => 'abc'];
160
    $tests[] = ['column' => 'varchar', 'value' => "0xC8 ' --"];
161
162
    $tests[] = ['column' => 'binary', 'value' => '1010'];
163
    $tests[] = ['column' => 'binary', 'value' => "\xFF\x7F\x80\x5c\x00\x10"];
164
    $tests[] = ['column' => 'varbinary', 'value' => '1010'];
165
    $tests[] = ['column' => 'varbinary', 'value' => "\xFF\x7F\x80\x5c\x00\x10"];
166
167
    $tests[] = ['column' => 'tinyblob', 'value' => 'abc'];
168
    $tests[] = ['column' => 'blob', 'value' => 'abc'];
169
    $tests[] = ['column' => 'mediumblob', 'value' => 'abc'];
170
    $tests[] = ['column' => 'longblob', 'value' => 'abc'];
171
172
    $tests[] = ['column' => 'tinytext', 'value' => 'abc'];
173
    $tests[] = ['column' => 'text', 'value' => 'abc'];
174
    $tests[] = ['column' => 'mediumtext', 'value' => 'abc'];
175
    $tests[] = ['column' => 'longtext', 'value' => 'abc'];
176
177
    $tests[] = ['column' => 'enum', 'value' => 'a'];
178
    $tests[] = ['column' => 'enum', 'value' => 'b'];
179
180
    $tests[] = ['column' => 'set', 'value' => 'a'];
181
    $tests[] = ['column' => 'set', 'value' => 'b'];
182
183
    foreach ($tests as $test)
184
    {
185
      $this->genericValid($test['column'], $test['value']);
186
    }
187
  }
188
189
  //--------------------------------------------------------------------------------------------------------------------
190
}
191
192
//----------------------------------------------------------------------------------------------------------------------
193