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

MySqlDataLayerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 173
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=0);
3
4
namespace SetBased\Stratum\MySql\Test;
5
6
use SetBased\Stratum\MySql\MySqlDefaultConnector;
7
8
/**
9
 * Test cases for class MySqlDataLayer.
10
 */
11
class MySqlDataLayerTest extends DataLayerTestCase
12
{
13
  //--------------------------------------------------------------------------------------------------------------------
14
  /**
15
   * Tests for quoteFloat.
16
   */
17
  public function testQuoteFloat1()
18
  {
19
    $value    = 123.123;
20
    $expected = '123.123';
21
    self::assertSame($expected, $this->dataLayer->quoteFloat($value), var_export($value, true));
22
23
    $value    = '123.123';
24
    $expected = '123.123';
25
    self::assertSame($expected, $this->dataLayer->quoteFloat($value), var_export($value, true));
26
27
    $value    = 0;
28
    $expected = '0';
29
    self::assertSame($expected, $this->dataLayer->quoteFloat($value), var_export($value, true));
30
31
    $value    = '0';
32
    $expected = '0';
33
    self::assertSame($expected, $this->dataLayer->quoteFloat($value), var_export($value, true));
34
35
    $value    = null;
36
    $expected = 'null';
37
    self::assertSame($expected, $this->dataLayer->quoteFloat($value), var_export($value, true));
38
39
    $value    = false;
40
    $expected = '0';
41
    self::assertSame($expected, $this->dataLayer->quoteFloat($value), var_export($value, true));
42
43
    $value    = true;
44
    $expected = '1';
45
    self::assertSame($expected, $this->dataLayer->quoteFloat($value), var_export($value, true));
46
  }
47
48
  //--------------------------------------------------------------------------------------------------------------------
49
  /**
50
   * Tests for quoteInt.
51
   */
52
  public function testQuoteInt1()
53
  {
54
    $value    = 123;
55
    $expected = '123';
56
    self::assertSame($expected, $this->dataLayer->quoteInt($value), var_export($value, true));
57
58
    $value    = '123';
59
    $expected = '123';
60
    self::assertSame($expected, $this->dataLayer->quoteInt($value), var_export($value, true));
61
62
    $value    = 0;
63
    $expected = '0';
64
    self::assertSame($expected, $this->dataLayer->quoteInt($value), var_export($value, true));
65
66
    $value    = '0';
67
    $expected = '0';
68
    self::assertSame($expected, $this->dataLayer->quoteInt($value), var_export($value, true));
69
70
    $value    = null;
71
    $expected = 'null';
72
    self::assertSame($expected, $this->dataLayer->quoteInt($value), var_export($value, true));
73
74
    $value    = false;
75
    $expected = '0';
76
    self::assertSame($expected, $this->dataLayer->quoteInt($value), var_export($value, true));
77
78
    $value    = true;
79
    $expected = '1';
80
    self::assertSame($expected, $this->dataLayer->quoteInt($value), var_export($value, true));
81
  }
82
83
  //--------------------------------------------------------------------------------------------------------------------
84
  /**
85
   * Tests for quoteString.
86
   */
87
  public function testQuoteString1()
88
  {
89
    $value    = 123;
90
    $expected = "'123'";
91
    self::assertSame($expected, $this->dataLayer->quoteString($value), var_export($value, true));
92
93
    $value    = '123';
94
    $expected = "'123'";
95
    self::assertSame($expected, $this->dataLayer->quoteString($value), var_export($value, true));
96
97
    $value    = 0;
98
    $expected = "'0'";
99
    self::assertSame($expected, $this->dataLayer->quoteString($value), var_export($value, true));
100
101
    $value    = '0';
102
    $expected = "'0'";
103
    self::assertSame($expected, $this->dataLayer->quoteString($value), var_export($value, true));
104
105
    $value    = '';
106
    $expected = 'null';
107
    self::assertSame($expected, $this->dataLayer->quoteString($value), var_export($value, true));
108
109
    $value    = null;
110
    $expected = 'null';
111
    self::assertSame($expected, $this->dataLayer->quoteString($value), var_export($value, true));
112
113
    $value    = false;
114
    $expected = 'null';
115
    self::assertSame($expected, $this->dataLayer->quoteString($value), var_export($value, true));
116
117
    $value    = true;
118
    $expected = "'1'";
119
    self::assertSame($expected, $this->dataLayer->quoteString($value), var_export($value, true));
120
  }
121
122
  //--------------------------------------------------------------------------------------------------------------------
123
  /**
124
   * Tests for quoteString.
125
   */
126
  public function testQuoteString4()
127
  {
128
    $this->expectException(\TypeError::class);
129
    $this->dataLayer->quoteString($this);
130
  }
131
132
  //--------------------------------------------------------------------------------------------------------------------
133
  /**
134
   * Test connectIfNotAlive.
135
   *
136
   * @throws \Exception
137
   */
138
  public function testReconnect()
139
  {
140
    $connector = new MySqlDefaultConnector('localhost', 'test', 'test', 'test');
141
    $dl        = new TestMySqlDataLayer($connector);
142
143
    // Reconnect when not connected.
144
    $dl->connectIfNotAlive();
145
    self::assertTrue(true);
146
147
    // Reconnect when alive.
148
    $dl->connectIfNotAlive();
149
    self::assertTrue(true);
150
151
    // Reconnect when server has been gone.
152
    exec('sudo systemctl restart mysql > /dev/null 2>&1 || sudo service mysql restart');
153
    $dl->connectIfNotAlive();
154
    self::assertTrue(true);
155
156
    // Reconnect when disconnected.
157
    $dl->disconnect();
158
    $dl->connectIfNotAlive();
159
    self::assertTrue(true);
160
  }
161
162
  //--------------------------------------------------------------------------------------------------------------------
163
  /**
164
   * Test connectIfNotAlive.
165
   *
166
   * @throws \Exception
167
   */
168
  public function testSetConnector()
169
  {
170
    $connector1 = new MySqlDefaultConnector('x-localhost', 'x-test', 'x-test', 'x-test');
171
    $dl         = new TestMySqlDataLayer($connector1);
172
173
    $connector2 = new MySqlDefaultConnector('localhost', 'test', 'test', 'test');
174
    $dl->setConnector($connector2);
175
    self::assertTrue(true);
176
177
    $this->expectException(\LogicException::class);
178
    $dl->connect();
179
    $dl->setConnector($connector1);
180
  }
181
182
  //--------------------------------------------------------------------------------------------------------------------
183
}
184
185
//----------------------------------------------------------------------------------------------------------------------
186
187