PHPunit example
This commit is contained in:
parent
d88b89209b
commit
f61eea9169
2 changed files with 60 additions and 0 deletions
33
src/Email.php
Normal file
33
src/Email.php
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MMK2410\MyTodoList;
|
||||||
|
|
||||||
|
final class Email
|
||||||
|
{
|
||||||
|
private $email;
|
||||||
|
|
||||||
|
private function __construct(string $email)
|
||||||
|
{
|
||||||
|
$this->ensureIsValidEmail($email);
|
||||||
|
$this->email = $email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromString(string $email): self
|
||||||
|
{
|
||||||
|
return new self($email);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return $this->email;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function ensureIsValidEmail(string $email): void
|
||||||
|
{
|
||||||
|
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf("%s is not a valid email address", $email)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
tests/EmailTest.php
Normal file
27
tests/EmailTest.php
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use MMK2410\MyTodoList\Email;
|
||||||
|
|
||||||
|
final class EmailTest extends TestCase
|
||||||
|
{
|
||||||
|
private $email = 'user@example.com';
|
||||||
|
|
||||||
|
public function testCanBeCreatedFromValidEmailAddress(): void {
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
Email::class,
|
||||||
|
Email::fromString($this->email)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCannotBeCreatedFromInvalidEmailAddress(): void {
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
Email::fromString('invalid');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanBeUsedAsString(): void {
|
||||||
|
$this->assertEquals(
|
||||||
|
$this->email,
|
||||||
|
Email::fromString($this->email)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue