Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate entity manager factory #61

Open
wants to merge 1 commit into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ public function register(\Pimple $app)
$app['orm.ems.options.initializer']();

$ems = new \Pimple();
foreach ($app['orm.ems.options'] as $name => $options) {
$ems[$name] = $app->share(function() use ($name, $app) {
return $app['orm.ems.factory'][$name]();
});
}

return $ems;
});

$app['orm.ems.factory'] = $app->share(function($app) {
$app['orm.ems.options.initializer']();

$factory = new \Pimple();
foreach ($app['orm.ems.options'] as $name => $options) {
if ($app['orm.ems.default'] === $name) {
// we use shortcuts here in case the default has been overridden
Expand All @@ -102,7 +115,7 @@ public function register(\Pimple $app)
$config = $app['orm.ems.config'][$name];
}

$ems[$name] = $app->share(function ($ems) use ($app, $options, $config) {
$factory[$name] = $app->protect(function () use ($app, $options, $config) {
return EntityManager::create(
$app['dbs'][$options['connection']],
$config,
Expand All @@ -111,7 +124,7 @@ public function register(\Pimple $app)
});
}

return $ems;
return $factory;
});

$app['orm.ems.config'] = $app->share(function($app) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ protected function createMockDefaultApp()
return $app;
}

/**
* Test entity manager factory.
*/
public function testEntityManagerFactory()
{
$app = $this->createMockDefaultApp();

$doctrineOrmServiceProvider = new DoctrineOrmServiceProvider;
$doctrineOrmServiceProvider->register($app);

// Only one instance is created.
$em1 = $app['orm.em'];
$this->assertSame($em1, $app['orm.em']);
$this->assertSame($em1, $app['orm.ems']['default']);

// Reset the entity manager.
$app['orm.em'] = $app['orm.ems']['default'] = $app['orm.ems.factory'][$app['orm.ems.default']]();

// One more instance is created.
$em2 = $app['orm.em'];
$this->assertInstanceOf('Doctrine\ORM\EntityManager', $em2);
$this->assertNotSame($em1, $em2);
$this->assertSame($em2, $app['orm.em']);
$this->assertSame($em2, $app['orm.ems']['default']);
}

/**
* Test registration (test expected class for default implementations)
*/
Expand All @@ -61,7 +87,7 @@ public function testRegisterDefaultImplementations()
$doctrineOrmServiceProvider = new DoctrineOrmServiceProvider;
$doctrineOrmServiceProvider->register($app);

$this->assertEquals($app['orm.em'], $app['orm.ems']['default']);
$this->assertSame($app['orm.em'], $app['orm.ems']['default']);
$this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $app['orm.em.config']->getQueryCacheImpl());
$this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $app['orm.em.config']->getResultCacheImpl());
$this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $app['orm.em.config']->getMetadataCacheImpl());
Expand Down Expand Up @@ -91,7 +117,7 @@ public function testRegisterDefinedImplementations()
$doctrineOrmServiceProvider = new DoctrineOrmServiceProvider;
$doctrineOrmServiceProvider->register($app);

$this->assertEquals($app['orm.em'], $app['orm.ems']['default']);
$this->assertSame($app['orm.em'], $app['orm.ems']['default']);
$this->assertEquals($queryCache, $app['orm.em.config']->getQueryCacheImpl());
$this->assertEquals($resultCache, $app['orm.em.config']->getResultCacheImpl());
$this->assertEquals($metadataCache, $app['orm.em.config']->getMetadataCacheImpl());
Expand Down