-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Florian Eckerstorfer
committed
Oct 6, 2015
1 parent
985c49a
commit ef49acb
Showing
2 changed files
with
143 additions
and
0 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
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)); | ||
} | ||
} |
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,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'); | ||
} | ||
} |