Skip to content

Commit

Permalink
- [bugfix] fixes typo in eager loading
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Jul 30, 2020
1 parent 232445f commit 763dce2
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

v1.2.12 (30.07.2020)
--------------------
- [bugfix] fixes typo in eager loading

v1.2.11 (22.07.2020)
--------------------
- [bugfix] incorrect update sequence for nullable numeric values
Expand Down
2 changes: 1 addition & 1 deletion src/Select/JoinableLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function withContext(LoaderInterface $parent, array $options = []): Loade
$loader->setConstrain($this->getSource()->getConstrain());
}

if ($this->isLoaded()) {
if ($loader->isLoaded()) {
foreach ($loader->getEagerRelations() as $relation) {
$loader->loadRelation($relation, [], false, true);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/ORM/Driver/MySQL/Eager2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\ORM\Tests\Driver\MySQL;

class Eager2Test extends \Cycle\ORM\Tests\Eager2Test
{
public const DRIVER = 'mysql';
}
17 changes: 17 additions & 0 deletions tests/ORM/Driver/MySQL/ManyToManyPromiseEagerLoadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\ORM\Tests\Driver\MySQL;

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

/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\ORM\Tests\Driver\Postgres;

class Eager2Test extends \Cycle\ORM\Tests\Eager2Test
{
public const DRIVER = 'postgres';
}
17 changes: 17 additions & 0 deletions tests/ORM/Driver/Postgres/ManyToManyPromiseEagerLoadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\ORM\Tests\Driver\Postgres;

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

/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\ORM\Tests\Driver\SQLServer;

class Eager2Test extends \Cycle\ORM\Tests\Eager2Test
{
public const DRIVER = 'sqlserver';
}
17 changes: 17 additions & 0 deletions tests/ORM/Driver/SQLServer/ManyToManyPromiseEagerLoadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\ORM\Tests\Driver\SQLServer;

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

/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\ORM\Tests\Driver\SQLite;

class Eager2Test extends \Cycle\ORM\Tests\Eager2Test
{
public const DRIVER = 'sqlite';
}
17 changes: 17 additions & 0 deletions tests/ORM/Driver/SQLite/ManyToManyPromiseEagerLoadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\ORM\Tests\Driver\SQLite;

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

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Cycle\ORM\Tests;

use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\Relation;
use Cycle\ORM\Schema;
use Cycle\ORM\Select;
use Cycle\ORM\Tests\Fixtures\Nested;
use Cycle\ORM\Tests\Fixtures\Profile;
use Cycle\ORM\Tests\Fixtures\User;
use Cycle\ORM\Tests\Traits\TableTrait;

abstract class Eager2Test extends BaseTest
{
use TableTrait;

public function setUp(): void
{
parent::setUp();

$this->makeTable('user', [
'id' => 'primary',
'email' => 'string',
'balance' => 'float'
]);

$this->makeTable('profile', [
'id' => 'primary',
'user_id' => 'integer,nullable',
'image' => 'string'
]);

$this->makeTable('nested', [
'id' => 'primary',
'profile_id' => 'integer',
'label' => 'string'
]);

$this->makeFK('profile', 'user_id', 'user', 'id');
$this->makeFK('nested', 'profile_id', 'profile', 'id');

$this->getDatabase()->table('user')->insertMultiple(
['email', 'balance'],
[
['hello@world.com', 100],
['another@world.com', 200],
]
);

$this->getDatabase()->table('profile')->insertMultiple(
['user_id', 'image'],
[
[1, 'image.png'],
]
);


$this->getDatabase()->table('nested')->insertMultiple(
['profile_id', 'label'],
[
[1, 'nested-label'],
]
);

$this->orm = $this->withSchema(new Schema([
User::class => [
Schema::ROLE => 'user',
Schema::MAPPER => Mapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'user',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => ['id', 'email', 'balance'],
Schema::SCHEMA => [],
Schema::RELATIONS => [
'profile' => [
Relation::TYPE => Relation::HAS_ONE,
Relation::TARGET => Profile::class,
Relation::LOAD => Relation::LOAD_PROMISE,
Relation::SCHEMA => [
Relation::CASCADE => true,
Relation::INNER_KEY => 'id',
Relation::OUTER_KEY => 'user_id',
],
]
]
],
Profile::class => [
Schema::ROLE => 'profile',
Schema::MAPPER => Mapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'profile',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => ['id', 'user_id', 'image'],
Schema::SCHEMA => [],
Schema::RELATIONS => [
'nested' => [
Relation::TYPE => Relation::HAS_ONE,
Relation::TARGET => Nested::class,
Relation::LOAD => Relation::LOAD_EAGER,
Relation::SCHEMA => [
Relation::CASCADE => true,
Relation::INNER_KEY => 'id',
Relation::OUTER_KEY => 'profile_id',
],
]
]
],
Nested::class => [
Schema::ROLE => 'nested',
Schema::MAPPER => Mapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'nested',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => ['id', 'profile_id', 'label'],
Schema::SCHEMA => [],
Schema::RELATIONS => []
]
]));
}

public function testFetchEager(): void
{
$selector = new Select($this->orm, User::class);

$this->assertEquals([
[
'id' => 1,
'email' => 'hello@world.com',
'balance' => 100.0,
],
[
'id' => 2,
'email' => 'another@world.com',
'balance' => 200.0,
],
], $selector->fetchData());
}

public function testFetchEager2(): void
{
$selector = new Select($this->orm, User::class);
$selector->where('profile.id', '>', 0);

$this->assertCount(3, $selector->buildQuery()->getColumns());

$this->assertEquals([
[
'id' => 1,
'email' => 'hello@world.com',
'balance' => 100.0,
],
], $selector->fetchData());
}
}

0 comments on commit 763dce2

Please sign in to comment.