-
Notifications
You must be signed in to change notification settings - Fork 439
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
Mongodb transport #430
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
07ed3ba
Mongo db transport
turboboy88 4346e9f
client
turboboy88 83d4b85
smfony transport
turboboy88 2e8f931
fix
turboboy88 7e9f62d
develop
turboboy88 9cbbce8
develop
turboboy88 947e00f
Update .travis.yml
makasim c17e79f
Merge branch 'master' into mongodb_transport
makasim a7ea2f1
update enqueue/dev docker image
makasim e924c23
debug travis.
makasim e8de54a
use $HOME
makasim f76e48d
add group
makasim aa3178f
add license, readme, phpunit cfg
makasim f460e6c
mongolib ^1.2, ext-mongodb ^1.3
makasim 41f2b47
require php7.0
makasim a9fd835
[doc][skip ci] add docs.
makasim 368b608
[symfony] add mongodb to default transport and dsn_to_function
makasim 0f8d1dc
run mongodb tests.
makasim 41254f9
fix tests.
makasim f011a8b
fix tests.
makasim b128a65
fix tests
makasim e15e92f
fix test.
makasim 3b8c997
Add mongodb transport.
makasim 1b23270
mongodb transport, fixes
makasim c407d24
revert sf version check
makasim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} else { | ||
throw new \LogicException('Set "dsn" options when you want ot use Mongodb.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
pkg/mongodb/Tests/Symfony/MongodbTransportFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.