From 1a2f48adc3dfac2a6a0e7ac1dfde7d37d74ced97 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Mon, 24 Jul 2023 14:10:05 +0400 Subject: [PATCH] Add test case --- .../Common/Integration/Case427/CaseTest.php | 85 +++++++++++++++++++ .../Integration/Case427/Entity/Buyer.php | 16 ++++ .../Case427/Entity/BuyerPartner.php | 13 +++ .../Integration/Case427/Entity/User.php | 13 +++ .../Integration/Case427/UuidTypecast.php | 67 +++++++++++++++ .../Common/Integration/Case427/schema.php | 68 +++++++++++++++ .../MySQL/Integration/Case427/CaseTest.php | 17 ++++ .../Postgres/Integration/Case427/CaseTest.php | 17 ++++ .../Integration/Case427/CaseTest.php | 17 ++++ .../SQLite/Integration/Case427/CaseTest.php | 17 ++++ 10 files changed, 330 insertions(+) create mode 100644 tests/ORM/Functional/Driver/Common/Integration/Case427/CaseTest.php create mode 100644 tests/ORM/Functional/Driver/Common/Integration/Case427/Entity/Buyer.php create mode 100644 tests/ORM/Functional/Driver/Common/Integration/Case427/Entity/BuyerPartner.php create mode 100644 tests/ORM/Functional/Driver/Common/Integration/Case427/Entity/User.php create mode 100644 tests/ORM/Functional/Driver/Common/Integration/Case427/UuidTypecast.php create mode 100644 tests/ORM/Functional/Driver/Common/Integration/Case427/schema.php create mode 100644 tests/ORM/Functional/Driver/MySQL/Integration/Case427/CaseTest.php create mode 100644 tests/ORM/Functional/Driver/Postgres/Integration/Case427/CaseTest.php create mode 100644 tests/ORM/Functional/Driver/SQLServer/Integration/Case427/CaseTest.php create mode 100644 tests/ORM/Functional/Driver/SQLite/Integration/Case427/CaseTest.php diff --git a/tests/ORM/Functional/Driver/Common/Integration/Case427/CaseTest.php b/tests/ORM/Functional/Driver/Common/Integration/Case427/CaseTest.php new file mode 100644 index 00000000..8a03591f --- /dev/null +++ b/tests/ORM/Functional/Driver/Common/Integration/Case427/CaseTest.php @@ -0,0 +1,85 @@ +makeTable('case_5_users', [ + 'id' => 'string,primary', + 'name' => 'string', + ]); + $this->makeTable('case_5_buyers', [ + 'id' => 'integer,primary', + 'address' => 'string', + ]); + $this->makeTable( + table: 'case_5_buyer_partners', + columns: [ + 'buyer_id' => 'int', + 'partner_id' => 'string', + ], + pk: ['buyer_id', 'partner_id'], + ); + + $this->loadSchema(__DIR__ . '/schema.php'); + + $this->getDatabase()->table('case_5_users')->insertMultiple( + ['id', 'name'], + [ + ['00000000-0000-0000-0000-000000000001', 'John'], + ['00000000-0000-0000-0000-000000000002', 'Sam'], + ['00000000-0000-0000-0000-000000000003', 'Paul'], + ], + ); + $this->getDatabase()->table('case_5_buyers')->insertMultiple( + ['id', 'address'], + [[4, 'foo'], [5, 'bar'], [6, 'baz']], + ); + $this->getDatabase()->table('case_5_buyer_partners')->insertMultiple( + ['buyer_id', 'partner_id'], + [[4, '00000000-0000-0000-0000-000000000001'], [4, '00000000-0000-0000-0000-000000000002']], + ); + } + + public function testSelect(): void + { + $buyer = (new Select($this->orm, Buyer::class)) + ->wherePK(4) + ->fetchOne(); + + $this->assertInstanceOf(Buyer::class, $buyer); + $this->assertSame(4, $buyer->id); + $this->assertSame('foo', $buyer->address); + + $this->assertCount(2, $buyer->partners); + + $user1 = $buyer->partners[0]; + $user2 = $buyer->partners[1]; + + \assert($user1 instanceof User); + \assert($user2 instanceof User); + + $this->assertSame('00000000-0000-0000-0000-000000000001', $user1->id->toString()); + $this->assertSame('00000000-0000-0000-0000-000000000002', $user2->id->toString()); + $this->assertSame('John', $user1->name); + $this->assertSame('Sam', $user2->name); + } +} diff --git a/tests/ORM/Functional/Driver/Common/Integration/Case427/Entity/Buyer.php b/tests/ORM/Functional/Driver/Common/Integration/Case427/Entity/Buyer.php new file mode 100644 index 00000000..52f5e0f7 --- /dev/null +++ b/tests/ORM/Functional/Driver/Common/Integration/Case427/Entity/Buyer.php @@ -0,0 +1,16 @@ + $rule) { + if ($rule === 'uuid') { + unset($rules[$key]); + $this->rules[$key] = $rule; + } + } + + return $rules; + } + + public function cast(array $data): array + { + foreach ($this->rules as $column => $rule) { + if (!isset($data[$column])) { + continue; + } + + \assert(\is_string($data[$column])); + $data[$column] = Uuid::fromString($data[$column]); + } + + return $data; + } + + public function uncast(array $data): array + { + foreach ($this->rules as $column => $rule) { + if (!isset($data[$column])) { + continue; + } + if (!$data[$column] instanceof UuidInterface) { + TestCase::fail(\sprintf( + 'Expected UuidInterface, got %s: %s', + \get_debug_type($data[$column]), + \print_r($data[$column], true) + )); + } + + $data[$column] = $data[$column]->toString(); + } + + return $data; + } +} diff --git a/tests/ORM/Functional/Driver/Common/Integration/Case427/schema.php b/tests/ORM/Functional/Driver/Common/Integration/Case427/schema.php new file mode 100644 index 00000000..66ea1c1c --- /dev/null +++ b/tests/ORM/Functional/Driver/Common/Integration/Case427/schema.php @@ -0,0 +1,68 @@ + [ + Schema::ENTITY => User::class, + Schema::TABLE => 'case_5_users', + Schema::PRIMARY_KEY => ['id'], + Schema::COLUMNS => [ + 'id' => 'id', + 'name' => 'name', + ], + Schema::TYPECAST => [ + 'id' => 'uuid', + ], + Schema::TYPECAST_HANDLER => [UuidTypecast::class, Cycle\ORM\Parser\Typecast::class], + ], + 'buyer' => [ + Schema::ENTITY => Buyer::class, + Schema::TABLE => 'case_5_buyers', + Schema::PRIMARY_KEY => ['id'], + Schema::COLUMNS => [ + 'id' => 'id', + 'address' => 'address', + ], + Schema::TYPECAST => [ + 'id' => 'int', + ], + Schema::RELATIONS => [ + 'partners' => [ + Relation::TYPE => Relation::MANY_TO_MANY, + Relation::TARGET => 'user', + Relation::LOAD => Relation::LOAD_PROMISE, + Relation::SCHEMA => [ + Relation::COLLECTION_TYPE => 'array', + Relation::THROUGH_ENTITY => 'buyer_partner', + Relation::INNER_KEY => 'id', + Relation::OUTER_KEY => 'id', + Relation::THROUGH_INNER_KEY => 'buyer_id', + Relation::THROUGH_OUTER_KEY => 'partner_id', + ], + ], + ], + Schema::TYPECAST_HANDLER => [UuidTypecast::class, Cycle\ORM\Parser\Typecast::class], + ], + 'buyer_partner' => [ + Schema::ENTITY => BuyerPartner::class, + Schema::TABLE => 'case_5_buyer_partners', + Schema::PRIMARY_KEY => ['buyer_id', 'partner_id'], + Schema::COLUMNS => [ + 'buyer_id' => 'buyer_id', + 'partner_id' => 'partner_id', + ], + Schema::TYPECAST => [ + 'buyer_id' => 'int', + 'partner_id' => 'uuid', + ], + Schema::TYPECAST_HANDLER => [UuidTypecast::class, Cycle\ORM\Parser\Typecast::class], + ], +]; diff --git a/tests/ORM/Functional/Driver/MySQL/Integration/Case427/CaseTest.php b/tests/ORM/Functional/Driver/MySQL/Integration/Case427/CaseTest.php new file mode 100644 index 00000000..c333cb32 --- /dev/null +++ b/tests/ORM/Functional/Driver/MySQL/Integration/Case427/CaseTest.php @@ -0,0 +1,17 @@ +