Files
Chamilo/vendor/knplabs/knp-components/tests/Test/Tool/BaseTestCaseMongoODM.php
2025-08-14 22:41:49 +02:00

163 lines
4.5 KiB
PHP

<?php
namespace Test\Tool;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Common\EventManager;
use Doctrine\MongoDB\Connection;
/**
* Base test case contains common mock objects
*/
abstract class BaseTestCaseMongoODM extends \PHPUnit_Framework_TestCase
{
/**
* @var DocumentManager
*/
protected $dm;
/**
* {@inheritdoc}
*/
protected function setUp()
{
if (!class_exists('MongoClient')) {
$this->markTestSkipped('Missing Mongo extension.');
}
}
/**
* {@inheritdoc}
*/
protected function tearDown()
{
if ($this->dm) {
foreach ($this->dm->getDocumentDatabases() as $db) {
foreach ($db->listCollections() as $collection) {
$collection->drop();
}
}
$this->dm->getConnection()->close();
$this->dm = null;
}
}
/**
* DocumentManager mock object together with
* annotation mapping driver and database
*
* @param EventManager $evm
* @return DocumentManager
*/
protected function getMockDocumentManager(EventManager $evm = null)
{
$conn = new Connection;
$config = $this->getMockAnnotatedConfig();
try {
$this->dm = DocumentManager::create($conn, $config, $evm ?: $this->getEventManager());
$this->dm->getConnection()->connect();
} catch (\MongoException $e) {
$this->markTestSkipped('Doctrine MongoDB ODM failed to connect');
}
return $this->dm;
}
/**
* DocumentManager mock object with
* annotation mapping driver
*
* @param EventManager $evm
* @return DocumentManager
*/
protected function getMockMappedDocumentManager(EventManager $evm = null)
{
$conn = $this->getMock('Doctrine\\MongoDB\\Connection');
$config = $this->getMockAnnotatedConfig();
$this->dm = DocumentManager::create($conn, $config, $evm ?: $this->getEventManager());
return $this->dm;
}
/**
* Creates default mapping driver
*
* @return \Doctrine\ORM\Mapping\Driver\Driver
*/
protected function getMetadataDriverImplementation()
{
return new AnnotationDriver($_ENV['annotation_reader']);
}
/**
* Build event manager
*
* @return EventManager
*/
private function getEventManager()
{
$evm = new EventManager;
return $evm;
}
/**
* Get annotation mapping configuration
*
* @return Doctrine\ORM\Configuration
*/
private function getMockAnnotatedConfig()
{
$config = $this->getMock('Doctrine\\ODM\\MongoDB\\Configuration');
$config->expects($this->once())
->method('getProxyDir')
->will($this->returnValue(__DIR__.'/../../temp'));
$config->expects($this->once())
->method('getProxyNamespace')
->will($this->returnValue('Proxy'));
$config->expects($this->once())
->method('getHydratorDir')
->will($this->returnValue(__DIR__.'/../../temp'));
$config->expects($this->once())
->method('getHydratorNamespace')
->will($this->returnValue('Hydrator'));
$config->expects($this->any())
->method('getDefaultDB')
->will($this->returnValue('knp_pager_tests'));
$config->expects($this->once())
->method('getAutoGenerateProxyClasses')
->will($this->returnValue(true));
$config->expects($this->once())
->method('getAutoGenerateHydratorClasses')
->will($this->returnValue(true));
$config->expects($this->once())
->method('getClassMetadataFactoryName')
->will($this->returnValue('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadataFactory'));
$config->expects($this->any())
->method('getMongoCmd')
->will($this->returnValue('$'));
$config
->expects($this->any())
->method('getDefaultCommitOptions')
->will($this->returnValue(array('safe' => true)))
;
$mappingDriver = $this->getMetadataDriverImplementation();
$config->expects($this->any())
->method('getMetadataDriverImpl')
->will($this->returnValue($mappingDriver));
return $config;
}
}