Skip to content

Commit

Permalink
Add factory method to create NQM from Doctrine EntityManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Eckerstorfer committed Feb 11, 2015
1 parent 034ba1d commit 4529d6a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Bridge/Doctrine/NQMFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

namespace Cocur\NQM\Bridge\Doctrine;

use Doctrine\ORM\EntityManager;
use Cocur\NQM\QueryLoader\QueryLoaderInterface;
use Cocur\NQM\NQM;

/**
* NQMFactory
*
* @package cocur/nqm
* @subpackage bridge
* @author Florian Eckerstorfer <florian@eckerstorfer.co>
* @copyright 2013-2015 Florian Eckerstorfer
* @license http://opensource.org/licenses/MIT The MIT License
*/
class NQMFactory
{
/**
* @param EntityManager $em
* @param QueryLoaderInterface $queryLoader
*
* @return NQM
*/
public static function createFromEntityManager(EntityManager $em, QueryLoaderInterface $queryLoader)
{
return new NQM($em->getConnection()->getWrappedConnection(), $queryLoader);
}
}
43 changes: 43 additions & 0 deletions tests/Bridge/Doctrine/NQMFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

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

namespace Cocur\NQM\Bridge\Doctrine;

use Mockery as m;
use Pseudo\Pdo;

class NQMFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
* @covers Cocur\NQM\Bridge\Doctrine\NQMFactory::createFromEntityManager()
*/
public function createFromEntityManager()
{
$conn = m::mock('Doctrine\DBAL\Connection');
$conn->shouldReceive('getWrappedConnection')->andReturn($this->getMockPdo());

$em = m::mock('Doctrine\ORM\EntityManager');
$em->shouldReceive('getConnection')->andReturn($conn);

$queryLoader = m::mock('Cocur\NQM\QueryLoader\QueryLoaderInterface');

$this->assertInstanceOf('Cocur\NQM\NQM', NQMFactory::createFromEntityManager($em, $queryLoader));
}

/**
* @return \PDO
*/
protected function getMockPdo()
{
return new Pdo();
}
}

0 comments on commit 4529d6a

Please sign in to comment.