Skip to content

Commit

Permalink
Add the test suite GenerateRelationsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Mar 31, 2021
1 parent 52d9694 commit 18d525f
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tests/Schema/Driver/MySQL/GenerateRelationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cycle\Schema\Tests\Driver\MySQL;

class GenerateRelationsTest extends \Cycle\Schema\Tests\Generator\GenerateRelationsTest
{
public const DRIVER = 'mysql';
}
10 changes: 10 additions & 0 deletions tests/Schema/Driver/Postgres/GenerateRelationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cycle\Schema\Tests\Driver\Postgres;

class GenerateRelationsTest extends \Cycle\Schema\Tests\Generator\GenerateRelationsTest
{
public const DRIVER = 'postgres';
}
10 changes: 10 additions & 0 deletions tests/Schema/Driver/SQLServer/GenerateRelationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cycle\Schema\Tests\Driver\SQLServer;

class GenerateRelationsTest extends \Cycle\Schema\Tests\Generator\GenerateRelationsTest
{
public const DRIVER = 'sqlserver';
}
10 changes: 10 additions & 0 deletions tests/Schema/Driver/SQLite/GenerateRelationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cycle\Schema\Tests\Driver\SQLite;

class GenerateRelationsTest extends \Cycle\Schema\Tests\Generator\GenerateRelationsTest
{
public const DRIVER = 'sqlite';
}
84 changes: 84 additions & 0 deletions tests/Schema/Generator/GenerateRelationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Cycle\Schema\Tests\Generator;

use Cycle\ORM\Relation;
use Cycle\ORM\Schema;
use Cycle\Schema\Compiler;
use Cycle\Schema\Definition\Relation as RelationDefinition;
use Cycle\Schema\Generator\GenerateRelations;
use Cycle\Schema\Generator\RenderTables;
use Cycle\Schema\Registry;
use Cycle\Schema\Tests\BaseTest;
use Cycle\Schema\Tests\Fixtures\Plain;
use Cycle\Schema\Tests\Fixtures\Post;
use Cycle\Schema\Tests\Fixtures\Tag;
use Cycle\Schema\Tests\Fixtures\TagContext;
use Cycle\Schema\Tests\Fixtures\User;

abstract class GenerateRelationsTest extends BaseTest
{
public function relationOptionsDataProvider(): array
{
return [
'default orderBy' => ['orderBy', [], Relation::ORDER_BY],
'custom orderBy' => ['orderBy', ['id' => 'DESC'], Relation::ORDER_BY],
'default where' => ['where', [], Relation::WHERE],
'custom where' => ['where', ['id' => '1'], Relation::WHERE],
];
}

/**
* @dataProvider relationOptionsDataProvider
*/
public function testHasManyToManyRelationOptions(string $optionKey, $optionValue, int $relationKey): void
{
$post = Post::define();
$tag = Tag::define();
$tagContext = TagContext::define();

$post->getRelations()->remove('author');

$post->getRelations()->set('tags', new RelationDefinition());
$post->getRelations()->get('tags')
->setType('manyToMany')
->setTarget('tag')
->getOptions()
->set('though', 'tagContext')
->set($optionKey, $optionValue);

$r = new Registry($this->dbal);
$r->register($post)->linkTable($post, 'default', 'post');
$r->register($tag)->linkTable($tag, 'default', 'tag');
$r->register($tagContext)->linkTable($tagContext, 'default', 'tag_context');

$c = new Compiler();
$schema = $c->compile($r, [new RenderTables(), new GenerateRelations()]);

$this->assertSame($optionValue, $schema['post'][Schema::RELATIONS]['tags'][Relation::SCHEMA][$relationKey]);
}

/**
* @dataProvider relationOptionsDataProvider
*/
public function testHasManyRelationOptions(string $optionKey, $optionValue, int $relationKey): void
{
$e = Plain::define();
$u = User::define();

$relation = $u->getRelations()->get('plain');
$relation->setType('hasMany');
$relation->getOptions()->set($optionKey, $optionValue);

$r = new Registry($this->dbal);
$r->register($e)->linkTable($e, 'default', 'plain');
$r->register($u)->linkTable($u, 'default', 'user');

$c = new Compiler();
$schema = $c->compile($r, [new RenderTables(), new GenerateRelations()]);

$this->assertSame($optionValue, $schema['user'][Schema::RELATIONS]['plain'][Relation::SCHEMA][$relationKey]);
}
}
3 changes: 2 additions & 1 deletion tests/Schema/Relation/BelongsToRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Cycle\ORM\Relation;
use Cycle\ORM\Schema;
use Cycle\Schema\Compiler;
use Cycle\Schema\Exception\RegistryException;
use Cycle\Schema\Generator\GenerateRelations;
use Cycle\Schema\Generator\RenderRelations;
use Cycle\Schema\Generator\RenderTables;
Expand Down Expand Up @@ -149,7 +150,7 @@ public function testInverseUnknownType(): void
$r->register($e)->linkTable($e, 'default', 'post');
$r->register($u)->linkTable($u, 'default', 'author');

$this->expectException(\Cycle\Schema\Exception\RegistryException::class);
$this->expectException(RegistryException::class);

(new Compiler())->compile($r, [
new GenerateRelations(['belongsTo' => new BelongsTo()])
Expand Down

0 comments on commit 18d525f

Please sign in to comment.