Skip to content

Commit

Permalink
Enhancement: Allow to mutate format
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 27, 2018
1 parent 12c6623 commit 1233add
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 12 deletions.
50 changes: 50 additions & 0 deletions src/Format/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,54 @@ public function hasFinalNewLine(): bool
{
return $this->hasFinalNewLine;
}

public function withJsonEncodeOptions(int $jsonEncodeOptions): FormatInterface
{
if (0 > $jsonEncodeOptions) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not valid options for json_encode().',
$jsonEncodeOptions
));
}

$mutated = clone $this;

$mutated->jsonEncodeOptions = $jsonEncodeOptions;

return $mutated;
}

public function withIndent(string $indent): FormatInterface
{
if (1 !== \preg_match('/^[ \t]+$/', $indent)) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not a valid indent.',
$indent
));
}

$mutated = clone $this;

$mutated->indent = $indent;

return $mutated;
}

public function withFinalNewLine(): FormatInterface
{
$mutated = clone $this;

$mutated->hasFinalNewLine = true;

return $mutated;
}

public function withoutFinalNewLine(): FormatInterface
{
$mutated = clone $this;

$mutated->hasFinalNewLine = false;

return $mutated;
}
}
22 changes: 22 additions & 0 deletions src/Format/FormatInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,26 @@ public function jsonEncodeOptions(): int;
public function indent(): string;

public function hasFinalNewLine(): bool;

/**
* @param string $indent
*
* @throws \InvalidArgumentException
*
* @return FormatInterface
*/
public function withIndent(string $indent): self;

/**
* @param int $jsonEncodeOptions
*
* @throws \InvalidArgumentException
*
* @return FormatInterface
*/
public function withJsonEncodeOptions(int $jsonEncodeOptions): self;

public function withFinalNewLine(): self;

public function withoutFinalNewLine(): self;
}
153 changes: 141 additions & 12 deletions test/Unit/Format/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,152 @@ public function testConstructorSetsValues(string $indent, bool $hasFinalNewLine)

public function providerJsonIndentAndFinalNewLine(): \Generator
{
$indents = [
' ',
"\t",
];

$hasFinalNewLines = [
true,
false,
];

foreach ($indents as $indent) {
foreach ($hasFinalNewLines as $hasFinalNewLine) {
foreach ($this->indents() as $indent) {
foreach ($this->hasFinalNewLines() as $hasFinalNewLine) {
yield [
$indent,
$hasFinalNewLine,
];
}
}
}

public function testWithJsonEncodeOptionsRejectsInvalidJsonEncodeOptions(): void
{
$jsonEncodeOptions = -1;

$format = new Format(
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
' ',
true
);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf(
'"%s" is not valid options for json_encode().',
$jsonEncodeOptions
));

$format->withJsonEncodeOptions($jsonEncodeOptions);
}

public function testWithIndentClonesFormatAndSetsJsonEncodeOptions(): void
{
$format = new Format(
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
' ',
true
);

$jsonEncodeOptions = 9000;

$mutated = $format->withJsonEncodeOptions($jsonEncodeOptions);

$this->assertInstanceOf(FormatInterface::class, $mutated);
$this->assertNotSame($format, $mutated);
$this->assertSame($jsonEncodeOptions, $mutated->jsonEncodeOptions());
}

/**
* @dataProvider providerInvalidIndent
*
* @param string $indent
*/
public function testWithIndentRejectsInvalidIndent(string $indent): void
{
$format = new Format(
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
' ',
true
);

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

$format->withIndent($indent);
}

/**
* @dataProvider providerIndent
*
* @param string $indent
*/
public function testWithIndentClonesFormatAndSetsIndent(string $indent): void
{
$format = new Format(
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
' ',
true
);

$mutated = $format->withIndent($indent);

$this->assertInstanceOf(FormatInterface::class, $mutated);
$this->assertNotSame($format, $mutated);
$this->assertSame($indent, $mutated->indent());
}

public function providerIndent(): \Generator
{
foreach ($this->indents() as $key => $indent) {
yield $key => [
$indent,
];
}
}

public function testWithFinalNewLineClonesFormatAndSetsFinalNewLine(): void
{
$format = new Format(
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
' ',
false
);

$mutated = $format->withFinalNewLine();

$this->assertInstanceOf(FormatInterface::class, $mutated);
$this->assertNotSame($format, $mutated);
$this->assertTrue($mutated->hasFinalNewLine());
}

public function testWithoutFinalNewLineClonesFormatAndSetsFinalNewLine(): void
{
$format = new Format(
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
' ',
true
);

$mutated = $format->withoutFinalNewLine();

$this->assertInstanceOf(FormatInterface::class, $mutated);
$this->assertNotSame($format, $mutated);
$this->assertFalse($mutated->hasFinalNewLine());
}

/**
* @return string[]
*/
private function indents(): array
{
return [
'space' => ' ',
'tab' => "\t",
];
}

/**
* @return bool[]
*/
private function hasFinalNewLines(): array
{
return [
'yes' => true,
'no' => false,
];
}
}

0 comments on commit 1233add

Please sign in to comment.