Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jul 24, 2023
1 parent 8b85b83 commit 1a2f48a
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427;

use Cycle\ORM\Select;
use Cycle\ORM\Tests\Functional\Driver\Common\BaseTest;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\Entity\Buyer;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\Entity\User;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\IntegrationTestTrait;
use Cycle\ORM\Tests\Traits\TableTrait;

abstract class CaseTest extends BaseTest
{
use IntegrationTestTrait;
use TableTrait;

public function setUp(): void
{
// Init DB
parent::setUp();

// Make tables
$this->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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\Entity;

class Buyer
{
public array $partners = [];

public function __construct(
public int $id,
public string $address,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\Entity;

use Ramsey\Uuid\UuidInterface;

class BuyerPartner
{
public int $buyer_id;
public UuidInterface $partner_id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\Entity;

use Ramsey\Uuid\UuidInterface;

class User
{
public ?UuidInterface $id;
public string $name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427;

use Cycle\ORM\Parser\CastableInterface;
use Cycle\ORM\Parser\UncastableInterface;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

class UuidTypecast implements CastableInterface, UncastableInterface
{
/** @var non-empty-string[] */
private array $rules = [];

public function setRules(array $rules): array
{
/**
* @var non-empty-string $key
* @var mixed $rule
*/
foreach ($rules as $key => $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;
}
}
68 changes: 68 additions & 0 deletions tests/ORM/Functional/Driver/Common/Integration/Case427/schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

use Cycle\ORM\Relation;
use Cycle\ORM\SchemaInterface as Schema;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\Entity\Buyer;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\Entity\BuyerPartner;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\Entity\User;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\UuidTypecast;

return [
'user' => [
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],
],
];
17 changes: 17 additions & 0 deletions tests/ORM/Functional/Driver/MySQL/Integration/Case427/CaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\MySQL\Integration\Case427;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\CaseTest as CommonClass;

/**
* @group driver
* @group driver-mysql
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'mysql';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Postgres\Integration\Case427;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\CaseTest as CommonClass;

/**
* @group driver
* @group driver-postgres
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'postgres';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\SQLServer\Integration\Case427;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\CaseTest as CommonClass;

/**
* @group driver
* @group driver-sqlserver
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'sqlserver';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\SQLite\Integration\Case427;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case427\CaseTest as CommonClass;

/**
* @group driver
* @group driver-sqlite
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'sqlite';
}

0 comments on commit 1a2f48a

Please sign in to comment.