Skip to content

Commit

Permalink
add SelectOneQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Oct 5, 2016
1 parent cc5d7dc commit d07dedf
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/SelectOneQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
namespace Librette\Doctrine\Queries;

/**
* @author David Matejka
*/
class SelectOneQuery extends BaseQueryObject
{

/** @var string */
private $entityClass;

/** @var array */
private $filters = [];

/** @var array */
private $orderBy = [];

/**
* @param string
*/
public function __construct($entityClass, array $filters = [])
{
$this->entityClass = $entityClass;
$this->filters = array_map(NULL, array_keys($filters), $filters);
}


/**
* @param string|\Closure
* @param string|array|null|mixed
* @return self
*/
public function filterBy($field, $value = NULL)
{
$this->filters[] = [$field, $value];

return $this;
}


/**
* @param string
* @param string
* @return self
*/
public function orderBy($field, $direction = 'ASC')
{
$this->orderBy[$field] = $direction;

return $this;
}


protected function doFetch(Queryable $queryable)
{
$qb = $queryable->createQueryBuilder($this->entityClass, 'e');
foreach ($this->filters as $filter) {
list ($field, $value) = $filter;
if ($value === NULL && $field instanceof \Closure) {
$field($qb, 'e');
} else {
$qb->whereCriteria([$field => $value]);
}
}
foreach ($this->orderBy as $field => $direction) {
$qb->autoJoinOrderBy($field, $direction);
}
$qb->setMaxResults(1);

return $qb->getQuery()->getOneOrNullResult();
}

}
50 changes: 50 additions & 0 deletions tests/src/SelectOneQueryTestCase.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace LibretteTests\Doctrine\Queries;

use Doctrine\DBAL\Logging\DebugStack;
use Librette\Doctrine\Queries\EntityQuery;
use Librette\Doctrine\Queries\Queryable;
use Librette\Doctrine\Queries\QueryHandler;
use Librette\Doctrine\Queries\SelectOneQuery;
use Librette\Queries\IQueryHandlerAccessor;
use LibretteTests\Doctrine\Queries\Model\User;
use Nette;
use Tester;
use Tester\Assert;

require_once __DIR__ . '/../bootstrap.php';


/**
* @author David Matějka
* @testCase
*/
class SelectOneQueryTestCase extends Tester\TestCase
{
use EntityManagerTest;


public function setUp()
{
}


public function testSelect()
{
$em = $this->createMemoryManager();
$queryHandler = new QueryHandler(new Queryable($em, \Mockery::mock(IQueryHandlerAccessor::class)));
$em->persist($john = new User('John'));
$em->persist($jack = new User('Jack'));
$em->flush();
$query = new SelectOneQuery(User::class, ['name' => 'John']);
Assert::same($john, $queryHandler->fetch($query));
$query = new SelectOneQuery(User::class, ['name' => 'Jack']);
Assert::same($jack, $queryHandler->fetch($query));
$query = new SelectOneQuery(User::class, ['name' => 'Jane']);
Assert::null($queryHandler->fetch($query));
}

}


\run(new SelectOneQueryTestCase());

0 comments on commit d07dedf

Please sign in to comment.