Skip to content

Commit

Permalink
Add ArrayQueryLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Eckerstorfer committed Oct 6, 2015
1 parent 985c49a commit ef49acb
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/QueryLoader/ArrayQueryLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* This file is part of cocur/nqm.
*
* (c) Florian Eckerstorfer <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cocur\NQM\QueryLoader;

use Cocur\NQM\Exception\QueryNotExistsException;

/**
* Stories queries in an array.
*
* use Cocur\NQM\QueryLoader\ArrayQueryLoader;
*
* $cache = new ArrayQueryLoader(['foo' => 'SELECT ...;']);
*
* $pdo = new \PDO(...);
* $nqm = new NQM($pdo, $cache);
*
* @package cocur/nqm
* @subpackage queryloader
* @author Florian Eckerstorfer <[email protected]>
* @copyright 2013-2015 Florian Eckerstorfer
* @license http://opensource.org/licenses/MIT The MIT License
*/
class ArrayQueryLoader implements QueryLoaderInterface
{
/** @var string[] */
private $queries = [];

/**
* @param string[] $queries
*
* @codeCoverageIgnore
*/
public function __construct(array $queries)
{
$this->queries = $queries;
}

/**
* {@inheritDoc}
*/
public function hasQuery($name)
{
if (isset($this->queries[$name])) {
return true;
}

return false;
}

/**
* {@inheritDoc}
*/
public function getQuery($name)
{
if (isset($this->queries[$name])) {
return $this->queries[$name];
}

throw new QueryNotExistsException(sprintf('There exists no query with the name "%s".', $name));
}
}
73 changes: 73 additions & 0 deletions tests/QueryLoader/ArrayQueryLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* This file is part of cocur/nqm.
*
* (c) Florian Eckerstorfer <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cocur\NQM\QueryLoader;

use Cocur\NQM\QueryLoader\ArrayQueryLoader;
use Mockery as m;

/**
* ArrayQueryLoaderTest
*
* @category test
* @package cocur/nqm
* @author Florian Eckerstorfer <[email protected]>
* @copyright 2013-2015 Florian Eckerstorfer
* @license http://opensource.org/licenses/MIT The MIT License
* @group unit
*/
class ArrayQueryLoaderTest extends \PHPUnit_Framework_TestCase
{
/** @var ArrayQueryLoader */
private $loader;

public function setUp()
{
$this->loader = new ArrayQueryLoader(['foo' => 'SELECT;']);
}

/**
* @test
* @covers Cocur\NQM\QueryLoader\ArrayQueryLoader::hasQuery()
*/
public function hasQueryReturnsTrueIfQueryExists()
{
$this->assertTrue($this->loader->hasQuery('foo'));
}

/**
* @test
* @covers Cocur\NQM\QueryLoader\ArrayQueryLoader::hasQuery()
*/
public function hasQueryReturnsFalseIfQueryDoesNotExist()
{
$this->assertFalse($this->loader->hasQuery('invalid'));
}

/**
* @test
* @covers Cocur\NQM\QueryLoader\ArrayQueryLoader::getQuery()
*/
public function getQueryReturnsQueryIfQueryExists()
{
$this->assertEquals('SELECT;', $this->loader->getQuery('foo'));
}

/**
* @test
* @covers Cocur\NQM\QueryLoader\ArrayQueryLoader::getQuery()
* @expectedException \Cocur\NQM\Exception\QueryNotExistsException
*/
public function getQueryThrowsExceptionIfQueryDoesNotExist()
{
$this->loader->getQuery('invalid');
}
}

0 comments on commit ef49acb

Please sign in to comment.