Skip to content

Commit

Permalink
#17 Tested simple EntityManager query run.
Browse files Browse the repository at this point in the history
  • Loading branch information
hexus committed Sep 8, 2019
1 parent eba13c2 commit 36d2494
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 28 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<phpunit bootstrap="phpunit.php">
<testsuites>
<testsuite name="Darya Framework Functional Test Suite">
<directory suffix="Test.php">tests/Functional</directory>
</testsuite>
<testsuite name="Darya Framework Unit Test Suite">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
Expand Down
24 changes: 20 additions & 4 deletions src/Darya/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,30 @@ public function run(Query $query)
$storage = $mapper->getStorage();
$storageKey = $mapper->getEntityMap()->getStorageKey();

$idQuery = clone $query;
$idQuery->fields($storageKey);
$ids = $storage->run($idQuery);
$fields = $query->fields;
$query = clone $query;
$query->fields($storageKey);
$idsData = $storage->run($query->storageQuery)->data;

// TODO: Cleaner ID pluck with a helper function perhaps
$ids = [];

foreach ($idsData as $idsDatum) {
$ids[] = $idsDatum[$storageKey];
}

// TODO: Check related entity existence ($query->has) to filter down IDs

// Load root entities by ID
$entities = $storage->run($query->where($storageKey, $ids));
$query->fields($fields)->where($storageKey, $ids);
$entitiesResult = $storage->run($query->storageQuery);

// TODO: Cleaner multi-entity mapping
$entities = [];

foreach ($entitiesResult->data as $entityDatum) {
$entities[] = $mapper->mapFromStorage($mapper->newInstance(), $entityDatum);
}

// TODO: Load related entities and map them to the root entities ($query->with)

Expand Down
4 changes: 2 additions & 2 deletions src/Darya/ORM/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public function getStorage(): Queryable
* @param array $storageData The storage data to map from.
* @return object The resulting entity.
*/
protected function mapFromStorage($entity, array $storageData)
public function mapFromStorage($entity, array $storageData)
{
$entityMap = $this->getEntityMap();
$mapping = $entityMap->getMapping();
Expand All @@ -301,7 +301,7 @@ protected function mapFromStorage($entity, array $storageData)
* @param object $entity The entity to map from.
* @return array The resulting storage data.
*/
protected function mapToStorage($entity): array
public function mapToStorage($entity): array
{
$entityMap = $this->getEntityMap();
$mapping = $entityMap->getMapping();
Expand Down
39 changes: 32 additions & 7 deletions src/Darya/ORM/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

use Darya\Storage;
use InvalidArgumentException;
use RuntimeException;

/**
* Darya's ORM query.
*
* @mixin Storage\Query
* @property-read string $entity
* @property-read Storage\Query $storageQuery
* @property-read string[] $has
* @property-read string[] $with
* @property-read string $entity
* @property-read Storage\Query $storageQuery
* @property-read string[]|callable[] $has
* @property-read string[]|callable[] $with
*
* @author Chris Andrew <[email protected]>
*/
Expand Down Expand Up @@ -52,10 +53,10 @@ class Query
* @param string $entity
* @param Storage\Query $storageQuery
*/
public function __construct(string $entity, Storage\Query $storageQuery)
public function __construct(Storage\Query $storageQuery, string $entity)
{
$this->entity = $entity;
$this->storageQuery = $storageQuery;
$this->entity($entity);
}

/**
Expand All @@ -69,7 +70,9 @@ public function __construct(string $entity, Storage\Query $storageQuery)
*/
public function entity(string $entity)
{
return $this->resource($entity);
$this->entity = $entity;

return $this;
}

/**
Expand Down Expand Up @@ -112,4 +115,26 @@ public function __get(string $property)

return $this->storageQuery->$property;
}

/**
* Dynamically invoke a method.
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public function __call(string $method, array $arguments)
{
if (!method_exists($this->storageQuery, $method)) {
throw new RuntimeException("Undefined method $method()");
}

$result = $this->storageQuery->{$method}(...$arguments);

if ($result instanceof Storage\Query) {
return $this;
}

return $result;
}
}
12 changes: 6 additions & 6 deletions src/Darya/Storage/InMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected static function limit(array $data, $limit = 0, $offset = 0)
* @param int $offset [optional]
* @return array
*/
public function read($resource, array $filter = array(), $order = array(), $limit = 0, $offset = 0)
public function read($resource, array $filter = array(), $order = null, $limit = null, $offset = 0)
{
if (empty($this->data[$resource])) {
return array();
Expand Down Expand Up @@ -96,7 +96,7 @@ public function read($resource, array $filter = array(), $order = array(), $limi
* @param int $offset [optional]
* @return array
*/
public function listing($resource, $fields, array $filter = array(), $order = array(), $limit = 0, $offset = 0)
public function listing($resource, $fields, array $filter = array(), $order = array(), $limit = null, $offset = 0)
{
$data = $this->read($resource, $filter, $order, $limit, $offset);

Expand Down Expand Up @@ -172,7 +172,7 @@ public function create($resource, $data)
* @param int $limit [optional]
* @return int
*/
public function update($resource, $data, array $filter = array(), $limit = 0)
public function update($resource, $data, array $filter = array(), $limit = null)
{
if (empty($this->data[$resource])) {
return 0;
Expand Down Expand Up @@ -288,8 +288,8 @@ public function distinct($resource, $field, array $filter = array(), $order = ar
*/
public function run(Query $query)
{
$data = array();
$info = array();
$data = [];
$info = [];

switch ($query->type) {
case Query::CREATE:
Expand Down Expand Up @@ -338,7 +338,7 @@ public function query($resource, $fields = array())
}

/**
* Retrieve the error that occured with the last operation.
* Retrieve the error that occurred with the last operation.
*
* Returns false if there was no error.
*
Expand Down
9 changes: 0 additions & 9 deletions tests/Functional/ORM/EntityManager.php

This file was deleted.

87 changes: 87 additions & 0 deletions tests/Functional/ORM/EntityManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Darya\Tests\Functional\ORM;

use Darya\ORM\EntityGraph;
use Darya\ORM\EntityManager;
use Darya\ORM\EntityMap;
use Darya\ORM\Mapper;
use Darya\ORM\Query;
use Darya\ORM\Strategy\PropertyStrategy;
use Darya\Storage\InMemory;
use Darya\Tests\Unit\ORM\Fixtures\User;
use PHPUnit\Framework\TestCase;

class EntityManagerTest extends TestCase
{
/**
* @var InMemory
*/
protected $storage;

/**
* @var EntityGraph
*/
protected $graph;

/**
* Prepare in-memory storage and an entity graph for each test.
*/
public function setUp()
{
$this->storage = new InMemory([
'users' => [
[
'id' => 1,
'firstname' => 'Qui-Gon',
'surname' => 'Jinn',
'padawan_id' => 2
],
[
'id' => 2,
'firstname' => 'Obi-Wan',
'surname' => 'Kenobi',
'master_id' => 1
]
]
]);

$this->graph = new EntityGraph([
new Mapper(
new EntityMap(
User::class,
'users',
[
'id' => 'id',
'firstname' => 'firstname',
'surname' => 'surname',
'padawan_id' => 'padawan_id',
'master_id' => 'master_id'
],
new PropertyStrategy()
),
$this->storage
)
]);
}

public function testSimpleQueryRun()
{
$orm = new EntityManager($this->graph);

$query = (new Query(
new \Darya\Storage\Query('users'),
User::class
))->where('id', 2);

$users = $orm->run($query);

$this->assertCount(1, $users);
$user = $users[0];
$this->assertInstanceOf(User::class, $user);
$this->assertEquals($user->id, 2);
$this->assertEquals($user->firstname, 'Obi-Wan');
$this->assertEquals($user->surname, 'Kenobi');
$this->assertEquals($user->master_id, 1);
}
}

0 comments on commit 36d2494

Please sign in to comment.