-
Notifications
You must be signed in to change notification settings - Fork 12
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 #39 from PcComponentes/feature/add_unique_id_value…
…_object Add unique id value object
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PcComponentes\Ddd\Domain\Model\ValueObject; | ||
|
||
class UniqueId extends StringValueObject | ||
{ | ||
public static function create(): static | ||
{ | ||
return self::from( | ||
\strtoupper(\base_convert(\uniqid(), 16, 36)), | ||
); | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PcComponentes\Ddd\Tests\Domain\Model\ValueObject; | ||
|
||
use PcComponentes\Ddd\Domain\Model\ValueObject\UniqueId; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class UniqueIdTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function given_uid_value_when_ask_to_get_info_then_return_expected_info() | ||
{ | ||
$value = 'H7HVQ2U72X'; | ||
$str = UniqueId::from($value); | ||
|
||
$this->assertEquals($value, $str->value()); | ||
$this->assertEquals($value, $str->jsonSerialize()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function given_uid_class_when_ask_to_generate_an_uid_then_return_uid_instance() | ||
{ | ||
$uid = UniqueId::create(); | ||
$this->assertInstanceOf(UniqueId::class, $uid); | ||
$this->assertMatchesRegularExpression('/^[0-9A-Z]{10}$/i', $uid->value()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function given_two_identical_uids_when_ask_to_check_equality_then_return_true() | ||
{ | ||
$value = 'H7HVQ2U72X'; | ||
$str = UniqueId::from($value); | ||
$other = UniqueId::from($value); | ||
|
||
$this->assertTrue($str->equalTo($other)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function given_two_different_uids_when_ask_to_check_equality_then_return_false() | ||
{ | ||
$str = UniqueId::from('H7HVQ2U72X'); | ||
$other = UniqueId::from('H7INVWHVPR'); | ||
|
||
$this->assertFalse($str->equalTo($other)); | ||
} | ||
} |