Skip to content

Commit 8749041

Browse files
authored
Merge pull request #4 from localheinz/feature/abstract-normalizer-test-case
Enhancement: Extract AbstractNormalizerTestCase
2 parents 1f27f45 + 09c3177 commit 8749041

4 files changed

+61
-83
lines changed
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

test/Unit/FinalNewLineNormalizerTest.php

+1-26
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,9 @@
1414
namespace Localheinz\Json\Normalizer\Test\Unit;
1515

1616
use Localheinz\Json\Normalizer\FinalNewLineNormalizer;
17-
use Localheinz\Json\Normalizer\NormalizerInterface;
18-
use Localheinz\Test\Util\Helper;
19-
use PHPUnit\Framework;
2017

21-
final class FinalNewLineNormalizerTest extends Framework\TestCase
18+
final class FinalNewLineNormalizerTest extends AbstractNormalizerTestCase
2219
{
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-
4520
/**
4621
* @dataProvider providerWhitespace
4722
*

test/Unit/IndentNormalizerTest.php

+1-31
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,11 @@
1414
namespace Localheinz\Json\Normalizer\Test\Unit;
1515

1616
use Localheinz\Json\Normalizer\IndentNormalizer;
17-
use Localheinz\Json\Normalizer\NormalizerInterface;
1817
use Localheinz\Json\Printer\PrinterInterface;
19-
use Localheinz\Test\Util\Helper;
20-
use PHPUnit\Framework;
2118
use Prophecy\Argument;
2219

23-
final class IndentNormalizerTest extends Framework\TestCase
20+
final class IndentNormalizerTest extends AbstractNormalizerTestCase
2421
{
25-
use Helper;
26-
27-
public function testImplementsNormalizerInterface()
28-
{
29-
$this->assertClassImplementsInterface(NormalizerInterface::class, IndentNormalizer::class);
30-
}
31-
3222
/**
3323
* @dataProvider providerInvalidIndent
3424
*
@@ -62,26 +52,6 @@ public function providerInvalidIndent(): \Generator
6252
}
6353
}
6454

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-
8555
public function testNormalizeUsesPrinterToNormalizeJsonWithIndent()
8656
{
8757
$indent = ' ';

test/Unit/NoFinalNewLineNormalizerTest.php

+1-26
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,9 @@
1414
namespace Localheinz\Json\Normalizer\Test\Unit;
1515

1616
use Localheinz\Json\Normalizer\NoFinalNewLineNormalizer;
17-
use Localheinz\Json\Normalizer\NormalizerInterface;
18-
use Localheinz\Test\Util\Helper;
19-
use PHPUnit\Framework;
2017

21-
final class NoFinalNewLineNormalizerTest extends Framework\TestCase
18+
final class NoFinalNewLineNormalizerTest extends AbstractNormalizerTestCase
2219
{
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-
4520
/**
4621
* @dataProvider providerWhitespace
4722
*

0 commit comments

Comments
 (0)