-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from madewithlove/2.x
2.x
- Loading branch information
Showing
12 changed files
with
194 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Madewithlove; | ||
|
||
final class ServerVariable | ||
{ | ||
public const HTTP_REFERER = 'HTTP_REFERER'; | ||
public const SERVER_NAME = 'SERVER_NAME'; | ||
|
||
public const ALL = [ | ||
self::HTTP_REFERER, | ||
self::SERVER_NAME, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Madewithlove; | ||
|
||
use InvalidArgumentException; | ||
|
||
class ServerVariables | ||
{ | ||
private function __construct( | ||
private array $variables | ||
) { | ||
} | ||
|
||
public static function default(): self | ||
{ | ||
return new self([]); | ||
} | ||
|
||
public function with(string $optionName, string $value): self | ||
{ | ||
if (!in_array($optionName, ServerVariable::ALL)) { | ||
throw new InvalidArgumentException('Unsupported server variable: ' . $optionName); | ||
} | ||
|
||
$clone = clone $this; | ||
$clone->variables[$optionName] = $value; | ||
|
||
return $clone; | ||
} | ||
|
||
public function has(string $optionName): bool | ||
{ | ||
return isset($this->variables[$optionName]); | ||
} | ||
|
||
public function get(string $optionName): string | ||
{ | ||
return $this->variables[$optionName] ?? ''; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->variables; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Madewithlove; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class ResultLineTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it correctly returns all properties(): void | ||
{ | ||
$resultLine = new ResultLine('foo', 'bar', true, true, true, true); | ||
$this->assertEquals('foo', $resultLine->getLine()); | ||
$this->assertEquals('bar', $resultLine->getMessage()); | ||
$this->assertTrue($resultLine->isMet()); | ||
$this->assertTrue($resultLine->isValid()); | ||
$this->assertTrue($resultLine->isSupported()); | ||
$this->assertTrue($resultLine->wasReached()); | ||
|
||
$resultLine = new ResultLine('foo', 'bar', false, true, true, true); | ||
$this->assertFalse($resultLine->isMet()); | ||
|
||
$resultLine = new ResultLine('foo', 'bar', true, false, true, true); | ||
$this->assertFalse($resultLine->isValid()); | ||
|
||
$resultLine = new ResultLine('foo', 'bar', true, true, false, true); | ||
$this->assertFalse($resultLine->wasReached()); | ||
|
||
$resultLine = new ResultLine('foo', 'bar', true, true, true, false); | ||
$this->assertFalse($resultLine->isSupported()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Madewithlove; | ||
|
||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ServerVariablesTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it only allows supported variables(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Unsupported server variable: foo'); | ||
ServerVariables::default()->with('foo', 'bar'); | ||
} | ||
|
||
/** @test */ | ||
public function it holds supported server variables(): void | ||
{ | ||
$serverVariables = ServerVariables::default() | ||
->with(ServerVariable::HTTP_REFERER, 'example.com'); | ||
|
||
$this->assertTrue($serverVariables->has(ServerVariable::HTTP_REFERER)); | ||
$this->assertEquals('example.com', $serverVariables->get(ServerVariable::HTTP_REFERER)); | ||
|
||
$this->assertFalse($serverVariables->has(ServerVariable::SERVER_NAME)); | ||
$this->assertEquals('', $serverVariables->get(ServerVariable::SERVER_NAME)); | ||
} | ||
|
||
/** @test */ | ||
public function it is immutable(): void | ||
{ | ||
$original = ServerVariables::default(); | ||
$clone = $original->with(ServerVariable::SERVER_NAME, 'example.com'); | ||
|
||
$this->assertNotSame($original, $clone); | ||
$this->assertFalse($original->has(ServerVariable::SERVER_NAME)); | ||
} | ||
} |