-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#17 Tested simple EntityManager query run.
- Loading branch information
Showing
7 changed files
with
150 additions
and
28 deletions.
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
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
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
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 |
---|---|---|
|
@@ -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]> | ||
*/ | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,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); | ||
} | ||
} |