Skip to content

Commit

Permalink
use Guid Codec for UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Nov 19, 2018
1 parent ef81d00 commit f9fbb80
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/EventId.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Prooph\EventStoreClient;

use Ramsey\Uuid\Uuid;
use Prooph\EventStoreClient\Util\UuidGenerator;
use Ramsey\Uuid\UuidInterface;

class EventId
Expand All @@ -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)
Expand Down
37 changes: 33 additions & 4 deletions src/Util/UuidGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions tests/UserManagement/TestWithUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -25,7 +25,7 @@ protected function setUp(): void
{
parent::setUp();

$this->username = Uuid::uuid4()->toString();
$this->username = UuidGenerator::generateString();

$this->manager->createUser(
$this->username,
Expand Down
4 changes: 2 additions & 2 deletions tests/UserManagement/creating_a_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tests/UserManagement/deleting_a_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());

Expand All @@ -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());
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions tests/UserManagement/updating_a_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
56 changes: 56 additions & 0 deletions tests/append_to_stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}));
}
}
1 change: 0 additions & 1 deletion tests/subscribe_to_all_catching_up_should.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit f9fbb80

Please sign in to comment.