From 9796d267c3953cb7e688b13bb50414102e723f16 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 09:06:22 +0300 Subject: [PATCH 01/13] Identical twoWayKey --- composer.lock | 60 ++++++++++++------------- phpunit.xml | 2 +- src/Database/Database.php | 5 +++ tests/Database/Base.php | 94 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 31 deletions(-) diff --git a/composer.lock b/composer.lock index c833faf44..ff525b0a8 100644 --- a/composer.lock +++ b/composer.lock @@ -512,16 +512,16 @@ }, { "name": "fakerphp/faker", - "version": "v1.21.0", + "version": "v1.22.0", "source": { "type": "git", "url": "https://github.com/FakerPHP/Faker.git", - "reference": "92efad6a967f0b79c499705c69b662f738cc9e4d" + "reference": "f85772abd508bd04e20bb4b1bbe260a68d0066d2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/FakerPHP/Faker/zipball/92efad6a967f0b79c499705c69b662f738cc9e4d", - "reference": "92efad6a967f0b79c499705c69b662f738cc9e4d", + "url": "https://api.github.com/repos/FakerPHP/Faker/zipball/f85772abd508bd04e20bb4b1bbe260a68d0066d2", + "reference": "f85772abd508bd04e20bb4b1bbe260a68d0066d2", "shasum": "" }, "require": { @@ -574,9 +574,9 @@ ], "support": { "issues": "https://github.com/FakerPHP/Faker/issues", - "source": "https://github.com/FakerPHP/Faker/tree/v1.21.0" + "source": "https://github.com/FakerPHP/Faker/tree/v1.22.0" }, - "time": "2022-12-13T13:54:32+00:00" + "time": "2023-05-14T12:31:37+00:00" }, { "name": "laravel/pint", @@ -705,16 +705,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.15.4", + "version": "v4.15.5", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "6bb5176bc4af8bcb7d926f88718db9b96a2d4290" + "reference": "11e2663a5bc9db5d714eedb4277ee300403b4a9e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/6bb5176bc4af8bcb7d926f88718db9b96a2d4290", - "reference": "6bb5176bc4af8bcb7d926f88718db9b96a2d4290", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/11e2663a5bc9db5d714eedb4277ee300403b4a9e", + "reference": "11e2663a5bc9db5d714eedb4277ee300403b4a9e", "shasum": "" }, "require": { @@ -755,9 +755,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.15.4" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.15.5" }, - "time": "2023-03-05T19:49:14+00:00" + "time": "2023-05-19T20:20:00+00:00" }, { "name": "pcov/clobber", @@ -906,16 +906,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.14", + "version": "1.10.15", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "d232901b09e67538e5c86a724be841bea5768a7c" + "reference": "762c4dac4da6f8756eebb80e528c3a47855da9bd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/d232901b09e67538e5c86a724be841bea5768a7c", - "reference": "d232901b09e67538e5c86a724be841bea5768a7c", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/762c4dac4da6f8756eebb80e528c3a47855da9bd", + "reference": "762c4dac4da6f8756eebb80e528c3a47855da9bd", "shasum": "" }, "require": { @@ -964,7 +964,7 @@ "type": "tidelift" } ], - "time": "2023-04-19T13:47:27+00:00" + "time": "2023-05-09T15:28:01+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1286,16 +1286,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.7", + "version": "9.6.8", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "c993f0d3b0489ffc42ee2fe0bd645af1538a63b2" + "reference": "17d621b3aff84d0c8b62539e269e87d8d5baa76e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c993f0d3b0489ffc42ee2fe0bd645af1538a63b2", - "reference": "c993f0d3b0489ffc42ee2fe0bd645af1538a63b2", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/17d621b3aff84d0c8b62539e269e87d8d5baa76e", + "reference": "17d621b3aff84d0c8b62539e269e87d8d5baa76e", "shasum": "" }, "require": { @@ -1369,7 +1369,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.7" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.8" }, "funding": [ { @@ -1385,7 +1385,7 @@ "type": "tidelift" } ], - "time": "2023-04-14T08:58:40+00:00" + "time": "2023-05-11T05:14:45+00:00" }, { "name": "psr/container", @@ -1786,16 +1786,16 @@ }, { "name": "sebastian/diff", - "version": "4.0.4", + "version": "4.0.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "3461e3fccc7cfdfc2720be910d3bd73c69be590d" + "reference": "74be17022044ebaaecfdf0c5cd504fc9cd5a7131" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/3461e3fccc7cfdfc2720be910d3bd73c69be590d", - "reference": "3461e3fccc7cfdfc2720be910d3bd73c69be590d", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/74be17022044ebaaecfdf0c5cd504fc9cd5a7131", + "reference": "74be17022044ebaaecfdf0c5cd504fc9cd5a7131", "shasum": "" }, "require": { @@ -1840,7 +1840,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/diff/issues", - "source": "https://github.com/sebastianbergmann/diff/tree/4.0.4" + "source": "https://github.com/sebastianbergmann/diff/tree/4.0.5" }, "funding": [ { @@ -1848,7 +1848,7 @@ "type": "github" } ], - "time": "2020-10-26T13:10:38+00:00" + "time": "2023-05-07T05:35:17+00:00" }, { "name": "sebastian/environment", @@ -2673,5 +2673,5 @@ "php": ">=8.0" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } diff --git a/phpunit.xml b/phpunit.xml index 31b947dd6..3833748e0 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false"> + stopOnFailure="true"> ./tests/ diff --git a/src/Database/Database.php b/src/Database/Database.php index 986f263d5..ff34c929f 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1474,6 +1474,11 @@ 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)){ + throw new DuplicateException('TwoWayKey already exists'); + } } if ( diff --git a/tests/Database/Base.php b/tests/Database/Base.php index edbaf7d84..cec910eb4 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -3738,6 +3738,100 @@ public function testWritePermissions(): void } // Relationships + + // Relationships + public function testDoubleOneToOneOneWayRelationship(): void + { + if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + $this->expectNotToPerformAssertions(); + return; + } + + static::getDatabase()->createCollection('parent'); + static::getDatabase()->createCollection('son'); + + static::getDatabase()->createRelationship( + collection: 'parent', + relatedCollection: 'son', + type: Database::RELATION_ONE_TO_ONE, + id: 'son1' + ); + + try { + static::getDatabase()->createRelationship( + collection: 'parent', + relatedCollection: 'son', + type: Database::RELATION_ONE_TO_MANY, + id: 'sons', + ); + $this->fail('Failed to throw Exception'); + } catch (Exception $e) { + $this->assertEquals('TwoWayKey already exists', $e->getMessage()); + } + + static::getDatabase()->createRelationship( + collection: 'parent', + relatedCollection: 'son', + type: Database::RELATION_ONE_TO_MANY, + id: 'sons', + twoWayKey: 'parent_id' + ); + + $collection = static::getDatabase()->getCollection('parent'); + + $attributes = $collection->getAttribute('attributes', []); + + foreach ($attributes as $attribute) { + if ($attribute['key'] === 'son1') { + $this->assertEquals('twoWayKey', $attribute['options']['twoWayKey']); + var_dump($attribute); + } + + if ($attribute['key'] === 'sons') { + $this->assertEquals('parent_id', $attribute['options']['twoWayKey']); + } + } + + var_dump($collection->getAttribute('attributes')[0]->getId()); + + die; + + //todo make some tests + $docs = static::getDatabase()->find('parent', []); + var_dump($docs); + + $document = static::getDatabase()->createDocument('parent', new Document([ + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + 'son1' => [ + '$id' => 'foo', + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ], + 'sons' => [ + [ + '$id' => 'bar', + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ], + ], + ])); + + $docs = static::getDatabase()->find('parent', []); + + + + } + public function testOneToOneOneWayRelationship(): void { if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { From c0d246048a3b04813eaf7f1af8d2c160d840e68c Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 09:09:29 +0300 Subject: [PATCH 02/13] Identical twoWayKey --- tests/Database/Base.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index cec910eb4..9ca4676bc 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -3783,7 +3783,7 @@ public function testDoubleOneToOneOneWayRelationship(): void foreach ($attributes as $attribute) { if ($attribute['key'] === 'son1') { - $this->assertEquals('twoWayKey', $attribute['options']['twoWayKey']); + $this->assertEquals('parent', $attribute['options']['twoWayKey']); var_dump($attribute); } @@ -3794,8 +3794,6 @@ public function testDoubleOneToOneOneWayRelationship(): void var_dump($collection->getAttribute('attributes')[0]->getId()); - die; - //todo make some tests $docs = static::getDatabase()->find('parent', []); var_dump($docs); From f39ee8cd5d7233ee7bb53b7a967018a2bac4f6bd Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 11:15:49 +0300 Subject: [PATCH 03/13] Some tests + lint --- src/Database/Database.php | 6 +- tests/Database/Base.php | 175 ++++++++++++++++++-------------------- 2 files changed, 87 insertions(+), 94 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index ff34c929f..3ff78e84c 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1475,8 +1475,10 @@ public function createRelationship( throw new DuplicateException('Attribute already exists'); } - if($attribute->getAttribute('type') === self::VAR_RELATIONSHIP && - \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey)){ + if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP + && \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) + && $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId() + ) { throw new DuplicateException('TwoWayKey already exists'); } } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 9ca4676bc..d60acbd01 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -3739,97 +3739,6 @@ public function testWritePermissions(): void // Relationships - // Relationships - public function testDoubleOneToOneOneWayRelationship(): void - { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { - $this->expectNotToPerformAssertions(); - return; - } - - static::getDatabase()->createCollection('parent'); - static::getDatabase()->createCollection('son'); - - static::getDatabase()->createRelationship( - collection: 'parent', - relatedCollection: 'son', - type: Database::RELATION_ONE_TO_ONE, - id: 'son1' - ); - - try { - static::getDatabase()->createRelationship( - collection: 'parent', - relatedCollection: 'son', - type: Database::RELATION_ONE_TO_MANY, - id: 'sons', - ); - $this->fail('Failed to throw Exception'); - } catch (Exception $e) { - $this->assertEquals('TwoWayKey already exists', $e->getMessage()); - } - - static::getDatabase()->createRelationship( - collection: 'parent', - relatedCollection: 'son', - type: Database::RELATION_ONE_TO_MANY, - id: 'sons', - twoWayKey: 'parent_id' - ); - - $collection = static::getDatabase()->getCollection('parent'); - - $attributes = $collection->getAttribute('attributes', []); - - foreach ($attributes as $attribute) { - if ($attribute['key'] === 'son1') { - $this->assertEquals('parent', $attribute['options']['twoWayKey']); - var_dump($attribute); - } - - if ($attribute['key'] === 'sons') { - $this->assertEquals('parent_id', $attribute['options']['twoWayKey']); - } - } - - var_dump($collection->getAttribute('attributes')[0]->getId()); - - //todo make some tests - $docs = static::getDatabase()->find('parent', []); - var_dump($docs); - - $document = static::getDatabase()->createDocument('parent', new Document([ - '$permissions' => [ - Permission::read(Role::any()), - Permission::update(Role::any()), - Permission::delete(Role::any()), - ], - 'son1' => [ - '$id' => 'foo', - '$permissions' => [ - Permission::read(Role::any()), - Permission::update(Role::any()), - Permission::delete(Role::any()), - ], - ], - 'sons' => [ - [ - '$id' => 'bar', - '$permissions' => [ - Permission::read(Role::any()), - Permission::update(Role::any()), - Permission::delete(Role::any()), - ], - ], - ], - ])); - - $docs = static::getDatabase()->find('parent', []); - - - - } - public function testOneToOneOneWayRelationship(): void { if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { @@ -4247,7 +4156,6 @@ public function testOneToOneOneWayRelationship(): void $library = $person->getAttribute('newLibrary', ''); $this->assertEquals(null, $library); } - /** * @throws AuthorizationException * @throws LimitException @@ -4825,6 +4733,89 @@ 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('son'); + + static::getDatabase()->createRelationship( + collection: 'parent', + relatedCollection: 'son', + type: Database::RELATION_ONE_TO_ONE, + id: 'son1' + ); + + try { + static::getDatabase()->createRelationship( + collection: 'parent', + relatedCollection: 'son', + type: Database::RELATION_ONE_TO_MANY, + id: 'sons', + ); + $this->fail('Failed to throw Exception'); + } catch (Exception $e) { + $this->assertEquals('TwoWayKey already exists', $e->getMessage()); + } + + static::getDatabase()->createRelationship( + collection: 'parent', + relatedCollection: 'son', + type: Database::RELATION_ONE_TO_MANY, + id: 'sons', + twoWayKey: 'parent_id' + ); + + $collection = static::getDatabase()->getCollection('parent'); + $attributes = $collection->getAttribute('attributes', []); + foreach ($attributes as $attribute) { + if ($attribute['key'] === 'son1') { + $this->assertEquals('parent', $attribute['options']['twoWayKey']); + } + + if ($attribute['key'] === 'sons') { + $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()), + ], + 'son1' => [ + '$id' => 'foo', + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ], + 'sons' => [ + [ + '$id' => 'bar', + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ], + ], + ])); + + $documents = static::getDatabase()->find('parent', []); + $document = array_pop($documents); + $this->assertArrayHasKey('son1', $document); + $this->assertEquals('foo', $document->getAttribute('son1')->getId()); + $this->assertArrayHasKey('sons', $document); + $this->assertEquals('bar', $document->getAttribute('sons')[0]->getId()); + } + public function testOneToManyOneWayRelationship(): void { if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { From 8728b545f5277b3ecd7d27d0ec7d4e864b9a467f Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 11:16:17 +0300 Subject: [PATCH 04/13] Typo --- tests/Database/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index d60acbd01..6e4c5a7ca 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -4733,7 +4733,7 @@ public function testOneToOneTwoWayRelationship(): void $this->assertEquals(null, $country); } - public function testIdenticaltwoWayKeyRelationship(): void + public function testIdenticalTwoWayKeyRelationship(): void { if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); From afdd83e9f7d6bb05506978e3bd07eb54a41f0296 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 12:01:08 +0300 Subject: [PATCH 05/13] $permissions --- tests/Database/Base.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 6e4c5a7ca..d47ed5293 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -4790,20 +4790,12 @@ public function testIdenticalTwoWayKeyRelationship(): void ], 'son1' => [ '$id' => 'foo', - '$permissions' => [ - Permission::read(Role::any()), - Permission::update(Role::any()), - Permission::delete(Role::any()), - ], + '$permissions' => [], ], 'sons' => [ [ '$id' => 'bar', - '$permissions' => [ - Permission::read(Role::any()), - Permission::update(Role::any()), - Permission::delete(Role::any()), - ], + '$permissions' => [], ], ], ])); From fd8b79ece913cb31554367ac8b9d6045a654513d Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 12:03:05 +0300 Subject: [PATCH 06/13] $permissions --- tests/Database/Base.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index d47ed5293..1943f72bd 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -4790,12 +4790,12 @@ public function testIdenticalTwoWayKeyRelationship(): void ], 'son1' => [ '$id' => 'foo', - '$permissions' => [], + '$permissions' => [Permission::read(Role::any())], ], 'sons' => [ [ '$id' => 'bar', - '$permissions' => [], + '$permissions' => [Permission::read(Role::any())], ], ], ])); From 6afd41ce5ad34127addb41e964da9bc9e700153b Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 12:04:12 +0300 Subject: [PATCH 07/13] stopOnFailure --- phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 3833748e0..31b947dd6 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true"> + stopOnFailure="false"> ./tests/ From eab087befc5e5f2dacaec84c584856c12193ae7a Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 12:05:00 +0300 Subject: [PATCH 08/13] Extra line --- tests/Database/Base.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 1943f72bd..69987e196 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -3738,7 +3738,6 @@ public function testWritePermissions(): void } // Relationships - public function testOneToOneOneWayRelationship(): void { if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { From 56268bcd9e924db7c3f98e3c176ad36c0a925560 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 12:05:43 +0300 Subject: [PATCH 09/13] Extra line --- tests/Database/Base.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 69987e196..9cda88b41 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -4155,6 +4155,7 @@ public function testOneToOneOneWayRelationship(): void $library = $person->getAttribute('newLibrary', ''); $this->assertEquals(null, $library); } + /** * @throws AuthorizationException * @throws LimitException From 700098ced386d39d298745cd57115829e4ec2fcf Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 13:29:26 +0300 Subject: [PATCH 10/13] Comments --- src/Database/Database.php | 2 +- tests/Database/Base.php | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 3ff78e84c..223b4fdb4 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1479,7 +1479,7 @@ public function createRelationship( && \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) && $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId() ) { - throw new DuplicateException('TwoWayKey already exists'); + throw new DuplicateException('Related attribute already exists'); } } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 9cda88b41..7b89c1850 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -4741,43 +4741,43 @@ public function testIdenticalTwoWayKeyRelationship(): void } static::getDatabase()->createCollection('parent'); - static::getDatabase()->createCollection('son'); + static::getDatabase()->createCollection('child'); static::getDatabase()->createRelationship( collection: 'parent', - relatedCollection: 'son', + relatedCollection: 'child', type: Database::RELATION_ONE_TO_ONE, - id: 'son1' + id: 'child1' ); try { static::getDatabase()->createRelationship( collection: 'parent', - relatedCollection: 'son', + relatedCollection: 'child', type: Database::RELATION_ONE_TO_MANY, - id: 'sons', + id: 'children', ); $this->fail('Failed to throw Exception'); } catch (Exception $e) { - $this->assertEquals('TwoWayKey already exists', $e->getMessage()); + $this->assertEquals('Related attribute already exists', $e->getMessage()); } static::getDatabase()->createRelationship( collection: 'parent', - relatedCollection: 'son', + relatedCollection: 'child', type: Database::RELATION_ONE_TO_MANY, - id: 'sons', + id: 'children', twoWayKey: 'parent_id' ); $collection = static::getDatabase()->getCollection('parent'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { - if ($attribute['key'] === 'son1') { + if ($attribute['key'] === 'child1') { $this->assertEquals('parent', $attribute['options']['twoWayKey']); } - if ($attribute['key'] === 'sons') { + if ($attribute['key'] === 'children') { $this->assertEquals('parent_id', $attribute['options']['twoWayKey']); } } @@ -4788,11 +4788,11 @@ public function testIdenticalTwoWayKeyRelationship(): void Permission::update(Role::any()), Permission::delete(Role::any()), ], - 'son1' => [ + 'child1' => [ '$id' => 'foo', '$permissions' => [Permission::read(Role::any())], ], - 'sons' => [ + 'children' => [ [ '$id' => 'bar', '$permissions' => [Permission::read(Role::any())], @@ -4802,10 +4802,10 @@ public function testIdenticalTwoWayKeyRelationship(): void $documents = static::getDatabase()->find('parent', []); $document = array_pop($documents); - $this->assertArrayHasKey('son1', $document); - $this->assertEquals('foo', $document->getAttribute('son1')->getId()); - $this->assertArrayHasKey('sons', $document); - $this->assertEquals('bar', $document->getAttribute('sons')[0]->getId()); + $this->assertArrayHasKey('child1', $document); + $this->assertEquals('foo', $document->getAttribute('child1')->getId()); + $this->assertArrayHasKey('children', $document); + $this->assertEquals('bar', $document->getAttribute('children')[0]->getId()); } public function testOneToManyOneWayRelationship(): void From d292d56dd8270a3078fbf279d534c27b3abdc1d6 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 15:53:13 +0300 Subject: [PATCH 11/13] UpdateRelationship tests --- phpunit.xml | 2 +- src/Database/Database.php | 2 +- tests/Database/Base.php | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 31b947dd6..3833748e0 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false"> + stopOnFailure="true"> ./tests/ diff --git a/src/Database/Database.php b/src/Database/Database.php index 223b4fdb4..053b41d39 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1670,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'); } $type = $attribute['options']['relationType']; diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 7b89c1850..4aa513466 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -4806,6 +4806,28 @@ public function testIdenticalTwoWayKeyRelationship(): void $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 From c56e61dfd18b959801b7a7ae1afb7c0a739500bf Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 15:56:56 +0300 Subject: [PATCH 12/13] 'Related attribute already exists' --- tests/Database/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 4aa513466..9697fabb7 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -10261,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()); } } From 9540fa7eaa07eb9db454d270cea8d9ea16b1cc49 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 23 May 2023 15:58:48 +0300 Subject: [PATCH 13/13] stopOnFailure --- phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 3833748e0..31b947dd6 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true"> + stopOnFailure="false"> ./tests/