From f9fbb80b2adab4a89cc858db421efa4602fa6166 Mon Sep 17 00:00:00 2001 From: prolic Date: Tue, 20 Nov 2018 01:30:51 +0800 Subject: [PATCH] use Guid Codec for UUIDs --- src/EventId.php | 8 +-- src/Util/UuidGenerator.php | 37 ++++++++++-- tests/UserManagement/TestWithUser.php | 4 +- tests/UserManagement/creating_a_user.php | 4 +- tests/UserManagement/deleting_a_user.php | 8 +-- tests/UserManagement/updating_a_user.php | 6 +- ...in_autoack_mode_drops_the_subscription.php | 4 +- tests/append_to_stream.php | 56 +++++++++++++++++++ tests/subscribe_to_all_catching_up_should.php | 1 - 9 files changed, 106 insertions(+), 22 deletions(-) diff --git a/src/EventId.php b/src/EventId.php index b9a80a5e..33643934 100644 --- a/src/EventId.php +++ b/src/EventId.php @@ -13,7 +13,7 @@ namespace Prooph\EventStoreClient; -use Ramsey\Uuid\Uuid; +use Prooph\EventStoreClient\Util\UuidGenerator; use Ramsey\Uuid\UuidInterface; class EventId @@ -23,17 +23,17 @@ class EventId public static function generate(): EventId { - return new self(Uuid::uuid4()); + return new self(UuidGenerator::generate()); } public static function fromString(string $eventId): EventId { - return new self(Uuid::fromString($eventId)); + return new self(UuidGenerator::fromString($eventId)); } public static function fromBinary(string $bytes): EventId { - return new self(Uuid::fromBytes($bytes)); + return new self(UuidGenerator::fromBytes($bytes)); } private function __construct(UuidInterface $eventId) diff --git a/src/Util/UuidGenerator.php b/src/Util/UuidGenerator.php index 2bc72f20..a1a53f8b 100644 --- a/src/Util/UuidGenerator.php +++ b/src/Util/UuidGenerator.php @@ -13,18 +13,38 @@ namespace Prooph\EventStoreClient\Util; -use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\FeatureSet; +use Ramsey\Uuid\UuidFactory; +use Ramsey\Uuid\UuidInterface; class UuidGenerator { - public static function generate(): string + /** @var UuidFactory */ + private static $factory; + + public static function generate(): UuidInterface + { + return self::factory()->uuid4(); + } + + public static function generateString(): string { - return Uuid::uuid4()->toString(); + return self::generate()->toString(); } public static function generateWithoutDash(): string { - return \str_replace('-', '', self::generate()); + return \str_replace('-', '', self::generateString()); + } + + public static function fromString(string $uuid): UuidInterface + { + return self::factory()->fromString($uuid); + } + + public static function fromBytes(string $bytes): UuidInterface + { + return self::factory()->fromBytes($bytes); } public static function empty(): string @@ -35,4 +55,13 @@ public static function empty(): string final private function __construct() { } + + private static function factory(): UuidFactory + { + if (null === self::$factory) { + self::$factory = new UuidFactory(new FeatureSet(true)); + } + + return self::$factory; + } } diff --git a/tests/UserManagement/TestWithUser.php b/tests/UserManagement/TestWithUser.php index b4d685d7..d0018986 100644 --- a/tests/UserManagement/TestWithUser.php +++ b/tests/UserManagement/TestWithUser.php @@ -13,8 +13,8 @@ namespace ProophTest\EventStoreClient\UserManagement; +use Prooph\EventStoreClient\Util\UuidGenerator; use ProophTest\EventStoreClient\DefaultData; -use Ramsey\Uuid\Uuid; abstract class TestWithUser extends TestWithNode { @@ -25,7 +25,7 @@ protected function setUp(): void { parent::setUp(); - $this->username = Uuid::uuid4()->toString(); + $this->username = UuidGenerator::generateString(); $this->manager->createUser( $this->username, diff --git a/tests/UserManagement/creating_a_user.php b/tests/UserManagement/creating_a_user.php index 6e7363b9..6e9cda51 100644 --- a/tests/UserManagement/creating_a_user.php +++ b/tests/UserManagement/creating_a_user.php @@ -15,8 +15,8 @@ use Exception; use Prooph\EventStoreClient\Exception\InvalidArgumentException; +use Prooph\EventStoreClient\Util\UuidGenerator; use ProophTest\EventStoreClient\DefaultData; -use Ramsey\Uuid\Uuid; class creating_a_user extends TestWithNode { @@ -70,7 +70,7 @@ public function fetching_user_with_empty_name_throws(): void /** @test */ public function creating_a_user_with_parameters_can_be_read(): void { - $login = Uuid::uuid4()->toString(); + $login = UuidGenerator::generateString(); $this->manager->createUser( $login, diff --git a/tests/UserManagement/deleting_a_user.php b/tests/UserManagement/deleting_a_user.php index 87303bca..90798a3e 100644 --- a/tests/UserManagement/deleting_a_user.php +++ b/tests/UserManagement/deleting_a_user.php @@ -17,8 +17,8 @@ use Prooph\EventStoreClient\Exception\UserCommandFailedException; use Prooph\EventStoreClient\Transport\Http\HttpStatusCode; use Prooph\EventStoreClient\UserManagement\UserDetails; +use Prooph\EventStoreClient\Util\UuidGenerator; use ProophTest\EventStoreClient\DefaultData; -use Ramsey\Uuid\Uuid; class deleting_a_user extends TestWithNode { @@ -28,7 +28,7 @@ public function deleting_non_existing_user_throws(): void $this->expectException(UserCommandFailedException::class); try { - $this->manager->deleteUser(Uuid::uuid4()->toString(), DefaultData::adminCredentials()); + $this->manager->deleteUser(UuidGenerator::generateString(), DefaultData::adminCredentials()); } catch (UserCommandFailedException $e) { $this->assertSame(HttpStatusCode::NOT_FOUND, $e->httpStatusCode()); @@ -42,7 +42,7 @@ public function deleting_non_existing_user_throws(): void */ public function deleting_created_user_deletes_it(): void { - $user = Uuid::uuid4()->toString(); + $user = UuidGenerator::generateString(); $this->manager->createUser($user, 'ourofull', ['foo', 'bar'], 'ouro', DefaultData::adminCredentials()); $this->manager->deleteUser($user, DefaultData::adminCredentials()); @@ -59,7 +59,7 @@ public function deleting_empty_user_throws(): void /** @test */ public function can_delete_a_user(): void { - $name = Uuid::uuid4()->toString(); + $name = UuidGenerator::generateString(); $this->manager->createUser( $name, diff --git a/tests/UserManagement/updating_a_user.php b/tests/UserManagement/updating_a_user.php index d53f36c2..dc6a180e 100644 --- a/tests/UserManagement/updating_a_user.php +++ b/tests/UserManagement/updating_a_user.php @@ -15,8 +15,8 @@ use Prooph\EventStoreClient\Exception\InvalidArgumentException; use Prooph\EventStoreClient\Exception\UserCommandFailedException; +use Prooph\EventStoreClient\Util\UuidGenerator; use ProophTest\EventStoreClient\DefaultData; -use Ramsey\Uuid\Uuid; class updating_a_user extends TestWithNode { @@ -41,13 +41,13 @@ public function updating_non_existing_user_throws(): void { $this->expectException(UserCommandFailedException::class); - $this->manager->updateUser(Uuid::uuid4()->toString(), 'bar', ['foo'], DefaultData::adminCredentials()); + $this->manager->updateUser(UuidGenerator::generateString(), 'bar', ['foo'], DefaultData::adminCredentials()); } /** @test */ public function updating_a_user_with_parameters_can_be_read(): void { - $name = Uuid::uuid4()->toString(); + $name = UuidGenerator::generateString(); $this->manager->createUser($name, 'ourofull', ['foo', 'bar'], 'password', DefaultData::adminCredentials()); diff --git a/tests/a_nak_in_subscription_handler_in_autoack_mode_drops_the_subscription.php b/tests/a_nak_in_subscription_handler_in_autoack_mode_drops_the_subscription.php index fecbd790..4ae3ba13 100644 --- a/tests/a_nak_in_subscription_handler_in_autoack_mode_drops_the_subscription.php +++ b/tests/a_nak_in_subscription_handler_in_autoack_mode_drops_the_subscription.php @@ -29,7 +29,7 @@ use Prooph\EventStoreClient\PersistentSubscriptionDropped; use Prooph\EventStoreClient\PersistentSubscriptionSettings; use Prooph\EventStoreClient\SubscriptionDropReason; -use Ramsey\Uuid\Uuid; +use Prooph\EventStoreClient\Util\UuidGenerator; use Throwable; class a_nak_in_subscription_handler_in_autoack_mode_drops_the_subscription extends TestCase @@ -51,7 +51,7 @@ class a_nak_in_subscription_handler_in_autoack_mode_drops_the_subscription exten protected function setUp() { - $this->stream = '$' . Uuid::uuid4()->toString(); + $this->stream = '$' . UuidGenerator::generateString(); $this->settings = PersistentSubscriptionSettings::create() ->doNotResolveLinkTos() ->startFromBeginning() diff --git a/tests/append_to_stream.php b/tests/append_to_stream.php index 9444f423..d000112a 100644 --- a/tests/append_to_stream.php +++ b/tests/append_to_stream.php @@ -13,6 +13,9 @@ namespace ProophTest\EventStoreClient; +use Amp\Artax\DefaultClient; +use Amp\Artax\Request; +use Amp\Artax\Response; use PHPUnit\Framework\TestCase; use Prooph\EventStoreClient\ConditionalWriteResult; use Prooph\EventStoreClient\ConditionalWriteStatus; @@ -23,6 +26,7 @@ use Prooph\EventStoreClient\ReadDirection; use Prooph\EventStoreClient\StreamEventsSlice; use Prooph\EventStoreClient\StreamMetadata; +use Prooph\EventStoreClient\Util\Json; use Prooph\EventStoreClient\WriteResult; use ProophTest\EventStoreClient\Helper\TestConnection; use ProophTest\EventStoreClient\Helper\TestEvent; @@ -657,4 +661,56 @@ public function returns_failure_status_when_conditionally_appending_to_a_deleted $connection->close(); })); } + + /** + * @test + * @throws Throwable + */ + public function writes_predefined_event_id(): void + { + wait(call(function () { + $stream = 'writes_predefined_event_id'; + + $connection = TestConnection::createAsync(); + + yield $connection->connectAsync(); + + $event = TestEvent::newTestEvent(); + + yield $connection->appendToStreamAsync($stream, ExpectedVersion::ANY, [$event]); + + $events = yield $connection->readStreamEventsBackwardAsync($stream, -1, 1); + \assert($events instanceof StreamEventsSlice); + + $readEvent = $events->events()[0]->event(); + + $connection->close(); + + $this->assertEquals($event->eventId()->toString(), $readEvent->eventId()->toString()); + + $url = \sprintf( + 'http://%s:%s/streams/%s/head/backward/1?embed=body', + \getenv('ES_HOST'), + \getenv('ES_HTTP_PORT'), + $stream + ); + + $request = new Request($url, 'GET'); + $request = $request->withAddedHeader('Accept', 'application/vnd.eventstore.atom+json'); + + $client = new DefaultClient(); + + $response = yield $client->request($request); + + \assert($response instanceof Response); + + $body = yield $response->getBody()->read(); + + $json = Json::decode($body); + + $eventId = $json['entries'][0]['eventId']; + + $this->assertEquals($event->eventId()->toString(), $eventId); + })); + } } diff --git a/tests/subscribe_to_all_catching_up_should.php b/tests/subscribe_to_all_catching_up_should.php index e20e1e95..f99d1892 100644 --- a/tests/subscribe_to_all_catching_up_should.php +++ b/tests/subscribe_to_all_catching_up_should.php @@ -429,7 +429,6 @@ public function __invoke( ResolvedEvent $resolvedEvent ): Promise { if (! SystemStreams::isSystemStream($resolvedEvent->originalEvent()->eventStreamId())) { - //\var_dump($resolvedEvent->originalEvent()->eventStreamId()); $this->events[] = $resolvedEvent; $this->appeared->signal(); }