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

Identical twoWayKey #273

Merged
merged 13 commits into from
May 26, 2023
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
60 changes: 30 additions & 30 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,13 @@ public function createRelationship(
if (\strtolower($attribute->getId()) === \strtolower($id)) {
throw new DuplicateException('Attribute already exists');
}

if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP
&& \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey)
&& $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId()
) {
throw new DuplicateException('Related attribute already exists');
}
}

if (
Expand Down Expand Up @@ -1663,7 +1670,7 @@ public function updateRelationship(
!\is_null($newTwoWayKey)
&& \in_array($newTwoWayKey, \array_map(fn ($attribute) => $attribute['key'], $relatedAttributes))
) {
throw new DuplicateException('Attribute already exists');
throw new DuplicateException('Related attribute already exists');
abnegate marked this conversation as resolved.
Show resolved Hide resolved
}

$type = $attribute['options']['relationType'];
Expand Down
99 changes: 98 additions & 1 deletion tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4733,6 +4733,103 @@ public function testOneToOneTwoWayRelationship(): void
$this->assertEquals(null, $country);
}

public function testIdenticalTwoWayKeyRelationship(): void
{
if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) {
$this->expectNotToPerformAssertions();
return;
}

static::getDatabase()->createCollection('parent');
static::getDatabase()->createCollection('child');

static::getDatabase()->createRelationship(
collection: 'parent',
relatedCollection: 'child',
type: Database::RELATION_ONE_TO_ONE,
id: 'child1'
);

try {
static::getDatabase()->createRelationship(
collection: 'parent',
relatedCollection: 'child',
type: Database::RELATION_ONE_TO_MANY,
id: 'children',
);
$this->fail('Failed to throw Exception');
} catch (Exception $e) {
$this->assertEquals('Related attribute already exists', $e->getMessage());
}

static::getDatabase()->createRelationship(
collection: 'parent',
relatedCollection: 'child',
type: Database::RELATION_ONE_TO_MANY,
id: 'children',
twoWayKey: 'parent_id'
);

$collection = static::getDatabase()->getCollection('parent');
$attributes = $collection->getAttribute('attributes', []);
foreach ($attributes as $attribute) {
if ($attribute['key'] === 'child1') {
$this->assertEquals('parent', $attribute['options']['twoWayKey']);
}

if ($attribute['key'] === 'children') {
$this->assertEquals('parent_id', $attribute['options']['twoWayKey']);
}
}

static::getDatabase()->createDocument('parent', new Document([
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'child1' => [
'$id' => 'foo',
'$permissions' => [Permission::read(Role::any())],
],
'children' => [
[
'$id' => 'bar',
'$permissions' => [Permission::read(Role::any())],
],
],
]));

$documents = static::getDatabase()->find('parent', []);
$document = array_pop($documents);
$this->assertArrayHasKey('child1', $document);
$this->assertEquals('foo', $document->getAttribute('child1')->getId());
$this->assertArrayHasKey('children', $document);
$this->assertEquals('bar', $document->getAttribute('children')[0]->getId());

try {
static::getDatabase()->updateRelationship(
collection: 'parent',
id: 'children',
newKey: 'child1'
);
$this->fail('Failed to throw Exception');
} catch (Exception $e) {
$this->assertEquals('Attribute already exists', $e->getMessage());
}

try {
static::getDatabase()->updateRelationship(
collection: 'parent',
id: 'children',
newTwoWayKey: 'parent'
);
$this->fail('Failed to throw Exception');
} catch (Exception $e) {
$this->assertEquals('Related attribute already exists', $e->getMessage());
}
}

public function testOneToManyOneWayRelationship(): void
{
if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) {
Expand Down Expand Up @@ -10164,7 +10261,7 @@ public function testUpdateRelationshipToExistingKey(): void
static::getDatabase()->updateRelationship('ovens', 'cakes', newTwoWayKey: 'height');
$this->fail('Failed to throw exception');
} catch (DuplicateException $e) {
$this->assertEquals('Attribute already exists', $e->getMessage());
$this->assertEquals('Related attribute already exists', $e->getMessage());
}
}

Expand Down