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

Mongodb transport #430

Merged
merged 25 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions pkg/mongodb/Client/MongodbDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class MongodbDriver implements DriverInterface
];

/**
* @param MongodbContext $context
* @param Config $config
* @param MongodbContext $context
* @param Config $config
* @param QueueMetaRegistry $queueMetaRegistry
*/
public function __construct(MongodbContext $context, Config $config, QueueMetaRegistry $queueMetaRegistry)
Expand Down Expand Up @@ -161,7 +161,7 @@ public function setupBroker(LoggerInterface $logger = null)
{
$logger = $logger ?: new NullLogger();
$log = function ($text, ...$args) use ($logger) {
$logger->debug(sprintf('[MongodbDriver] ' . $text, ...$args));
$logger->debug(sprintf('[MongodbDriver] '.$text, ...$args));
};
$contextConfig = $this->context->getConfig();
$log('Creating database and collection: "%s" "%s"', $contextConfig['dbname'], $contextConfig['collection_name']);
Expand Down
123 changes: 123 additions & 0 deletions pkg/mongodb/Symfony/MongodbTransportFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Enqueue\Mongodb\Symfony;

use Enqueue\Mongodb\Client\MongodbDriver;
use Enqueue\Mongodb\MongodbConnectionFactory;
use Enqueue\Mongodb\MongodbContext;
use Enqueue\Symfony\DriverFactoryInterface;
use Enqueue\Symfony\TransportFactoryInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class MongodbTransportFactory implements TransportFactoryInterface, DriverFactoryInterface
{
/**
* @var string
*/
private $name;

/**
* @param string $name
*/
public function __construct($name = 'Mongodb')
{
$this->name = $name;
}

/**
* {@inheritdoc}
*/
public function addConfiguration(ArrayNodeDefinition $builder)
{
$builder
->beforeNormalization()
->ifString()
->then(function ($v) {
return ['dsn' => $v];
})
->end()
->children()
->scalarNode('dsn')
->info('The Mongodb DSN. Other parameters are ignored if set')
->end()
->scalarNode('dbname')
->defaultValue('enqueue')
->info('Database name.')
->end()
->scalarNode('collection_name')
->defaultValue('enqueue')
->info('Collection')
->end()
->integerNode('polling_interval')
->defaultValue(1000)
->min(100)
->info('How often query for new messages.')
->end()
;
}

/**
* {@inheritdoc}
*/
public function createConnectionFactory(ContainerBuilder $container, array $config)
{
if (false == empty($config['dsn'])) {
$factory = new Definition(MongodbConnectionFactory::class);
$factory->setArguments([$config]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I remember Definition takes an array of arguments as a second constructor argument.

} else {
throw new \LogicException('Set "dsn" options when you want ot use Mongodb.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of this add required to ->scalarNode('dsn')

}

$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
$container->setDefinition($factoryId, $factory);

return $factoryId;
}

/**
* {@inheritdoc}
*/
public function createContext(ContainerBuilder $container, array $config)
{
$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());

$context = new Definition(MongodbContext::class);
$context->setPublic(true);
$context->setFactory([new Reference($factoryId), 'createContext']);

$contextId = sprintf('enqueue.transport.%s.context', $this->getName());
$container->setDefinition($contextId, $context);

return $contextId;
}

/**
* {@inheritdoc}
*/
public function createDriver(ContainerBuilder $container, array $config)
{
$driver = new Definition(MongodbDriver::class);
$driver->setPublic(true);
$driver->setArguments([
new Reference(sprintf('enqueue.transport.%s.context', $this->getName())),
new Reference('enqueue.client.config'),
new Reference('enqueue.client.meta.queue_meta_registry'),
]);

$driverId = sprintf('enqueue.client.%s.driver', $this->getName());
$container->setDefinition($driverId, $driver);

return $driverId;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
}
2 changes: 0 additions & 2 deletions pkg/mongodb/Tests/MongodbConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public function testCouldBeConstructedWithEmptyConfiguration()
{
$params = [
'uri' => 'mongodb://127.0.0.1/',
'uriOptions' => [],
'driverOptions' => [],
];

$factory = new MongodbConnectionFactory();
Expand Down
161 changes: 161 additions & 0 deletions pkg/mongodb/Tests/Symfony/MongodbTransportFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace Enqueue\Mongodb\Tests\Symfony;

use Enqueue\Mongodb\Client\MongodbDriver;
use Enqueue\Mongodb\MongodbConnectionFactory;
use Enqueue\Mongodb\Symfony\MongodbTransportFactory;
use Enqueue\Symfony\TransportFactoryInterface;
use Enqueue\Test\ClassExtensionTrait;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class MongodbTransportFactoryTest extends \PHPUnit_Framework_TestCase
{
use ClassExtensionTrait;

public function testShouldImplementTransportFactoryInterface()
{
$this->assertClassImplements(TransportFactoryInterface::class, MongodbTransportFactory::class);
}

public function testCouldBeConstructedWithDefaultName()
{
$transport = new MongodbTransportFactory();

$this->assertEquals('Mongodb', $transport->getName());
}

public function testCouldBeConstructedWithCustomName()
{
$transport = new MongodbTransportFactory('theCustomName');

$this->assertEquals('theCustomName', $transport->getName());
}

public function testShouldAllowAddConfiguration()
{
$transport = new MongodbTransportFactory();
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');

$transport->addConfiguration($rootNode);
$processor = new Processor();
$config = $processor->process($tb->buildTree(), [[
'dsn' => 'mongodb://127.0.0.1/',
]]);

$this->assertEquals([
'dsn' => 'mongodb://127.0.0.1/',
'dbname' => 'enqueue',
'collection_name' => 'enqueue',
'polling_interval' => 1000,
], $config);
}

public function testShouldAllowAddConfigurationAsString()
{
$transport = new MongodbTransportFactory();
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');

$transport->addConfiguration($rootNode);
$processor = new Processor();
$config = $processor->process($tb->buildTree(), ['mysqlDSN']);

$this->assertEquals([
'dsn' => 'mysqlDSN',
'dbname' => 'enqueue',
'collection_name' => 'enqueue',
'polling_interval' => 1000,
], $config);
}

public function testShouldCreateMongodbConnectionFactory()
{
$container = new ContainerBuilder();

$transport = new MongodbTransportFactory();

$serviceId = $transport->createConnectionFactory($container, [
'dsn' => 'mysqlDSN',
'dbname' => 'enqueue',
'collection_name' => 'enqueue',
'polling_interval' => 1000,
]);

$this->assertTrue($container->hasDefinition($serviceId));
$factory = $container->getDefinition($serviceId);
$this->assertEquals(MongodbConnectionFactory::class, $factory->getClass());

$this->assertSame([
'dsn' => 'mysqlDSN',
'dbname' => 'enqueue',
'collection_name' => 'enqueue',
'polling_interval' => 1000,
], $factory->getArgument(0));
}

public function testShouldCreateConnectionFactoryFromDsnString()
{
$container = new ContainerBuilder();

$transport = new MongodbTransportFactory();

$serviceId = $transport->createConnectionFactory($container, [
'dsn' => 'theDSN',
'connection' => [],
'lazy' => true,
'table_name' => 'enqueue',
'polling_interval' => 1000,
]);

$this->assertTrue($container->hasDefinition($serviceId));
$factory = $container->getDefinition($serviceId);
$this->assertEquals(MongodbConnectionFactory::class, $factory->getClass());
$this->assertSame('theDSN', $factory->getArgument(0)['dsn']);
}

public function testShouldCreateContext()
{
$container = new ContainerBuilder();

$transport = new MongodbTransportFactory();

$serviceId = $transport->createContext($container, []);

$this->assertEquals('enqueue.transport.Mongodb.context', $serviceId);
$this->assertTrue($container->hasDefinition($serviceId));

$context = $container->getDefinition('enqueue.transport.Mongodb.context');
$this->assertInstanceOf(Reference::class, $context->getFactory()[0]);
$this->assertEquals('enqueue.transport.Mongodb.connection_factory', (string) $context->getFactory()[0]);
$this->assertEquals('createContext', $context->getFactory()[1]);
}

public function testShouldCreateDriver()
{
$container = new ContainerBuilder();

$transport = new MongodbTransportFactory();

$serviceId = $transport->createDriver($container, []);

$this->assertEquals('enqueue.client.Mongodb.driver', $serviceId);
$this->assertTrue($container->hasDefinition($serviceId));

$driver = $container->getDefinition($serviceId);
$this->assertSame(MongodbDriver::class, $driver->getClass());

$this->assertInstanceOf(Reference::class, $driver->getArgument(0));
$this->assertEquals('enqueue.transport.Mongodb.context', (string) $driver->getArgument(0));

$this->assertInstanceOf(Reference::class, $driver->getArgument(1));
$this->assertEquals('enqueue.client.config', (string) $driver->getArgument(1));

$this->assertInstanceOf(Reference::class, $driver->getArgument(2));
$this->assertEquals('enqueue.client.meta.queue_meta_registry', (string) $driver->getArgument(2));
}
}