Skip to content

Commit

Permalink
Enhancement: Extract AbstractNormalizerTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 12, 2018
1 parent a5c43f4 commit fff9ad3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 83 deletions.
58 changes: 58 additions & 0 deletions test/Unit/AbstractNormalizerTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/localheinz/json-normalizer
*/

namespace Localheinz\Json\Normalizer\Test\Unit;

use Localheinz\Json\Normalizer\NormalizerInterface;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;

abstract class AbstractNormalizerTestCase extends Framework\TestCase
{
use Helper;

final public function testImplementsNormalizerInterface()
{
$this->assertClassImplementsInterface(NormalizerInterface::class, $this->className());
}

final public function testNormalizeRejectsInvalidJson()
{
$json = $this->faker()->realText();

$reflection = new \ReflectionClass($this->className());

$normalizer = $reflection->newInstanceWithoutConstructor();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf(
'"%s" is not valid JSON.',
$json
));

$normalizer->normalize($json);
}

final protected function className(): string
{
return \preg_replace(
'/Test$/',
'',
\str_replace(
'Localheinz\\Json\\Normalizer\\Test\\Unit\\',
'Localheinz\\Json\\Normalizer\\',
static::class
)
);
}
}
27 changes: 1 addition & 26 deletions test/Unit/FinalNewLineNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,9 @@
namespace Localheinz\Json\Normalizer\Test\Unit;

use Localheinz\Json\Normalizer\FinalNewLineNormalizer;
use Localheinz\Json\Normalizer\NormalizerInterface;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;

final class FinalNewLineNormalizerTest extends Framework\TestCase
final class FinalNewLineNormalizerTest extends AbstractNormalizerTestCase
{
use Helper;

public function testImplementsNormalizerInterface()
{
$this->assertClassImplementsInterface(NormalizerInterface::class, FinalNewLineNormalizer::class);
}

public function testNormalizeRejectsInvalidJson()
{
$json = $this->faker()->realText();

$normalizer = new FinalNewLineNormalizer();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf(
'"%s" is not valid JSON.',
$json
));

$normalizer->normalize($json);
}

/**
* @dataProvider providerWhitespace
*
Expand Down
32 changes: 1 addition & 31 deletions test/Unit/IndentNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,11 @@
namespace Localheinz\Json\Normalizer\Test\Unit;

use Localheinz\Json\Normalizer\IndentNormalizer;
use Localheinz\Json\Normalizer\NormalizerInterface;
use Localheinz\Json\Printer\PrinterInterface;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;
use Prophecy\Argument;

final class IndentNormalizerTest extends Framework\TestCase
final class IndentNormalizerTest extends AbstractNormalizerTestCase
{
use Helper;

public function testImplementsNormalizerInterface()
{
$this->assertClassImplementsInterface(NormalizerInterface::class, IndentNormalizer::class);
}

/**
* @dataProvider providerInvalidIndent
*
Expand Down Expand Up @@ -62,26 +52,6 @@ public function providerInvalidIndent(): \Generator
}
}

public function testNormalizeRejectsInvalidJson()
{
$indent = ' ';

$json = $this->faker()->realText();

$normalizer = new IndentNormalizer(
$indent,
$this->prophesize(PrinterInterface::class)->reveal()
);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf(
'"%s" is not valid JSON.',
$json
));

$normalizer->normalize($json);
}

public function testNormalizeUsesPrinterToNormalizeJsonWithIndent()
{
$indent = ' ';
Expand Down
27 changes: 1 addition & 26 deletions test/Unit/NoFinalNewLineNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,9 @@
namespace Localheinz\Json\Normalizer\Test\Unit;

use Localheinz\Json\Normalizer\NoFinalNewLineNormalizer;
use Localheinz\Json\Normalizer\NormalizerInterface;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;

final class NoFinalNewLineNormalizerTest extends Framework\TestCase
final class NoFinalNewLineNormalizerTest extends AbstractNormalizerTestCase
{
use Helper;

public function testImplementsNormalizerInterface()
{
$this->assertClassImplementsInterface(NormalizerInterface::class, NoFinalNewLineNormalizer::class);
}

public function testNormalizeRejectsInvalidJson()
{
$json = $this->faker()->realText();

$normalizer = new NoFinalNewLineNormalizer();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf(
'"%s" is not valid JSON.',
$json
));

$normalizer->normalize($json);
}

/**
* @dataProvider providerWhitespace
*
Expand Down

0 comments on commit fff9ad3

Please sign in to comment.