Skip to content

Commit 7f19325

Browse files
authored
Merge pull request #9 from fabacino/feature/enhance-tests
Improve tests
2 parents 7e2958a + d49f39e commit 7f19325

File tree

5 files changed

+116
-203
lines changed

5 files changed

+116
-203
lines changed

tests/DbgTest.php

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,66 +16,35 @@
1616
*/
1717
class DbgTest extends \PHPUnit\Framework\TestCase
1818
{
19-
/**
20-
* Test printing number.
21-
*
22-
* @return void
23-
*/
2419
public function testPrintNumber()
2520
{
26-
$var = 123;
27-
$this->assertEquals('123', $this->captureOutput($var));
21+
$var = TestHelper::randomInt();
22+
23+
$this->assertEquals($var, $this->captureOutput($var));
2824
}
2925

30-
/**
31-
* Test printing string.
32-
*
33-
* @return void
34-
*/
3526
public function testPrintString()
3627
{
37-
$var = 'some string';
28+
$var = TestHelper::randomString();
29+
3830
$this->assertEquals($var, $this->captureOutput($var));
3931
}
4032

41-
/**
42-
* Test printing output.
43-
*
44-
* @return void
45-
*/
4633
public function testPrintArray()
4734
{
48-
$var = ['first', 'second', 'third'];
49-
$expected = <<<'EOT'
50-
Array
51-
(
52-
[0] => first
53-
[1] => second
54-
[2] => third
55-
)
35+
$var = TestHelper::randomArray();
36+
$expected = TestHelper::makeArrayOutput($var);
5637

57-
EOT;
5838
$this->assertEquals($expected, $this->captureOutput($var));
5939
}
6040

61-
/**
62-
* Test printing output in non-CLI environment.
63-
*
64-
* @return void
65-
*/
6641
public function testPrintArrayNonCli()
6742
{
6843
TestDebug::init();
69-
$var = ['first', 'second', 'third'];
70-
$expected = <<<'EOT'
71-
<pre>Array
72-
(
73-
[0] => first
74-
[1] => second
75-
[2] => third
76-
)
77-
</pre>
78-
EOT;
44+
45+
$var = TestHelper::randomArray();
46+
$expected = '<pre>' . TestHelper::makeArrayOutput($var) . '</pre>';
47+
7948
$this->assertEquals($expected, $this->captureOutput($var));
8049
}
8150

tests/DbgThrowTest.php

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,25 @@
1818
*/
1919
class DbgThrowTest extends \PHPUnit\Framework\TestCase
2020
{
21-
/**
22-
* Test printing number.
23-
*
24-
* @return void
25-
*/
2621
public function testThrowNumber()
2722
{
28-
$var = 123;
23+
$var = TestHelper::randomInt();
24+
2925
$this->assertEquals($var, $this->captureException($var));
3026
}
3127

32-
/**
33-
* Test printing string.
34-
*
35-
* @return void
36-
*/
3728
public function testThrowString()
3829
{
39-
$var = 'some string';
30+
$var = TestHelper::randomString();
31+
4032
$this->assertEquals($var, $this->captureException($var));
4133
}
4234

43-
/**
44-
* Test printing output.
45-
*
46-
* @return void
47-
*/
48-
public function testPrintArray()
35+
public function testThrowArray()
4936
{
50-
$var = ['first', 'second', 'third'];
51-
$expected = <<<'EOT'
52-
Array
53-
(
54-
[0] => first
55-
[1] => second
56-
[2] => third
57-
)
37+
$var = TestHelper::randomArray();
38+
$expected = TestHelper::makeArrayOutput($var);
5839

59-
EOT;
6040
$this->assertEquals($expected, $this->captureException($var));
6141
}
6242

tests/DbglogTest.php

Lines changed: 16 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@
1919
*/
2020
class DbglogTest extends \PHPUnit\Framework\TestCase
2121
{
22-
/**
23-
* Test printing number with log file.
24-
*
25-
* @return void
26-
*/
2722
public function testLogNumberWithLogFile()
2823
{
2924
$logfile = TestHelper::createTempFile();
3025
dbginit([
3126
'log_file' => $logfile
3227
]);
3328

34-
$var = random_int(PHP_INT_MIN, PHP_INT_MAX);
29+
$var = TestHelper::randomInt();
3530
dbglog($var);
3631

3732
$this->assertRegExp(
@@ -40,19 +35,14 @@ public function testLogNumberWithLogFile()
4035
);
4136
}
4237

43-
/**
44-
* Test printing string with log file.
45-
*
46-
* @return void
47-
*/
4838
public function testLogStringWithLogFile()
4939
{
5040
$logfile = TestHelper::createTempFile();
5141
dbginit([
5242
'log_file' => $logfile
5343
]);
5444

55-
$var = base64_encode(random_bytes(24));
45+
$var = TestHelper::randomString();
5646
dbglog($var);
5747

5848
$this->assertRegExp(
@@ -61,68 +51,44 @@ public function testLogStringWithLogFile()
6151
);
6252
}
6353

64-
/**
65-
* Test printing output with log file.
66-
*
67-
* @return void
68-
*/
6954
public function testLogArrayWithLogFile()
7055
{
7156
$logfile = TestHelper::createTempFile();
7257
dbginit([
7358
'log_file' => $logfile
7459
]);
7560

76-
$var = ['first', 'second', 'third'];
61+
$var = TestHelper::randomArray();
7762
dbglog($var);
7863

79-
$expected = <<<'EOT'
80-
Array
81-
(
82-
[0] => first
83-
[1] => second
84-
[2] => third
85-
)
86-
87-
EOT;
64+
$expected = TestHelper::makeArrayOutput($var);
8865
$this->assertRegExp(
8966
TestHelper::makePattern($expected),
9067
file_get_contents($logfile)
9168
);
9269
}
9370

94-
/**
95-
* Test printing data with no log file.
96-
*
97-
* @return void
98-
*/
9971
public function testLogWithNoLogFile()
10072
{
10173
$logfile = TestHelper::createTempFile();
10274
dbginit([
10375
'log_file' => null
10476
]);
10577

106-
$var = random_int(PHP_INT_MIN, PHP_INT_MAX);
78+
$var = TestHelper::randomInt();
10779
dbglog($var);
10880

10981
$this->assertEmpty(file_get_contents($logfile));
11082
}
11183

112-
/**
113-
* Test printing number with logger.
114-
*
115-
* @return void
116-
*/
11784
public function testLogNumberWithLogger()
11885
{
119-
$fp = tmpfile();
120-
$logfile = stream_get_meta_data($fp)['uri'];
86+
$logfile = TestHelper::createTempFile();
12187
dbginit([
12288
'logger' => new Logger($logfile)
12389
]);
12490

125-
$var = random_int(PHP_INT_MIN, PHP_INT_MAX);
91+
$var = TestHelper::randomInt();
12692
dbglog($var);
12793

12894
$this->assertRegExp(
@@ -131,20 +97,14 @@ public function testLogNumberWithLogger()
13197
);
13298
}
13399

134-
/**
135-
* Test printing string with logger.
136-
*
137-
* @return void
138-
*/
139100
public function testLogStringWithLogger()
140101
{
141-
$fp = tmpfile();
142-
$logfile = stream_get_meta_data($fp)['uri'];
102+
$logfile = TestHelper::createTempFile();
143103
dbginit([
144104
'logger' => new Logger($logfile)
145105
]);
146106

147-
$var = base64_encode(random_bytes(24));
107+
$var = TestHelper::randomString();
148108
dbglog($var);
149109

150110
$this->assertRegExp(
@@ -153,52 +113,33 @@ public function testLogStringWithLogger()
153113
);
154114
}
155115

156-
/**
157-
* Test printing output with logger.
158-
*
159-
* @return void
160-
*/
161116
public function testLogArrayWithLogger()
162117
{
163-
$fp = tmpfile();
164-
$logfile = stream_get_meta_data($fp)['uri'];
118+
$logfile = TestHelper::createTempFile();
165119
dbginit([
166120
'logger' => new Logger($logfile)
167121
]);
168122

169-
$var = ['first', 'second', 'third'];
123+
$var = TestHelper::randomArray();
170124
dbglog($var);
171125

172-
$expected = <<<'EOT'
173-
Array
174-
(
175-
[0] => first
176-
[1] => second
177-
[2] => third
178-
)
126+
$expected = TestHelper::makeArrayOutput($var);
179127

180-
EOT;
181128
$this->assertRegExp(
182129
TestHelper::makePattern($expected),
183130
file_get_contents($logfile)
184131
);
185132
}
186133

187-
/**
188-
* Test printing data with custom date format.
189-
*
190-
* @return void
191-
*/
192134
public function testLogWithLoggerAndCustomDateFormat()
193135
{
194-
$fp = tmpfile();
195-
$logfile = stream_get_meta_data($fp)['uri'];
136+
$logfile = TestHelper::createTempFile();
196137
$dateFormat = 'Y/m/d H/i/s';
197138
dbginit([
198139
'logger' => new Logger($logfile, $dateFormat)
199140
]);
200141

201-
$var = random_int(PHP_INT_MIN, PHP_INT_MAX);
142+
$var = TestHelper::randomInt();
202143
dbglog($var);
203144

204145
$this->assertRegExp(
@@ -207,20 +148,14 @@ public function testLogWithLoggerAndCustomDateFormat()
207148
);
208149
}
209150

210-
/**
211-
* Test printing data with no logger.
212-
*
213-
* @return void
214-
*/
215151
public function testLogWithNoLogger()
216152
{
217-
$fp = tmpfile();
218-
$logfile = stream_get_meta_data($fp)['uri'];
153+
$logfile = TestHelper::createTempFile();
219154
dbginit([
220155
'logger' => null
221156
]);
222157

223-
$var = random_int(PHP_INT_MIN, PHP_INT_MAX);
158+
$var = TestHelper::randomInt();
224159
dbglog($var);
225160

226161
$this->assertEmpty(file_get_contents($logfile));

0 commit comments

Comments
 (0)