Skip to content

Commit

Permalink
Update feature test for pre-existing entities to better reflect its i…
Browse files Browse the repository at this point in the history
…ntention
  • Loading branch information
Stratadox committed Apr 25, 2018
1 parent 7a91b43 commit 9e9e7e0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
48 changes: 35 additions & 13 deletions tests/Feature/PreExistingEntities/Boxes_filled_with_Things.php
Expand Up @@ -4,7 +4,8 @@
namespace Stratadox\TableLoader\Test\Feature\PreExistingEntities;

use PHPUnit\Framework\TestCase;
use Stratadox\IdentityMap\IdentityMap;
use Stratadox\Hydration\Mapper\Instruction\Has;
use Stratadox\Hydration\Mapper\Instruction\Is;
use Stratadox\TableLoader\Joined;
use Stratadox\TableLoader\Load;
use Stratadox\TableLoader\LoadsTable;
Expand All @@ -23,19 +24,34 @@ class Boxes_filled_with_Things extends TestCase
/** @test */
function loading_boxes_with_their_things_using_previously_loaded_entities()
{
$foo = new Thing('Foo', new BoxProxy);
$bar = new Thing('Bar', new BoxProxy);
$identityMap = IdentityMap::with(['Foo' => $foo]);
/** @var LoadsTable $make */
$make = Joined::table(
Load::each('thing')
->by('name')
->as(Thing::class, [
'name' => Is::string(),
'box' => Has::one(BoxProxy::class)
])
)();

$table = $this->table([
//--------+--------------+,
[ 'box_id', 'thing_name' ],
//--------+--------------+,
[ 1 , 'Foo' ],
[ 1 , 'Bar' ],
//--------+--------------+,
]);

$previousResult = $make->from($table);

/** @var Thing $foo */
$foo = $previousResult['thing']['Foo'];
$this->assertInstanceOf(
BoxProxy::class,
$foo->box(),
'The box should be a proxy for now: we did not load all Things yet.'
);

/** @var LoadsTable $make */
$make = Joined::table(
Load::each('box')
Expand All @@ -48,27 +64,33 @@ function loading_boxes_with_their_things_using_previously_loaded_entities()
->havingOne('box', 'box')
)();

$result = $make->from($table, $identityMap);
$table = $this->table([
//--------+--------------+,
[ 'box_id', 'thing_name' ],
//--------+--------------+,
[ 1 , 'Foo' ],
[ 1 , 'Bar' ],
//--------+--------------+,
]);

$result = $make->from($table, $previousResult->identityMap());
$identityMap = $result->identityMap();

$this->assertSame(
$foo,
$result['thing']['Foo'],
'Foo should be reused because it was in the identity map.'
);
$this->assertNotSame(
$bar,
$result['thing']['Bar'],
'Bar should not be reused because it was not in the identity map.'
);
$this->assertNotInstanceOf(
BoxProxy::class,
$foo->box(),
'The box relation of Foo should be updated.'
'The box should not be a proxy anymore, now that the real one is loaded.'
);

$this->assertTrue(
$identityMap->has(Thing::class, 'Bar'),
'Bar should now be in the identity map.'
'Bar should now be in the identity map as well.'
);
$this->assertCount(2, $result['box'][1], 'Expecting two things in the box.');
}
}
11 changes: 10 additions & 1 deletion tests/Feature/PreExistingEntities/Fixture/Box.php
Expand Up @@ -3,7 +3,10 @@

namespace Stratadox\TableLoader\Test\Feature\PreExistingEntities\Fixture;

class Box
use function count;
use Countable;

class Box implements Countable
{
private $items;

Expand All @@ -20,4 +23,10 @@ public function items(): array
{
return $this->items;
}

/** @inheritdoc */
public function count()
{
return count($this->items);
}
}

0 comments on commit 9e9e7e0

Please sign in to comment.