Skip to content

Commit

Permalink
Merge pull request #39 from PcComponentes/feature/add_unique_id_value…
Browse files Browse the repository at this point in the history
…_object

Add unique id value object
  • Loading branch information
calmohallag authored Oct 26, 2022
2 parents 46a57c2 + 18c74d7 commit 9120ee8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Domain/Model/ValueObject/UniqueId.php
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)),
);
}
}
56 changes: 56 additions & 0 deletions tests/Domain/Model/ValueObject/UniqueIdTest.php
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));
}
}

0 comments on commit 9120ee8

Please sign in to comment.