-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the test suite
GenerateRelationsTest
- Loading branch information
Showing
6 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters