File tree 4 files changed +61
-83
lines changed
4 files changed +61
-83
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ /**
6
+ * Copyright (c) 2018 Andreas Möller.
7
+ *
8
+ * For the full copyright and license information, please view
9
+ * the LICENSE file that was distributed with this source code.
10
+ *
11
+ * @see https://github.com/localheinz/json-normalizer
12
+ */
13
+
14
+ namespace Localheinz \Json \Normalizer \Test \Unit ;
15
+
16
+ use Localheinz \Json \Normalizer \NormalizerInterface ;
17
+ use Localheinz \Test \Util \Helper ;
18
+ use PHPUnit \Framework ;
19
+
20
+ abstract class AbstractNormalizerTestCase extends Framework \TestCase
21
+ {
22
+ use Helper;
23
+
24
+ final public function testImplementsNormalizerInterface ()
25
+ {
26
+ $ this ->assertClassImplementsInterface (NormalizerInterface::class, $ this ->className ());
27
+ }
28
+
29
+ final public function testNormalizeRejectsInvalidJson ()
30
+ {
31
+ $ json = $ this ->faker ()->realText ();
32
+
33
+ $ reflection = new \ReflectionClass ($ this ->className ());
34
+
35
+ $ normalizer = $ reflection ->newInstanceWithoutConstructor ();
36
+
37
+ $ this ->expectException (\InvalidArgumentException::class);
38
+ $ this ->expectExceptionMessage (\sprintf (
39
+ '"%s" is not valid JSON. ' ,
40
+ $ json
41
+ ));
42
+
43
+ $ normalizer ->normalize ($ json );
44
+ }
45
+
46
+ final protected function className (): string
47
+ {
48
+ return \preg_replace (
49
+ '/Test$/ ' ,
50
+ '' ,
51
+ \str_replace (
52
+ 'Localheinz \\Json \\Normalizer \\Test \\Unit \\' ,
53
+ 'Localheinz \\Json \\Normalizer \\' ,
54
+ static ::class
55
+ )
56
+ );
57
+ }
58
+ }
Original file line number Diff line number Diff line change 14
14
namespace Localheinz \Json \Normalizer \Test \Unit ;
15
15
16
16
use Localheinz \Json \Normalizer \FinalNewLineNormalizer ;
17
- use Localheinz \Json \Normalizer \NormalizerInterface ;
18
- use Localheinz \Test \Util \Helper ;
19
- use PHPUnit \Framework ;
20
17
21
- final class FinalNewLineNormalizerTest extends Framework \TestCase
18
+ final class FinalNewLineNormalizerTest extends AbstractNormalizerTestCase
22
19
{
23
- use Helper;
24
-
25
- public function testImplementsNormalizerInterface ()
26
- {
27
- $ this ->assertClassImplementsInterface (NormalizerInterface::class, FinalNewLineNormalizer::class);
28
- }
29
-
30
- public function testNormalizeRejectsInvalidJson ()
31
- {
32
- $ json = $ this ->faker ()->realText ();
33
-
34
- $ normalizer = new FinalNewLineNormalizer ();
35
-
36
- $ this ->expectException (\InvalidArgumentException::class);
37
- $ this ->expectExceptionMessage (\sprintf (
38
- '"%s" is not valid JSON. ' ,
39
- $ json
40
- ));
41
-
42
- $ normalizer ->normalize ($ json );
43
- }
44
-
45
20
/**
46
21
* @dataProvider providerWhitespace
47
22
*
Original file line number Diff line number Diff line change 14
14
namespace Localheinz \Json \Normalizer \Test \Unit ;
15
15
16
16
use Localheinz \Json \Normalizer \IndentNormalizer ;
17
- use Localheinz \Json \Normalizer \NormalizerInterface ;
18
17
use Localheinz \Json \Printer \PrinterInterface ;
19
- use Localheinz \Test \Util \Helper ;
20
- use PHPUnit \Framework ;
21
18
use Prophecy \Argument ;
22
19
23
- final class IndentNormalizerTest extends Framework \TestCase
20
+ final class IndentNormalizerTest extends AbstractNormalizerTestCase
24
21
{
25
- use Helper;
26
-
27
- public function testImplementsNormalizerInterface ()
28
- {
29
- $ this ->assertClassImplementsInterface (NormalizerInterface::class, IndentNormalizer::class);
30
- }
31
-
32
22
/**
33
23
* @dataProvider providerInvalidIndent
34
24
*
@@ -62,26 +52,6 @@ public function providerInvalidIndent(): \Generator
62
52
}
63
53
}
64
54
65
- public function testNormalizeRejectsInvalidJson ()
66
- {
67
- $ indent = ' ' ;
68
-
69
- $ json = $ this ->faker ()->realText ();
70
-
71
- $ normalizer = new IndentNormalizer (
72
- $ indent ,
73
- $ this ->prophesize (PrinterInterface::class)->reveal ()
74
- );
75
-
76
- $ this ->expectException (\InvalidArgumentException::class);
77
- $ this ->expectExceptionMessage (\sprintf (
78
- '"%s" is not valid JSON. ' ,
79
- $ json
80
- ));
81
-
82
- $ normalizer ->normalize ($ json );
83
- }
84
-
85
55
public function testNormalizeUsesPrinterToNormalizeJsonWithIndent ()
86
56
{
87
57
$ indent = ' ' ;
Original file line number Diff line number Diff line change 14
14
namespace Localheinz \Json \Normalizer \Test \Unit ;
15
15
16
16
use Localheinz \Json \Normalizer \NoFinalNewLineNormalizer ;
17
- use Localheinz \Json \Normalizer \NormalizerInterface ;
18
- use Localheinz \Test \Util \Helper ;
19
- use PHPUnit \Framework ;
20
17
21
- final class NoFinalNewLineNormalizerTest extends Framework \TestCase
18
+ final class NoFinalNewLineNormalizerTest extends AbstractNormalizerTestCase
22
19
{
23
- use Helper;
24
-
25
- public function testImplementsNormalizerInterface ()
26
- {
27
- $ this ->assertClassImplementsInterface (NormalizerInterface::class, NoFinalNewLineNormalizer::class);
28
- }
29
-
30
- public function testNormalizeRejectsInvalidJson ()
31
- {
32
- $ json = $ this ->faker ()->realText ();
33
-
34
- $ normalizer = new NoFinalNewLineNormalizer ();
35
-
36
- $ this ->expectException (\InvalidArgumentException::class);
37
- $ this ->expectExceptionMessage (\sprintf (
38
- '"%s" is not valid JSON. ' ,
39
- $ json
40
- ));
41
-
42
- $ normalizer ->normalize ($ json );
43
- }
44
-
45
20
/**
46
21
* @dataProvider providerWhitespace
47
22
*
You can’t perform that action at this time.
0 commit comments