Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improved StringRecordId class #14

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Cbor/Types/Record/StringRecordId.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

final readonly class StringRecordId implements RecordInterface
{
/** @var string $recordId */
public string $recordId;

public function __construct(string $recordId)
public function __construct(string|StringRecordId|RecordId $recordId)
{
$this->recordId = $recordId;
$this->recordId = match(true) {
$recordId instanceof StringRecordId => $recordId->toString(),
$recordId instanceof RecordId => $recordId->toString(),
default => $recordId
};
}

public function __toString(): string
Expand All @@ -30,7 +35,7 @@ public function jsonSerialize(): string

public function equals(StringRecordId $recordId): bool
{
return $this->recordId === $recordId->recordId;
return $this->recordId === $recordId->toString();
}

public static function create(string $recordId): StringRecordId
Expand Down
35 changes: 34 additions & 1 deletion tests/core/cbor/StringRecordIdTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

use Beau\CborPHP\exceptions\CborException;
use PHPUnit\Framework\TestCase;
use Surreal\Cbor\CBOR;
use Surreal\Cbor\Types\Record\StringRecordId;

class StringRecordIdTest extends \PHPUnit\Framework\TestCase
class StringRecordIdTest extends TestCase
{
const RECORD_ID_STRING = "record:some-record";

Expand All @@ -27,4 +28,36 @@ public function testEncodeStringRecordId()

$this->assertEquals(self::RECORD_ID_STRING, strval($stringRecordId));
}

public function testInstantiateStringRecordId()
{
$stringRecordId = new StringRecordId(self::RECORD_ID_STRING);
$this->assertEquals(self::RECORD_ID_STRING, $stringRecordId->toString());
}

public function testEquals()
{
$strid1 = new StringRecordId(self::RECORD_ID_STRING);
$strid2 = new StringRecordId(self::RECORD_ID_STRING);

$this->assertTrue($strid1->equals($strid2));

$strid3 = new StringRecordId("record:some-other-record-1");
$this->assertFalse($strid1->equals($strid3));
}

public function testJSONSerialize()
{
$strid = new StringRecordId(self::RECORD_ID_STRING);
$this->assertEquals('"' . self::RECORD_ID_STRING . '"', json_encode($strid));

// reparsing the JSON should give the same string
$this->assertEquals(self::RECORD_ID_STRING, json_decode(json_encode($strid), true));
}

public function testCreate()
{
$strid = StringRecordId::create(self::RECORD_ID_STRING);
$this->assertEquals(self::RECORD_ID_STRING, $strid->toString());
}
}