diff --git a/src/Bridge/Doctrine/NQMFactory.php b/src/Bridge/Doctrine/NQMFactory.php new file mode 100644 index 0000000..cf63b81 --- /dev/null +++ b/src/Bridge/Doctrine/NQMFactory.php @@ -0,0 +1,39 @@ + + * + * 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 + * @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); + } +} diff --git a/tests/Bridge/Doctrine/NQMFactoryTest.php b/tests/Bridge/Doctrine/NQMFactoryTest.php new file mode 100644 index 0000000..6e43586 --- /dev/null +++ b/tests/Bridge/Doctrine/NQMFactoryTest.php @@ -0,0 +1,43 @@ + + * + * 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(); + } +}