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

Self referencing entity test #491

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

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

use Cycle\ORM\EntityManager;
use Cycle\ORM\Tests\Functional\Driver\Common\BaseTest;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\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();
$this->makeTables();

$this->loadSchema(__DIR__ . '/schema.php');
}

public function testSave(): void
{
$this->captureWriteQueries();
$em = new EntityManager($this->orm);

$user = new User();

$em->persist($user);
$em->run();

// Check write queries count
$this->assertNumWrites(1);
}

private function makeTables(): void
{
// Make tables
$this->makeTable(User::ROLE, [
'id' => 'primary', // autoincrement
'user_id' => 'int',
]);
$this->makeFK(User::ROLE, 'user_id', User::ROLE, 'id', 'CASCADE', 'CASCADE');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

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

class User
{
public const ROLE = 'user';

public ?int $id = null;
public ?self $user = null;
public iterable $users = [];
}
61 changes: 61 additions & 0 deletions tests/ORM/Functional/Driver/Common/Integration/Case428/schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\Relation;
use Cycle\ORM\SchemaInterface as Schema;
use Cycle\ORM\Select\Repository;
use Cycle\ORM\Select\Source;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity\User;

return [
'user' => [
Schema::ENTITY => User::class,
Schema::SOURCE => Source::class,
Schema::MAPPER => Mapper::class,
Schema::REPOSITORY => Repository::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'user',
Schema::PRIMARY_KEY => ['id'],
Schema::FIND_BY_KEYS => ['id'],
Schema::COLUMNS => [
'id' => 'id',
'user_id' => 'user_id',
],
Schema::RELATIONS => [
'user' => [
Relation::TYPE => Relation::BELONGS_TO,
Relation::TARGET => 'user',
Relation::LOAD => Relation::LOAD_PROMISE,
Relation::SCHEMA => [
Relation::CASCADE => true,
Relation::NULLABLE => true,
Relation::INNER_KEY => 'user_id',
Relation::OUTER_KEY => ['id'],
Relation::INVERSION => 'users',
],
],
'users' => [
Relation::TYPE => Relation::HAS_MANY,
Relation::TARGET => 'user',
Relation::LOAD => Relation::LOAD_PROMISE,
Relation::SCHEMA => [
Relation::CASCADE => true,
Relation::NULLABLE => false,
Relation::WHERE => [],
Relation::ORDER_BY => [],
Relation::INNER_KEY => ['id'],
Relation::OUTER_KEY => 'user_id',
Relation::INVERSION => 'user',
],
],
],
Schema::SCOPE => null,
Schema::TYPECAST => [
'id' => 'int',
'user_id' => 'int',
],
Schema::SCHEMA => [],
],
];
17 changes: 17 additions & 0 deletions tests/ORM/Functional/Driver/MySQL/Integration/Case428/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\Case428;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\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\Case428;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\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\Case428;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\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\Case428;

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

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