Skip to content

Commit 42290af

Browse files
committed
clean up tests, improve coverage
1 parent 58a32e6 commit 42290af

17 files changed

+69
-47
lines changed

tests/Rules/UseSafeCallables/expr.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
$json_encode_name = 'json_encode';
4+
$encode_from_expression = $json_encode_name(...);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
var_dump(...);
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
json_encode(...);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use function Safe\json_encode;
4+
5+
json_encode(...);

tests/Rules/UseSafeCallablesRuleTest.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,28 @@ protected function getRule(): Rule
1515
return new UseSafeCallablesRule();
1616
}
1717

18-
public function testFirstClassCallable(): void
18+
public function testUnsafe(): void
1919
{
20-
$this->analyse([__DIR__ . '/data/first_class_callable.php'], [
20+
$this->analyse([__DIR__ . '/UseSafeCallables/unsafe.php'], [
2121
[
2222
"Function json_encode is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\\json_encode;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.",
2323
3,
2424
],
2525
]);
2626
}
27+
28+
public function testUseSafe(): void
29+
{
30+
$this->analyse([__DIR__ . '/UseSafeCallables/use_safe.php'], []);
31+
}
32+
33+
public function testNativeSafe(): void
34+
{
35+
$this->analyse([__DIR__ . '/UseSafeCallables/native_safe.php'], []);
36+
}
37+
38+
public function testExpr(): void
39+
{
40+
$this->analyse([__DIR__ . '/UseSafeCallables/expr.php'], []);
41+
}
2742
}

tests/Rules/UseSafeClassesRuleTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected function getRule(): Rule
1717

1818
public function testDateTime(): void
1919
{
20-
$this->analyse([__DIR__ . '/data/datetime.php'], [
20+
$this->analyse([__DIR__ . '/UseSafeClassesRule/datetime.php'], [
2121
[
2222
"Class DateTime is unsafe to use. Its methods can return FALSE instead of throwing an exception. Please add 'use Safe\DateTime;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.",
2323
3,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
$fopen = 'fopen';
4+
$fopen('foobar', 'r');

tests/Rules/data/safe_json_decode.php renamed to tests/Rules/UseSafeFunctionsRule/native_safe.php

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
var_dump();
4+
35
// Test various combinations of flags
46
json_decode("{}", true, 512, JSON_THROW_ON_ERROR);
57
json_decode("{}", true, 512, JSON_INVALID_UTF8_IGNORE | JSON_THROW_ON_ERROR);
@@ -12,3 +14,16 @@
1214

1315
// Test named arguments instead of positional
1416
json_decode("{}", flags: JSON_THROW_ON_ERROR);
17+
18+
// Various combinations of flags
19+
json_encode([], JSON_THROW_ON_ERROR, 512);
20+
json_encode([], JSON_FORCE_OBJECT | JSON_THROW_ON_ERROR, 512);
21+
json_encode([], JSON_FORCE_OBJECT | JSON_INVALID_UTF8_IGNORE | JSON_THROW_ON_ERROR, 512);
22+
23+
// Test raw integers too
24+
json_encode([], 4194304, 512);
25+
json_encode([], 16 | 4194304, 512);
26+
json_encode([], 16 | 1048576 | 4194304, 512);
27+
28+
// Named arguments
29+
json_encode([], flags: JSON_THROW_ON_ERROR);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
fopen('foobar', 'r');
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
2+
3+
use function Safe\fopen;
24
use function Safe\preg_replace;
35

6+
fopen('foobar', 'r');
7+
48
$x = preg_replace('/foo/', 'bar', 'baz');
59
$y = stripos($x, 'foo');
610

7-
$x = preg_replace(['/foo/'], ['bar'], ['baz']);
11+
$x = Safe\preg_replace(['/foo/'], ['bar'], ['baz']);

tests/Rules/UseSafeFunctionsRuleTest.php

+9-14
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,28 @@ protected function getRule(): Rule
1515
return new UseSafeFunctionsRule();
1616
}
1717

18-
public function testCatch(): void
18+
public function testUnsafe(): void
1919
{
20-
$this->analyse([__DIR__ . '/data/fopen.php'], [
20+
$this->analyse([__DIR__ . '/UseSafeFunctionsRule/unsafe.php'], [
2121
[
2222
"Function fopen is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\\fopen;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.",
23-
4,
23+
3,
2424
],
2525
]);
2626
}
2727

28-
public function testNoCatchSafe(): void
28+
public function testUseSafe(): void
2929
{
30-
$this->analyse([__DIR__ . '/data/safe_fopen.php'], []);
30+
$this->analyse([__DIR__ . '/UseSafeFunctionsRule/use_safe.php'], []);
3131
}
3232

33-
public function testExprCall(): void
33+
public function testNativeSafe(): void
3434
{
35-
$this->analyse([__DIR__ . '/data/undirect_call.php'], []);
35+
$this->analyse([__DIR__ . '/UseSafeFunctionsRule/native_safe.php'], []);
3636
}
3737

38-
public function testJSONDecodeNoCatchSafe(): void
38+
public function testExpr(): void
3939
{
40-
$this->analyse([__DIR__ . '/data/safe_json_decode.php'], []);
41-
}
42-
43-
public function testJSONEncodeNoCatchSafe(): void
44-
{
45-
$this->analyse([__DIR__ . '/data/safe_json_encode.php'], []);
40+
$this->analyse([__DIR__ . '/UseSafeFunctionsRule/expr.php'], []);
4641
}
4742
}

tests/Rules/data/first_class_callable.php

-4
This file was deleted.

tests/Rules/data/fopen.php

-5
This file was deleted.

tests/Rules/data/safe_fopen.php

-6
This file was deleted.

tests/Rules/data/safe_json_encode.php

-9
This file was deleted.

tests/Rules/data/undirect_call.php

-5
This file was deleted.

0 commit comments

Comments
 (0)