Skip to content

Commit

Permalink
Update CouchDB Bundle to latest Symfony gadgets and standalone testing
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Nov 26, 2011
1 parent 7c3393d commit 66ebdb3
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
composer.lock
vendor
46 changes: 46 additions & 0 deletions DoctrineCouchDBBundle.php
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;

class DoctrineCouchDBBundle extends Bundle
{
Expand All @@ -26,12 +27,57 @@ public function build(ContainerBuilder $container)
parent::build($container);

$container->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
$container->addCompilerPass(new DoctrineValidationPass('couchdb'));
}

public function boot()
{
// force Doctrine annotations to be loaded
// should be removed when a better solution is found in Doctrine
class_exists('Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver');

// Register an autoloader for proxies to avoid issues when unserializing them
// when the ORM is used.
if ($this->container->hasParameter('doctrine_couchdb.odm.proxy_namespace')) {
$namespace = $this->container->getParameter('doctrine_couchdb.odm.proxy_namespace');
$dir = $this->container->getParameter('doctrine_couchdb.odm.proxy_dir');
$container = $this->container;

spl_autoload_register(function($class) use ($namespace, $dir, $container) {
if (0 === strpos($class, $namespace)) {
$className = substr($class, strlen($namespace) +1);
$file = $dir.DIRECTORY_SEPARATOR.$className.'.php';

if (!is_file($file) && $container->getParameter('kernel.debug')) {
$originalClassName = substr($className, 0, -5);
$registry = $container->get('doctrine_couchdb');

// Tries to auto-generate the proxy file
foreach ($registry->getObjectManagers() as $manager) {

if ($manager->getConfiguration()->getAutoGenerateProxyClasses()) {
$classes = $manager->getMetadataFactory()->getAllMetadata();

foreach ($classes as $class) {
$name = str_replace('\\', '', $class->name);

if ($name == $originalClassName) {
$manager->getProxyFactory()->generateProxyClasses(array($class));
}
}
}
}

clearstatcache($file);

if (!is_file($file)) {
throw new \RuntimeException(sprintf('The proxy file "%s" does not exist. If you still have objects serialized in the session, you need to clear the session manually.', $file));
}
}

require $file;
}
});
}
}
}
11 changes: 9 additions & 2 deletions Tests/BundleTest.php
Expand Up @@ -16,16 +16,23 @@

class BundleTest extends TestCase
{
public function testRegisterEventListener()
public function testRegisterCompilerPasses()
{
$bundle = new DoctrineCouchDBBundle();
$builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
$builder->expects($this->once())

$builder->expects($this->at(0))
->method('addCompilerPass')
->with(
$this->isInstanceOf('Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass'),
$this->equalTo(PassConfig::TYPE_BEFORE_OPTIMIZATION)
);
$builder->expects($this->at(1))
->method('addCompilerPass')
->with(
$this->isInstanceOf('Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass'),
$this->equalTo(PassConfig::TYPE_BEFORE_OPTIMIZATION)
);

$bundle->build($builder);
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -28,7 +28,7 @@ public function setUp()
public function testEmptyConfig()
{
$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidConfigurationException',
'The child node "client" at path "doctrine_couchdb" must be configured.');
'The child node "client" at path "doctrine_couch_db" must be configured.');
$config = $this->processor->processConfiguration($this->config, array());
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/bootstrap.php
@@ -0,0 +1,19 @@
<?php

if (!@include __DIR__ . '/../vendor/.composer/autoload.php') {
die("You must set up the project dependencies, run the following commands:
wget http://getcomposer.org/composer.phar
php composer.phar install
");
}

spl_autoload_register(function($class) {
if (0 === strpos($class, 'Doctrine\\Bundle\\CouchDBBundle')) {
$path = __DIR__.'/../'.implode('/', array_slice(explode('\\', $class), 3)).'.php';
if (!stream_resolve_include_path($path)) {
return false;
}
require_once $path;
return true;
}
});
13 changes: 9 additions & 4 deletions composer.json
@@ -1,6 +1,6 @@
{
"name": "doctrine/couchdb-odm-bundle",
"type": "library",
"type": "symfony-bundle",
"description": "Symfony2 Doctrine CouchDB Bundle",
"keywords": ["persistence", "couchdb", "symfony"],
"homepage": "http://www.doctrine-project.org",
Expand All @@ -11,7 +11,12 @@
],
"require": {
"php": ">=5.3.2",
"doctrine/couchdb-odm": ">=2.2.0",
"symfony/framework-bundle": ">=2.1.0"
}
"doctrine/couchdb-odm": ">=2.0",
"symfony/symfony": "2.1-dev",
"symfony/doctrine-bridge": "2.1-dev"
},
"autoload": {
"psr-0": { "Doctrine\\Bundle\\DoctrineCouchDBBundle": "" }
},
"target-dir": "Doctrine/Bundle/DoctrineCouchDBBundle"
}
35 changes: 35 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="Tests/bootstrap.php"
>
<testsuites>
<testsuite name="DoctrineCouchDBBundle for the Symfony Framework">
<directory>./Tests</directory>
</testsuite>
</testsuites>

<groups>
<exclude>
<group>benchmark</group>
</exclude>
</groups>

<filter>
<whitelist>
<directory>.</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

0 comments on commit 66ebdb3

Please sign in to comment.