|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Mongodb\Tests\Symfony; |
| 4 | + |
| 5 | +use Enqueue\Mongodb\Client\MongodbDriver; |
| 6 | +use Enqueue\Mongodb\MongodbConnectionFactory; |
| 7 | +use Enqueue\Mongodb\Symfony\MongodbTransportFactory; |
| 8 | +use Enqueue\Symfony\TransportFactoryInterface; |
| 9 | +use Enqueue\Test\ClassExtensionTrait; |
| 10 | +use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
| 11 | +use Symfony\Component\Config\Definition\Processor; |
| 12 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 13 | +use Symfony\Component\DependencyInjection\Reference; |
| 14 | + |
| 15 | +class MongodbTransportFactoryTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + use ClassExtensionTrait; |
| 18 | + |
| 19 | + public function testShouldImplementTransportFactoryInterface() |
| 20 | + { |
| 21 | + $this->assertClassImplements(TransportFactoryInterface::class, MongodbTransportFactory::class); |
| 22 | + } |
| 23 | + |
| 24 | + public function testCouldBeConstructedWithDefaultName() |
| 25 | + { |
| 26 | + $transport = new MongodbTransportFactory(); |
| 27 | + |
| 28 | + $this->assertEquals('Mongodb', $transport->getName()); |
| 29 | + } |
| 30 | + |
| 31 | + public function testCouldBeConstructedWithCustomName() |
| 32 | + { |
| 33 | + $transport = new MongodbTransportFactory('theCustomName'); |
| 34 | + |
| 35 | + $this->assertEquals('theCustomName', $transport->getName()); |
| 36 | + } |
| 37 | + |
| 38 | + public function testShouldAllowAddConfiguration() |
| 39 | + { |
| 40 | + $transport = new MongodbTransportFactory(); |
| 41 | + $tb = new TreeBuilder(); |
| 42 | + $rootNode = $tb->root('foo'); |
| 43 | + |
| 44 | + $transport->addConfiguration($rootNode); |
| 45 | + $processor = new Processor(); |
| 46 | + $config = $processor->process($tb->buildTree(), [[ |
| 47 | + 'dsn' => 'mongodb://127.0.0.1/', |
| 48 | + ]]); |
| 49 | + |
| 50 | + $this->assertEquals([ |
| 51 | + 'dsn' => 'mongodb://127.0.0.1/', |
| 52 | + 'dbname' => 'enqueue', |
| 53 | + 'collection_name' => 'enqueue', |
| 54 | + 'polling_interval' => 1000, |
| 55 | + ], $config); |
| 56 | + } |
| 57 | + |
| 58 | + public function testShouldAllowAddConfigurationAsString() |
| 59 | + { |
| 60 | + $transport = new MongodbTransportFactory(); |
| 61 | + $tb = new TreeBuilder(); |
| 62 | + $rootNode = $tb->root('foo'); |
| 63 | + |
| 64 | + $transport->addConfiguration($rootNode); |
| 65 | + $processor = new Processor(); |
| 66 | + $config = $processor->process($tb->buildTree(), ['mysqlDSN']); |
| 67 | + |
| 68 | + $this->assertEquals([ |
| 69 | + 'dsn' => 'mysqlDSN', |
| 70 | + 'dbname' => 'enqueue', |
| 71 | + 'collection_name' => 'enqueue', |
| 72 | + 'polling_interval' => 1000, |
| 73 | + ], $config); |
| 74 | + } |
| 75 | + |
| 76 | + public function testShouldCreateMongodbConnectionFactory() |
| 77 | + { |
| 78 | + $container = new ContainerBuilder(); |
| 79 | + |
| 80 | + $transport = new MongodbTransportFactory(); |
| 81 | + |
| 82 | + $serviceId = $transport->createConnectionFactory($container, [ |
| 83 | + 'dsn' => 'mysqlDSN', |
| 84 | + 'dbname' => 'enqueue', |
| 85 | + 'collection_name' => 'enqueue', |
| 86 | + 'polling_interval' => 1000, |
| 87 | + ]); |
| 88 | + |
| 89 | + $this->assertTrue($container->hasDefinition($serviceId)); |
| 90 | + $factory = $container->getDefinition($serviceId); |
| 91 | + $this->assertEquals(MongodbConnectionFactory::class, $factory->getClass()); |
| 92 | + |
| 93 | + $this->assertSame([ |
| 94 | + 'dsn' => 'mysqlDSN', |
| 95 | + 'dbname' => 'enqueue', |
| 96 | + 'collection_name' => 'enqueue', |
| 97 | + 'polling_interval' => 1000, |
| 98 | + ], $factory->getArgument(0)); |
| 99 | + } |
| 100 | + |
| 101 | + public function testShouldCreateConnectionFactoryFromDsnString() |
| 102 | + { |
| 103 | + $container = new ContainerBuilder(); |
| 104 | + |
| 105 | + $transport = new MongodbTransportFactory(); |
| 106 | + |
| 107 | + $serviceId = $transport->createConnectionFactory($container, [ |
| 108 | + 'dsn' => 'theDSN', |
| 109 | + 'connection' => [], |
| 110 | + 'lazy' => true, |
| 111 | + 'table_name' => 'enqueue', |
| 112 | + 'polling_interval' => 1000, |
| 113 | + ]); |
| 114 | + |
| 115 | + $this->assertTrue($container->hasDefinition($serviceId)); |
| 116 | + $factory = $container->getDefinition($serviceId); |
| 117 | + $this->assertEquals(MongodbConnectionFactory::class, $factory->getClass()); |
| 118 | + $this->assertSame('theDSN', $factory->getArgument(0)['dsn']); |
| 119 | + } |
| 120 | + |
| 121 | + public function testShouldCreateContext() |
| 122 | + { |
| 123 | + $container = new ContainerBuilder(); |
| 124 | + |
| 125 | + $transport = new MongodbTransportFactory(); |
| 126 | + |
| 127 | + $serviceId = $transport->createContext($container, []); |
| 128 | + |
| 129 | + $this->assertEquals('enqueue.transport.Mongodb.context', $serviceId); |
| 130 | + $this->assertTrue($container->hasDefinition($serviceId)); |
| 131 | + |
| 132 | + $context = $container->getDefinition('enqueue.transport.Mongodb.context'); |
| 133 | + $this->assertInstanceOf(Reference::class, $context->getFactory()[0]); |
| 134 | + $this->assertEquals('enqueue.transport.Mongodb.connection_factory', (string) $context->getFactory()[0]); |
| 135 | + $this->assertEquals('createContext', $context->getFactory()[1]); |
| 136 | + } |
| 137 | + |
| 138 | + public function testShouldCreateDriver() |
| 139 | + { |
| 140 | + $container = new ContainerBuilder(); |
| 141 | + |
| 142 | + $transport = new MongodbTransportFactory(); |
| 143 | + |
| 144 | + $serviceId = $transport->createDriver($container, []); |
| 145 | + |
| 146 | + $this->assertEquals('enqueue.client.Mongodb.driver', $serviceId); |
| 147 | + $this->assertTrue($container->hasDefinition($serviceId)); |
| 148 | + |
| 149 | + $driver = $container->getDefinition($serviceId); |
| 150 | + $this->assertSame(MongodbDriver::class, $driver->getClass()); |
| 151 | + |
| 152 | + $this->assertInstanceOf(Reference::class, $driver->getArgument(0)); |
| 153 | + $this->assertEquals('enqueue.transport.Mongodb.context', (string) $driver->getArgument(0)); |
| 154 | + |
| 155 | + $this->assertInstanceOf(Reference::class, $driver->getArgument(1)); |
| 156 | + $this->assertEquals('enqueue.client.config', (string) $driver->getArgument(1)); |
| 157 | + |
| 158 | + $this->assertInstanceOf(Reference::class, $driver->getArgument(2)); |
| 159 | + $this->assertEquals('enqueue.client.meta.queue_meta_registry', (string) $driver->getArgument(2)); |
| 160 | + } |
| 161 | +} |
0 commit comments