-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
…e valid e-mail address
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace ThePay\ApiClient\ValueObject; | ||
|
||
use Egulias\EmailValidator\EmailValidator; | ||
use Egulias\EmailValidator\Validation\RFCValidation; | ||
|
||
class EmailAddress extends NonEmptyString | ||
{ | ||
public static function filter($value) | ||
{ | ||
$nonEmptyString = parent::filter($value); | ||
|
||
if ((new EmailValidator())->isValid($nonEmptyString, new RFCValidation()) === false) { | ||
throw self::invalidValue('e-mail address', $nonEmptyString); | ||
} | ||
|
||
return $nonEmptyString; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace ThePay\ApiClient\ValueObject; | ||
|
||
class NonEmptyString extends StringValue | ||
{ | ||
protected static function filter($value) | ||
{ | ||
$string = parent::filter($value); | ||
|
||
if (trim($string) === '') { | ||
throw self::invalidValue('non-empty string', $string); | ||
} | ||
|
||
return $string; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ThePay\ApiClient\Tests\ValueObject; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ThePay\ApiClient\ValueObject\BaseValueObject; | ||
|
||
abstract class BaseValueObjectTestCase extends TestCase | ||
{ | ||
/** | ||
* @dataProvider validValuesDataProvider | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function testCreatesWorkingInstanceWithValidValue($value): void | ||
{ | ||
$className = static::getClassName(); | ||
$a = $className::create($value); | ||
$b = new $className($value); | ||
|
||
self::assertTrue($a->equals($b)); | ||
self::assertTrue($b->equals($a)); | ||
self::assertSame($value, $a->getValue()); | ||
self::assertSame((string) $value, (string) $a); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidValuesAndMessagesDataProvider | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function testThrowsWithInvalidValue($value, string $message): void | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage($message); | ||
|
||
$className = static::getClassName(); | ||
$className::create($value); | ||
} | ||
|
||
/** | ||
* @return class-string<BaseValueObject> | ||
*/ | ||
abstract protected static function getClassName(): string; | ||
Check failure on line 46 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.1
Check failure on line 46 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.2 psr/http-message 2.0
Check failure on line 46 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.2 psr/http-message 1.0
Check failure on line 46 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.3
|
||
|
||
/** | ||
* @return array<array<mixed>>|array<string, array<mixed> | ||
*/ | ||
abstract public static function validValuesDataProvider(): array; | ||
Check failure on line 51 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.1
Check failure on line 51 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.1
Check failure on line 51 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.2 psr/http-message 2.0
Check failure on line 51 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.2 psr/http-message 2.0
Check failure on line 51 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.2 psr/http-message 1.0
Check failure on line 51 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.2 psr/http-message 1.0
Check failure on line 51 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.3
Check failure on line 51 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.3
|
||
|
||
/** | ||
* @return array<array<mixed|string>>|array<string, array<mixed|string> | ||
*/ | ||
abstract public static function invalidValuesAndMessagesDataProvider(): array; | ||
Check failure on line 56 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.1
Check failure on line 56 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.1
Check failure on line 56 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.2 psr/http-message 2.0
Check failure on line 56 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.2 psr/http-message 2.0
Check failure on line 56 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.2 psr/http-message 1.0
Check failure on line 56 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.2 psr/http-message 1.0
Check failure on line 56 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.3
Check failure on line 56 in tests/ValueObject/BaseValueObjectTestCase.php GitHub Actions / php 8.3
|
||
} |