Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AbstractDoctrineExtensionTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractDoctrineExtensionTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | abstract class AbstractDoctrineExtensionTest extends TestCase |
||
| 25 | { |
||
| 26 | abstract protected function loadFromFile(ContainerBuilder $container, $file); |
||
|
|
|||
| 27 | |||
| 28 | public function testDbalLoadFromXmlMultipleConnections() |
||
| 29 | { |
||
| 30 | $container = $this->loadContainer('dbal_service_multiple_connections'); |
||
| 31 | |||
| 32 | // doctrine.dbal.mysql_connection |
||
| 33 | $config = $container->getDefinition('doctrine.dbal.mysql_connection')->getArgument(0); |
||
| 34 | |||
| 35 | $this->assertEquals('mysql_s3cr3t', $config['password']); |
||
| 36 | $this->assertEquals('mysql_user', $config['user']); |
||
| 37 | $this->assertEquals('mysql_db', $config['dbname']); |
||
| 38 | $this->assertEquals('/path/to/mysqld.sock', $config['unix_socket']); |
||
| 39 | |||
| 40 | // doctrine.dbal.sqlite_connection |
||
| 41 | $config = $container->getDefinition('doctrine.dbal.sqlite_connection')->getArgument(0); |
||
| 42 | $this->assertSame('pdo_sqlite', $config['driver']); |
||
| 43 | $this->assertSame('sqlite_db', $config['dbname']); |
||
| 44 | $this->assertSame('sqlite_user', $config['user']); |
||
| 45 | $this->assertSame('sqlite_s3cr3t', $config['password']); |
||
| 46 | $this->assertSame('/tmp/db.sqlite', $config['path']); |
||
| 47 | $this->assertTrue($config['memory']); |
||
| 48 | |||
| 49 | // doctrine.dbal.oci8_connection |
||
| 50 | $config = $container->getDefinition('doctrine.dbal.oci_connection')->getArgument(0); |
||
| 51 | $this->assertSame('oci8', $config['driver']); |
||
| 52 | $this->assertSame('oracle_db', $config['dbname']); |
||
| 53 | $this->assertSame('oracle_user', $config['user']); |
||
| 54 | $this->assertSame('oracle_s3cr3t', $config['password']); |
||
| 55 | $this->assertSame('oracle_service', $config['servicename']); |
||
| 56 | $this->assertTrue($config['service']); |
||
| 57 | $this->assertTrue($config['pooled']); |
||
| 58 | $this->assertSame('utf8', $config['charset']); |
||
| 59 | |||
| 60 | // doctrine.dbal.ibmdb2_connection |
||
| 61 | $config = $container->getDefinition('doctrine.dbal.ibmdb2_connection')->getArgument(0); |
||
| 62 | $this->assertSame('ibm_db2', $config['driver']); |
||
| 63 | $this->assertSame('ibmdb2_db', $config['dbname']); |
||
| 64 | $this->assertSame('ibmdb2_user', $config['user']); |
||
| 65 | $this->assertSame('ibmdb2_s3cr3t', $config['password']); |
||
| 66 | $this->assertSame('TCPIP', $config['protocol']); |
||
| 67 | |||
| 68 | // doctrine.dbal.pgsql_connection |
||
| 69 | $config = $container->getDefinition('doctrine.dbal.pgsql_connection')->getArgument(0); |
||
| 70 | $this->assertSame('pdo_pgsql', $config['driver']); |
||
| 71 | $this->assertSame('pgsql_schema', $config['dbname']); |
||
| 72 | $this->assertSame('pgsql_user', $config['user']); |
||
| 73 | $this->assertSame('pgsql_s3cr3t', $config['password']); |
||
| 74 | $this->assertSame('pgsql_db', $config['default_dbname']); |
||
| 75 | $this->assertSame('require', $config['sslmode']); |
||
| 76 | $this->assertSame('postgresql-ca.pem', $config['sslrootcert']); |
||
| 77 | $this->assertSame('postgresql-cert.pem', $config['sslcert']); |
||
| 78 | $this->assertSame('postgresql-key.pem', $config['sslkey']); |
||
| 79 | $this->assertSame('postgresql.crl', $config['sslcrl']); |
||
| 80 | $this->assertSame('utf8', $config['charset']); |
||
| 81 | |||
| 82 | // doctrine.dbal.sqlanywhere_connection |
||
| 83 | $config = $container->getDefinition('doctrine.dbal.sqlanywhere_connection')->getArgument(0); |
||
| 84 | $this->assertSame('sqlanywhere', $config['driver']); |
||
| 85 | $this->assertSame('localhost', $config['host']); |
||
| 86 | $this->assertSame(2683, $config['port']); |
||
| 87 | $this->assertSame('sqlanywhere_server', $config['server']); |
||
| 88 | $this->assertSame('sqlanywhere_db', $config['dbname']); |
||
| 89 | $this->assertSame('sqlanywhere_user', $config['user']); |
||
| 90 | $this->assertSame('sqlanywhere_s3cr3t', $config['password']); |
||
| 91 | $this->assertTrue($config['persistent']); |
||
| 92 | $this->assertSame('utf8', $config['charset']); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function testDbalLoadFromXmlSingleConnections() |
||
| 96 | { |
||
| 97 | $container = $this->loadContainer('dbal_service_single_connection'); |
||
| 98 | |||
| 99 | // doctrine.dbal.mysql_connection |
||
| 100 | $config = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0); |
||
| 101 | |||
| 102 | $this->assertEquals('mysql_s3cr3t', $config['password']); |
||
| 103 | $this->assertEquals('mysql_user', $config['user']); |
||
| 104 | $this->assertEquals('mysql_db', $config['dbname']); |
||
| 105 | $this->assertEquals('/path/to/mysqld.sock', $config['unix_socket']); |
||
| 106 | $this->assertEquals('5.6.20', $config['serverVersion']); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testDbalLoadSingleMasterSlaveConnection() |
||
| 110 | { |
||
| 111 | $container = $this->loadContainer('dbal_service_single_master_slave_connection'); |
||
| 112 | |||
| 113 | // doctrine.dbal.mysql_connection |
||
| 114 | $param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0); |
||
| 115 | |||
| 116 | $this->assertEquals('Doctrine\\DBAL\\Connections\\MasterSlaveConnection', $param['wrapperClass']); |
||
| 117 | $this->assertTrue($param['keepSlave']); |
||
| 118 | $this->assertEquals( |
||
| 119 | [ |
||
| 120 | 'user' => 'mysql_user', |
||
| 121 | 'password' => 'mysql_s3cr3t', |
||
| 122 | 'port' => null, |
||
| 123 | 'dbname' => 'mysql_db', |
||
| 124 | 'host' => 'localhost', |
||
| 125 | 'unix_socket' => '/path/to/mysqld.sock', |
||
| 126 | ], |
||
| 127 | $param['master'] |
||
| 128 | ); |
||
| 129 | $this->assertEquals( |
||
| 130 | [ |
||
| 131 | 'user' => 'slave_user', |
||
| 132 | 'password' => 'slave_s3cr3t', |
||
| 133 | 'port' => null, |
||
| 134 | 'dbname' => 'slave_db', |
||
| 135 | 'host' => 'localhost', |
||
| 136 | 'unix_socket' => '/path/to/mysqld_slave.sock', |
||
| 137 | ], |
||
| 138 | $param['slaves']['slave1'] |
||
| 139 | ); |
||
| 140 | $this->assertEquals(['engine' => 'InnoDB'], $param['defaultTableOptions']); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function testDbalLoadPoolShardingConnection() |
||
| 144 | { |
||
| 145 | $container = $this->loadContainer('dbal_service_pool_sharding_connection'); |
||
| 146 | |||
| 147 | // doctrine.dbal.mysql_connection |
||
| 148 | $param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0); |
||
| 149 | |||
| 150 | $this->assertEquals('Doctrine\\DBAL\\Sharding\\PoolingShardConnection', $param['wrapperClass']); |
||
| 151 | $this->assertEquals(new Reference('foo.shard_choser'), $param['shardChoser']); |
||
| 152 | $this->assertEquals( |
||
| 153 | [ |
||
| 154 | 'user' => 'mysql_user', |
||
| 155 | 'password' => 'mysql_s3cr3t', |
||
| 156 | 'port' => null, |
||
| 157 | 'dbname' => 'mysql_db', |
||
| 158 | 'host' => 'localhost', |
||
| 159 | 'unix_socket' => '/path/to/mysqld.sock', |
||
| 160 | ], |
||
| 161 | $param['global'] |
||
| 162 | ); |
||
| 163 | $this->assertEquals( |
||
| 164 | [ |
||
| 165 | 'user' => 'shard_user', |
||
| 166 | 'password' => 'shard_s3cr3t', |
||
| 167 | 'port' => null, |
||
| 168 | 'dbname' => 'shard_db', |
||
| 169 | 'host' => 'localhost', |
||
| 170 | 'unix_socket' => '/path/to/mysqld_shard.sock', |
||
| 171 | 'id' => 1, |
||
| 172 | ], |
||
| 173 | $param['shards'][0] |
||
| 174 | ); |
||
| 175 | $this->assertEquals(['engine' => 'InnoDB'], $param['defaultTableOptions']); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function testDbalLoadSavepointsForNestedTransactions() |
||
| 179 | { |
||
| 180 | $container = $this->loadContainer('dbal_savepoints'); |
||
| 181 | |||
| 182 | $calls = $container->getDefinition('doctrine.dbal.savepoints_connection')->getMethodCalls(); |
||
| 183 | $this->assertCount(1, $calls); |
||
| 184 | $this->assertEquals('setNestTransactionsWithSavepoints', $calls[0][0]); |
||
| 185 | $this->assertTrue($calls[0][1][0]); |
||
| 186 | |||
| 187 | $calls = $container->getDefinition('doctrine.dbal.nosavepoints_connection')->getMethodCalls(); |
||
| 188 | $this->assertCount(0, $calls); |
||
| 189 | |||
| 190 | $calls = $container->getDefinition('doctrine.dbal.notset_connection')->getMethodCalls(); |
||
| 191 | $this->assertCount(0, $calls); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function testLoadSimpleSingleConnection() |
||
| 195 | { |
||
| 196 | $container = $this->loadContainer('orm_service_simple_single_entity_manager'); |
||
| 197 | |||
| 198 | $definition = $container->getDefinition('doctrine.dbal.default_connection'); |
||
| 199 | |||
| 200 | $this->assertDICConstructorArguments($definition, [ |
||
| 201 | [ |
||
| 202 | 'dbname' => 'db', |
||
| 203 | 'host' => 'localhost', |
||
| 204 | 'port' => null, |
||
| 205 | 'user' => 'root', |
||
| 206 | 'password' => null, |
||
| 207 | 'driver' => 'pdo_mysql', |
||
| 208 | 'driverOptions' => [], |
||
| 209 | 'defaultTableOptions' => [], |
||
| 210 | ], |
||
| 211 | new Reference('doctrine.dbal.default_connection.configuration'), |
||
| 212 | new Reference('doctrine.dbal.default_connection.event_manager'), |
||
| 213 | [], |
||
| 214 | ]); |
||
| 215 | |||
| 216 | $definition = $container->getDefinition('doctrine.orm.default_entity_manager'); |
||
| 217 | $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass()); |
||
| 218 | $this->assertEquals(['%doctrine.orm.entity_manager.class%', 'create'], $definition->getFactory()); |
||
| 219 | |||
| 220 | $this->assertDICConstructorArguments($definition, [ |
||
| 221 | new Reference('doctrine.dbal.default_connection'), |
||
| 222 | new Reference('doctrine.orm.default_configuration'), |
||
| 223 | ]); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * The PDO driver doesn't require a database name to be to set when connecting to a database server |
||
| 228 | */ |
||
| 229 | public function testLoadSimpleSingleConnectionWithoutDbName() |
||
| 230 | { |
||
| 231 | $container = $this->loadContainer('orm_service_simple_single_entity_manager_without_dbname'); |
||
| 232 | |||
| 233 | /** @var Definition $definition */ |
||
| 234 | $definition = $container->getDefinition('doctrine.dbal.default_connection'); |
||
| 235 | |||
| 236 | $this->assertDICConstructorArguments($definition, [ |
||
| 237 | [ |
||
| 238 | 'host' => 'localhost', |
||
| 239 | 'port' => null, |
||
| 240 | 'user' => 'root', |
||
| 241 | 'password' => null, |
||
| 242 | 'driver' => 'pdo_mysql', |
||
| 243 | 'driverOptions' => [], |
||
| 244 | 'defaultTableOptions' => [], |
||
| 245 | ], |
||
| 246 | new Reference('doctrine.dbal.default_connection.configuration'), |
||
| 247 | new Reference('doctrine.dbal.default_connection.event_manager'), |
||
| 248 | [], |
||
| 249 | ]); |
||
| 250 | |||
| 251 | $definition = $container->getDefinition('doctrine.orm.default_entity_manager'); |
||
| 252 | $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass()); |
||
| 253 | $factory = $definition->getFactory(); |
||
| 254 | |||
| 255 | $this->assertEquals('%doctrine.orm.entity_manager.class%', $factory[0]); |
||
| 256 | $this->assertEquals('create', $factory[1]); |
||
| 257 | |||
| 258 | $this->assertDICConstructorArguments($definition, [ |
||
| 259 | new Reference('doctrine.dbal.default_connection'), |
||
| 260 | new Reference('doctrine.orm.default_configuration'), |
||
| 261 | ]); |
||
| 262 | } |
||
| 263 | |||
| 264 | public function testLoadSingleConnection() |
||
| 265 | { |
||
| 266 | $container = $this->loadContainer('orm_service_single_entity_manager'); |
||
| 267 | |||
| 268 | $definition = $container->getDefinition('doctrine.dbal.default_connection'); |
||
| 269 | |||
| 270 | $this->assertDICConstructorArguments($definition, [ |
||
| 271 | [ |
||
| 272 | 'host' => 'localhost', |
||
| 273 | 'driver' => 'pdo_sqlite', |
||
| 274 | 'driverOptions' => [], |
||
| 275 | 'user' => 'sqlite_user', |
||
| 276 | 'port' => null, |
||
| 277 | 'password' => 'sqlite_s3cr3t', |
||
| 278 | 'dbname' => 'sqlite_db', |
||
| 279 | 'memory' => true, |
||
| 280 | 'defaultTableOptions' => [], |
||
| 281 | ], |
||
| 282 | new Reference('doctrine.dbal.default_connection.configuration'), |
||
| 283 | new Reference('doctrine.dbal.default_connection.event_manager'), |
||
| 284 | [], |
||
| 285 | ]); |
||
| 286 | |||
| 287 | $definition = $container->getDefinition('doctrine.orm.default_entity_manager'); |
||
| 288 | $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass()); |
||
| 289 | $this->assertEquals(['%doctrine.orm.entity_manager.class%', 'create'], $definition->getFactory()); |
||
| 290 | |||
| 291 | $this->assertDICConstructorArguments($definition, [ |
||
| 292 | new Reference('doctrine.dbal.default_connection'), |
||
| 293 | new Reference('doctrine.orm.default_configuration'), |
||
| 294 | ]); |
||
| 295 | |||
| 296 | $configDef = $container->getDefinition('doctrine.orm.default_configuration'); |
||
| 297 | $this->assertDICDefinitionMethodCallOnce($configDef, 'setDefaultRepositoryClassName', ['Acme\Doctrine\Repository']); |
||
| 298 | } |
||
| 299 | |||
| 300 | public function testLoadMultipleConnections() |
||
| 301 | { |
||
| 302 | $container = $this->loadContainer('orm_service_multiple_entity_managers'); |
||
| 303 | |||
| 304 | $definition = $container->getDefinition('doctrine.dbal.conn1_connection'); |
||
| 305 | |||
| 306 | $args = $definition->getArguments(); |
||
| 307 | $this->assertEquals('pdo_sqlite', $args[0]['driver']); |
||
| 308 | $this->assertEquals('localhost', $args[0]['host']); |
||
| 309 | $this->assertEquals('sqlite_user', $args[0]['user']); |
||
| 310 | $this->assertEquals('doctrine.dbal.conn1_connection.configuration', (string) $args[1]); |
||
| 311 | $this->assertEquals('doctrine.dbal.conn1_connection.event_manager', (string) $args[2]); |
||
| 312 | |||
| 313 | $this->assertEquals('doctrine.orm.em2_entity_manager', (string) $container->getAlias('doctrine.orm.entity_manager')); |
||
| 314 | |||
| 315 | $definition = $container->getDefinition('doctrine.orm.em1_entity_manager'); |
||
| 316 | $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass()); |
||
| 317 | $this->assertEquals(['%doctrine.orm.entity_manager.class%', 'create'], $definition->getFactory()); |
||
| 318 | |||
| 319 | $arguments = $definition->getArguments(); |
||
| 320 | $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[0]); |
||
| 321 | $this->assertEquals('doctrine.dbal.conn1_connection', (string) $arguments[0]); |
||
| 322 | $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[1]); |
||
| 323 | $this->assertEquals('doctrine.orm.em1_configuration', (string) $arguments[1]); |
||
| 324 | |||
| 325 | $definition = $container->getDefinition('doctrine.dbal.conn2_connection'); |
||
| 326 | |||
| 327 | $args = $definition->getArguments(); |
||
| 328 | $this->assertEquals('pdo_sqlite', $args[0]['driver']); |
||
| 329 | $this->assertEquals('localhost', $args[0]['host']); |
||
| 330 | $this->assertEquals('sqlite_user', $args[0]['user']); |
||
| 331 | $this->assertEquals('doctrine.dbal.conn2_connection.configuration', (string) $args[1]); |
||
| 332 | $this->assertEquals('doctrine.dbal.conn2_connection.event_manager', (string) $args[2]); |
||
| 333 | |||
| 334 | $definition = $container->getDefinition('doctrine.orm.em2_entity_manager'); |
||
| 335 | $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass()); |
||
| 336 | $this->assertEquals(['%doctrine.orm.entity_manager.class%', 'create'], $definition->getFactory()); |
||
| 337 | |||
| 338 | $arguments = $definition->getArguments(); |
||
| 339 | $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[0]); |
||
| 340 | $this->assertEquals('doctrine.dbal.conn2_connection', (string) $arguments[0]); |
||
| 341 | $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[1]); |
||
| 342 | $this->assertEquals('doctrine.orm.em2_configuration', (string) $arguments[1]); |
||
| 343 | |||
| 344 | $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_metadata_cache')); |
||
| 345 | $this->assertEquals(DoctrineProvider::class, $definition->getClass()); |
||
| 346 | |||
| 347 | $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_query_cache')); |
||
| 348 | $this->assertEquals(DoctrineProvider::class, $definition->getClass()); |
||
| 349 | $arguments = $definition->getArguments(); |
||
| 350 | $this->assertInstanceOf(Reference::class, $arguments[0]); |
||
| 351 | $this->assertEquals('cache.app', (string) $arguments[0]); |
||
| 352 | |||
| 353 | $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_result_cache')); |
||
| 354 | $this->assertEquals(DoctrineProvider::class, $definition->getClass()); |
||
| 355 | $arguments = $definition->getArguments(); |
||
| 356 | $this->assertInstanceOf(Reference::class, $arguments[0]); |
||
| 357 | $this->assertEquals('cache.app', (string) $arguments[0]); |
||
| 358 | } |
||
| 359 | |||
| 360 | public function testLoadLogging() |
||
| 361 | { |
||
| 362 | $container = $this->loadContainer('dbal_logging'); |
||
| 363 | |||
| 364 | $definition = $container->getDefinition('doctrine.dbal.log_connection.configuration'); |
||
| 365 | $this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', [new Reference('doctrine.dbal.logger')]); |
||
| 366 | |||
| 367 | $definition = $container->getDefinition('doctrine.dbal.profile_connection.configuration'); |
||
| 368 | $this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', [new Reference('doctrine.dbal.logger.profiling.profile')]); |
||
| 369 | |||
| 370 | $definition = $container->getDefinition('doctrine.dbal.profile_with_backtrace_connection.configuration'); |
||
| 371 | $this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', [new Reference('doctrine.dbal.logger.backtrace.profile_with_backtrace')]); |
||
| 372 | |||
| 373 | $definition = $container->getDefinition('doctrine.dbal.backtrace_without_profile_connection.configuration'); |
||
| 374 | $this->assertDICDefinitionNoMethodCall($definition, 'setSQLLogger'); |
||
| 375 | |||
| 376 | $definition = $container->getDefinition('doctrine.dbal.both_connection.configuration'); |
||
| 377 | $this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', [new Reference('doctrine.dbal.logger.chain.both')]); |
||
| 378 | } |
||
| 379 | |||
| 380 | public function testEntityManagerMetadataCacheDriverConfiguration() |
||
| 381 | { |
||
| 382 | $container = $this->loadContainer('orm_service_multiple_entity_managers'); |
||
| 383 | |||
| 384 | $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_metadata_cache')); |
||
| 385 | $this->assertDICDefinitionClass($definition, DoctrineProvider::class); |
||
| 386 | |||
| 387 | $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em2_metadata_cache')); |
||
| 388 | $this->assertDICDefinitionClass($definition, DoctrineProvider::class); |
||
| 389 | } |
||
| 390 | |||
| 391 | public function testDependencyInjectionImportsOverrideDefaults() |
||
| 392 | { |
||
| 393 | $container = $this->loadContainer('orm_imports'); |
||
| 394 | |||
| 395 | $cacheDefinition = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_metadata_cache')); |
||
| 396 | $this->assertEquals(DoctrineProvider::class, $cacheDefinition->getClass()); |
||
| 397 | |||
| 398 | $configDefinition = $container->getDefinition('doctrine.orm.default_configuration'); |
||
| 399 | $this->assertDICDefinitionMethodCallOnce($configDefinition, 'setAutoGenerateProxyClasses', ['%doctrine.orm.auto_generate_proxy_classes%']); |
||
| 400 | } |
||
| 401 | |||
| 402 | public function testSingleEntityManagerMultipleMappingBundleDefinitions() |
||
| 403 | { |
||
| 404 | $container = $this->loadContainer('orm_single_em_bundle_mappings', ['YamlBundle', 'AnnotationsBundle', 'XmlBundle']); |
||
| 405 | |||
| 406 | $definition = $container->getDefinition('doctrine.orm.default_metadata_driver'); |
||
| 407 | |||
| 408 | $this->assertDICDefinitionMethodCallAt(0, $definition, 'addDriver', [ |
||
| 409 | new Reference('doctrine.orm.default_annotation_metadata_driver'), |
||
| 410 | 'Fixtures\Bundles\AnnotationsBundle\Entity', |
||
| 411 | ]); |
||
| 412 | |||
| 413 | $this->assertDICDefinitionMethodCallAt(1, $definition, 'addDriver', [ |
||
| 414 | new Reference('doctrine.orm.default_yml_metadata_driver'), |
||
| 415 | 'Fixtures\Bundles\YamlBundle\Entity', |
||
| 416 | ]); |
||
| 417 | |||
| 418 | $this->assertDICDefinitionMethodCallAt(2, $definition, 'addDriver', [ |
||
| 419 | new Reference('doctrine.orm.default_xml_metadata_driver'), |
||
| 420 | 'Fixtures\Bundles\XmlBundle', |
||
| 421 | ]); |
||
| 422 | |||
| 423 | $annDef = $container->getDefinition('doctrine.orm.default_annotation_metadata_driver'); |
||
| 424 | $this->assertDICConstructorArguments($annDef, [ |
||
| 425 | new Reference('doctrine.orm.metadata.annotation_reader'), |
||
| 426 | [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'AnnotationsBundle' . DIRECTORY_SEPARATOR . 'Entity'], |
||
| 427 | ]); |
||
| 428 | |||
| 429 | $ymlDef = $container->getDefinition('doctrine.orm.default_yml_metadata_driver'); |
||
| 430 | $this->assertDICConstructorArguments($ymlDef, [ |
||
| 431 | [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'YamlBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'doctrine' => 'Fixtures\Bundles\YamlBundle\Entity'], |
||
| 432 | ]); |
||
| 433 | |||
| 434 | $xmlDef = $container->getDefinition('doctrine.orm.default_xml_metadata_driver'); |
||
| 435 | $this->assertDICConstructorArguments($xmlDef, [ |
||
| 436 | [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'XmlBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'doctrine' => 'Fixtures\Bundles\XmlBundle'], |
||
| 437 | ]); |
||
| 438 | } |
||
| 439 | |||
| 440 | public function testMultipleEntityManagersMappingBundleDefinitions() |
||
| 441 | { |
||
| 442 | $container = $this->loadContainer('orm_multiple_em_bundle_mappings', ['YamlBundle', 'AnnotationsBundle', 'XmlBundle']); |
||
| 443 | |||
| 444 | $this->assertEquals(['em1' => 'doctrine.orm.em1_entity_manager', 'em2' => 'doctrine.orm.em2_entity_manager'], $container->getParameter('doctrine.entity_managers'), 'Set of the existing EntityManagers names is incorrect.'); |
||
| 445 | $this->assertEquals('%doctrine.entity_managers%', $container->getDefinition('doctrine')->getArgument(2), 'Set of the existing EntityManagers names is incorrect.'); |
||
| 446 | |||
| 447 | $def1 = $container->getDefinition('doctrine.orm.em1_metadata_driver'); |
||
| 448 | $def2 = $container->getDefinition('doctrine.orm.em2_metadata_driver'); |
||
| 449 | |||
| 450 | $this->assertDICDefinitionMethodCallAt(0, $def1, 'addDriver', [ |
||
| 451 | new Reference('doctrine.orm.em1_annotation_metadata_driver'), |
||
| 452 | 'Fixtures\Bundles\AnnotationsBundle\Entity', |
||
| 453 | ]); |
||
| 454 | |||
| 455 | $this->assertDICDefinitionMethodCallAt(0, $def2, 'addDriver', [ |
||
| 456 | new Reference('doctrine.orm.em2_yml_metadata_driver'), |
||
| 457 | 'Fixtures\Bundles\YamlBundle\Entity', |
||
| 458 | ]); |
||
| 459 | |||
| 460 | $this->assertDICDefinitionMethodCallAt(1, $def2, 'addDriver', [ |
||
| 461 | new Reference('doctrine.orm.em2_xml_metadata_driver'), |
||
| 462 | 'Fixtures\Bundles\XmlBundle', |
||
| 463 | ]); |
||
| 464 | |||
| 465 | $annDef = $container->getDefinition('doctrine.orm.em1_annotation_metadata_driver'); |
||
| 466 | $this->assertDICConstructorArguments($annDef, [ |
||
| 467 | new Reference('doctrine.orm.metadata.annotation_reader'), |
||
| 468 | [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'AnnotationsBundle' . DIRECTORY_SEPARATOR . 'Entity'], |
||
| 469 | ]); |
||
| 470 | |||
| 471 | $ymlDef = $container->getDefinition('doctrine.orm.em2_yml_metadata_driver'); |
||
| 472 | $this->assertDICConstructorArguments($ymlDef, [ |
||
| 473 | [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'YamlBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'doctrine' => 'Fixtures\Bundles\YamlBundle\Entity'], |
||
| 474 | ]); |
||
| 475 | |||
| 476 | $xmlDef = $container->getDefinition('doctrine.orm.em2_xml_metadata_driver'); |
||
| 477 | $this->assertDICConstructorArguments($xmlDef, [ |
||
| 478 | [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'XmlBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'doctrine' => 'Fixtures\Bundles\XmlBundle'], |
||
| 479 | ]); |
||
| 480 | } |
||
| 481 | |||
| 482 | public function testSingleEntityManagerDefaultTableOptions() |
||
| 483 | { |
||
| 484 | $container = $this->loadContainer('orm_single_em_default_table_options', ['YamlBundle', 'AnnotationsBundle', 'XmlBundle']); |
||
| 485 | |||
| 486 | $param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0); |
||
| 487 | |||
| 488 | $this->assertArrayHasKey('defaultTableOptions', $param); |
||
| 489 | |||
| 490 | $defaults = $param['defaultTableOptions']; |
||
| 491 | |||
| 492 | $this->assertArrayHasKey('charset', $defaults); |
||
| 493 | $this->assertArrayHasKey('collate', $defaults); |
||
| 494 | $this->assertArrayHasKey('engine', $defaults); |
||
| 495 | |||
| 496 | $this->assertEquals('utf8mb4', $defaults['charset']); |
||
| 497 | $this->assertEquals('utf8mb4_unicode_ci', $defaults['collate']); |
||
| 498 | $this->assertEquals('InnoDB', $defaults['engine']); |
||
| 499 | } |
||
| 500 | |||
| 501 | public function testSetTypes() |
||
| 502 | { |
||
| 503 | $container = $this->loadContainer('dbal_types'); |
||
| 504 | |||
| 505 | $this->assertEquals( |
||
| 506 | ['test' => ['class' => TestType::class]], |
||
| 507 | $container->getParameter('doctrine.dbal.connection_factory.types') |
||
| 508 | ); |
||
| 509 | $this->assertEquals('%doctrine.dbal.connection_factory.types%', $container->getDefinition('doctrine.dbal.connection_factory')->getArgument(0)); |
||
| 510 | } |
||
| 511 | |||
| 512 | public function testSetCustomFunctions() |
||
| 513 | { |
||
| 514 | $container = $this->loadContainer('orm_functions'); |
||
| 515 | |||
| 516 | $definition = $container->getDefinition('doctrine.orm.default_configuration'); |
||
| 517 | $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomStringFunction', ['test_string', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestStringFunction']); |
||
| 518 | $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomNumericFunction', ['test_numeric', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestNumericFunction']); |
||
| 519 | $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomDatetimeFunction', ['test_datetime', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestDatetimeFunction']); |
||
| 520 | } |
||
| 521 | |||
| 522 | View Code Duplication | public function testSetNamingStrategy() |
|
| 523 | { |
||
| 524 | $container = $this->loadContainer('orm_namingstrategy'); |
||
| 525 | |||
| 526 | $def1 = $container->getDefinition('doctrine.orm.em1_configuration'); |
||
| 527 | $def2 = $container->getDefinition('doctrine.orm.em2_configuration'); |
||
| 528 | |||
| 529 | $this->assertDICDefinitionMethodCallOnce($def1, 'setNamingStrategy', [0 => new Reference('doctrine.orm.naming_strategy.default')]); |
||
| 530 | $this->assertDICDefinitionMethodCallOnce($def2, 'setNamingStrategy', [0 => new Reference('doctrine.orm.naming_strategy.underscore')]); |
||
| 531 | } |
||
| 532 | |||
| 533 | View Code Duplication | public function testSetQuoteStrategy() |
|
| 534 | { |
||
| 535 | $container = $this->loadContainer('orm_quotestrategy'); |
||
| 536 | |||
| 537 | $def1 = $container->getDefinition('doctrine.orm.em1_configuration'); |
||
| 538 | $def2 = $container->getDefinition('doctrine.orm.em2_configuration'); |
||
| 539 | |||
| 540 | $this->assertDICDefinitionMethodCallOnce($def1, 'setQuoteStrategy', [0 => new Reference('doctrine.orm.quote_strategy.default')]); |
||
| 541 | $this->assertDICDefinitionMethodCallOnce($def2, 'setQuoteStrategy', [0 => new Reference('doctrine.orm.quote_strategy.ansi')]); |
||
| 542 | } |
||
| 543 | |||
| 544 | public function testSecondLevelCache() |
||
| 545 | { |
||
| 546 | $container = $this->loadContainer('orm_second_level_cache'); |
||
| 547 | |||
| 548 | $this->assertTrue($container->has('doctrine.orm.default_configuration')); |
||
| 549 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.cache_configuration')); |
||
| 550 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.region_cache_driver')); |
||
| 551 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.regions_configuration')); |
||
| 552 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.default_cache_factory')); |
||
| 553 | |||
| 554 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.logger_chain')); |
||
| 555 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.logger_statistics')); |
||
| 556 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.logger.my_service_logger1')); |
||
| 557 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.logger.my_service_logger2')); |
||
| 558 | |||
| 559 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.region.my_entity_region')); |
||
| 560 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.region.my_service_region')); |
||
| 561 | $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.region.my_query_region_filelock')); |
||
| 562 | |||
| 563 | $slcFactoryDef = $container->getDefinition('doctrine.orm.default_second_level_cache.default_cache_factory'); |
||
| 564 | $myEntityRegionDef = $container->getDefinition('doctrine.orm.default_second_level_cache.region.my_entity_region'); |
||
| 565 | $loggerChainDef = $container->getDefinition('doctrine.orm.default_second_level_cache.logger_chain'); |
||
| 566 | $loggerStatisticsDef = $container->getDefinition('doctrine.orm.default_second_level_cache.logger_statistics'); |
||
| 567 | $myQueryRegionDef = $container->getDefinition('doctrine.orm.default_second_level_cache.region.my_query_region_filelock'); |
||
| 568 | $cacheDriverDef = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_second_level_cache.region_cache_driver')); |
||
| 569 | $configDef = $container->getDefinition('doctrine.orm.default_configuration'); |
||
| 570 | $myEntityRegionArgs = $myEntityRegionDef->getArguments(); |
||
| 571 | $myQueryRegionArgs = $myQueryRegionDef->getArguments(); |
||
| 572 | $slcFactoryArgs = $slcFactoryDef->getArguments(); |
||
| 573 | |||
| 574 | $this->assertDICDefinitionClass($slcFactoryDef, '%doctrine.orm.second_level_cache.default_cache_factory.class%'); |
||
| 575 | $this->assertDICDefinitionClass($myQueryRegionDef, '%doctrine.orm.second_level_cache.filelock_region.class%'); |
||
| 576 | $this->assertDICDefinitionClass($myEntityRegionDef, '%doctrine.orm.second_level_cache.default_region.class%'); |
||
| 577 | $this->assertDICDefinitionClass($loggerChainDef, '%doctrine.orm.second_level_cache.logger_chain.class%'); |
||
| 578 | $this->assertDICDefinitionClass($loggerStatisticsDef, '%doctrine.orm.second_level_cache.logger_statistics.class%'); |
||
| 579 | $this->assertDICDefinitionClass($cacheDriverDef, DoctrineProvider::class); |
||
| 580 | $this->assertDICDefinitionMethodCallOnce($configDef, 'setSecondLevelCacheConfiguration'); |
||
| 581 | $this->assertDICDefinitionMethodCallCount($slcFactoryDef, 'setRegion', [], 3); |
||
| 582 | $this->assertDICDefinitionMethodCallCount($loggerChainDef, 'setLogger', [], 3); |
||
| 583 | |||
| 584 | $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $slcFactoryArgs[0]); |
||
| 585 | $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $slcFactoryArgs[1]); |
||
| 586 | |||
| 587 | $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $myEntityRegionArgs[1]); |
||
| 588 | $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $myQueryRegionArgs[0]); |
||
| 589 | |||
| 590 | $this->assertEquals('my_entity_region', $myEntityRegionArgs[0]); |
||
| 591 | $this->assertEquals('doctrine.orm.default_second_level_cache.region.my_entity_region_driver', $myEntityRegionArgs[1]); |
||
| 592 | $this->assertEquals(600, $myEntityRegionArgs[2]); |
||
| 593 | |||
| 594 | $this->assertEquals('doctrine.orm.default_second_level_cache.region.my_query_region', $myQueryRegionArgs[0]); |
||
| 595 | $this->assertContains('/doctrine/orm/slc/filelock', $myQueryRegionArgs[1]); |
||
| 596 | $this->assertEquals(60, $myQueryRegionArgs[2]); |
||
| 597 | |||
| 598 | $this->assertEquals('doctrine.orm.default_second_level_cache.regions_configuration', $slcFactoryArgs[0]); |
||
| 599 | $this->assertEquals('doctrine.orm.default_second_level_cache.region_cache_driver', $slcFactoryArgs[1]); |
||
| 600 | } |
||
| 601 | |||
| 602 | public function testSingleEMSetCustomFunctions() |
||
| 603 | { |
||
| 604 | $container = $this->loadContainer('orm_single_em_dql_functions'); |
||
| 605 | |||
| 606 | $definition = $container->getDefinition('doctrine.orm.default_configuration'); |
||
| 607 | $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomStringFunction', ['test_string', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestStringFunction']); |
||
| 608 | } |
||
| 609 | |||
| 610 | public function testAddCustomHydrationMode() |
||
| 611 | { |
||
| 612 | $container = $this->loadContainer('orm_hydration_mode'); |
||
| 613 | |||
| 614 | $definition = $container->getDefinition('doctrine.orm.default_configuration'); |
||
| 615 | $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomHydrationMode', ['test_hydrator', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestHydrator']); |
||
| 616 | } |
||
| 617 | |||
| 618 | public function testAddFilter() |
||
| 619 | { |
||
| 620 | $container = $this->loadContainer('orm_filters'); |
||
| 621 | |||
| 622 | $definition = $container->getDefinition('doctrine.orm.default_configuration'); |
||
| 623 | $args = [ |
||
| 624 | ['soft_delete', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestFilter'], |
||
| 625 | ['myFilter', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestFilter'], |
||
| 626 | ]; |
||
| 627 | $this->assertDICDefinitionMethodCallCount($definition, 'addFilter', $args, 2); |
||
| 628 | |||
| 629 | $definition = $container->getDefinition('doctrine.orm.default_manager_configurator'); |
||
| 630 | $this->assertDICConstructorArguments($definition, [['soft_delete', 'myFilter'], ['myFilter' => ['myParameter' => 'myValue', 'mySecondParameter' => 'mySecondValue']]]); |
||
| 631 | |||
| 632 | // Let's create the instance to check the configurator work. |
||
| 633 | /** @var EntityManager $entityManager */ |
||
| 634 | $entityManager = $container->get('doctrine.orm.entity_manager'); |
||
| 635 | $this->assertCount(2, $entityManager->getFilters()->getEnabledFilters()); |
||
| 636 | } |
||
| 637 | |||
| 638 | public function testResolveTargetEntity() |
||
| 639 | { |
||
| 640 | $container = $this->loadContainer('orm_resolve_target_entity'); |
||
| 641 | |||
| 642 | $definition = $container->getDefinition('doctrine.orm.listeners.resolve_target_entity'); |
||
| 643 | $this->assertDICDefinitionMethodCallOnce($definition, 'addResolveTargetEntity', ['Symfony\Component\Security\Core\User\UserInterface', 'MyUserClass', []]); |
||
| 644 | |||
| 645 | $this->assertEquals(['doctrine.event_subscriber' => [[]]], $definition->getTags()); |
||
| 646 | } |
||
| 647 | |||
| 648 | public function testAttachEntityListeners() |
||
| 649 | { |
||
| 650 | $container = $this->loadContainer('orm_attach_entity_listener'); |
||
| 651 | |||
| 652 | $definition = $container->getDefinition('doctrine.orm.default_listeners.attach_entity_listeners'); |
||
| 653 | $methodCalls = $definition->getMethodCalls(); |
||
| 654 | |||
| 655 | $this->assertDICDefinitionMethodCallCount($definition, 'addEntityListener', [], 6); |
||
| 656 | $this->assertEquals(['doctrine.event_listener' => [ ['event' => 'loadClassMetadata'] ] ], $definition->getTags()); |
||
| 657 | |||
| 658 | $this->assertEquals($methodCalls[0], [ |
||
| 659 | 'addEntityListener', |
||
| 660 | [ |
||
| 661 | 'ExternalBundles\Entities\FooEntity', |
||
| 662 | 'MyBundles\Listeners\FooEntityListener', |
||
| 663 | 'prePersist', |
||
| 664 | null, |
||
| 665 | ], |
||
| 666 | ]); |
||
| 667 | |||
| 668 | $this->assertEquals($methodCalls[1], [ |
||
| 669 | 'addEntityListener', |
||
| 670 | [ |
||
| 671 | 'ExternalBundles\Entities\FooEntity', |
||
| 672 | 'MyBundles\Listeners\FooEntityListener', |
||
| 673 | 'postPersist', |
||
| 674 | 'postPersist', |
||
| 675 | ], |
||
| 676 | ]); |
||
| 677 | |||
| 678 | $this->assertEquals($methodCalls[2], [ |
||
| 679 | 'addEntityListener', |
||
| 680 | [ |
||
| 681 | 'ExternalBundles\Entities\FooEntity', |
||
| 682 | 'MyBundles\Listeners\FooEntityListener', |
||
| 683 | 'postLoad', |
||
| 684 | 'postLoadHandler', |
||
| 685 | ], |
||
| 686 | ]); |
||
| 687 | |||
| 688 | $this->assertEquals($methodCalls[3], [ |
||
| 689 | 'addEntityListener', |
||
| 690 | [ |
||
| 691 | 'ExternalBundles\Entities\BarEntity', |
||
| 692 | 'MyBundles\Listeners\BarEntityListener', |
||
| 693 | 'prePersist', |
||
| 694 | 'prePersist', |
||
| 695 | ], |
||
| 696 | ]); |
||
| 697 | |||
| 698 | $this->assertEquals($methodCalls[4], [ |
||
| 699 | 'addEntityListener', |
||
| 700 | [ |
||
| 701 | 'ExternalBundles\Entities\BarEntity', |
||
| 702 | 'MyBundles\Listeners\BarEntityListener', |
||
| 703 | 'prePersist', |
||
| 704 | 'prePersistHandler', |
||
| 705 | ], |
||
| 706 | ]); |
||
| 707 | |||
| 708 | $this->assertEquals($methodCalls[5], [ |
||
| 709 | 'addEntityListener', |
||
| 710 | [ |
||
| 711 | 'ExternalBundles\Entities\BarEntity', |
||
| 712 | 'MyBundles\Listeners\LogDeleteEntityListener', |
||
| 713 | 'postDelete', |
||
| 714 | 'postDelete', |
||
| 715 | ], |
||
| 716 | ]); |
||
| 717 | } |
||
| 718 | |||
| 719 | public function testDbalAutoCommit() |
||
| 720 | { |
||
| 721 | $container = $this->loadContainer('dbal_auto_commit'); |
||
| 722 | |||
| 723 | $definition = $container->getDefinition('doctrine.dbal.default_connection.configuration'); |
||
| 724 | $this->assertDICDefinitionMethodCallOnce($definition, 'setAutoCommit', [false]); |
||
| 725 | } |
||
| 726 | |||
| 727 | public function testDbalOracleConnectstring() |
||
| 728 | { |
||
| 729 | $container = $this->loadContainer('dbal_oracle_connectstring'); |
||
| 730 | |||
| 731 | $config = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0); |
||
| 732 | $this->assertSame('scott@sales-server:1521/sales.us.example.com', $config['connectstring']); |
||
| 733 | } |
||
| 734 | |||
| 735 | public function testDbalOracleInstancename() |
||
| 736 | { |
||
| 737 | $container = $this->loadContainer('dbal_oracle_instancename'); |
||
| 738 | |||
| 739 | $config = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0); |
||
| 740 | $this->assertSame('mySuperInstance', $config['instancename']); |
||
| 741 | } |
||
| 742 | |||
| 743 | public function testDbalSchemaFilter() |
||
| 744 | { |
||
| 745 | if (method_exists(Configuration::class, 'setSchemaAssetsFilter')) { |
||
| 746 | $this->markTestSkipped('Test only applies to doctrine/dbal 2.8 or lower'); |
||
| 747 | } |
||
| 748 | |||
| 749 | $container = $this->loadContainer('dbal_schema_filter'); |
||
| 750 | |||
| 751 | $definition = $container->getDefinition('doctrine.dbal.connection1_connection.configuration'); |
||
| 752 | $this->assertDICDefinitionMethodCallOnce($definition, 'setFilterSchemaAssetsExpression', ['~^(?!t_)~']); |
||
| 753 | } |
||
| 754 | |||
| 755 | public function testDbalSchemaFilterNewConfig() |
||
| 756 | { |
||
| 757 | if (! method_exists(Configuration::class, 'setSchemaAssetsFilter')) { |
||
| 758 | $this->markTestSkipped('Test requires doctrine/dbal 2.9 or higher'); |
||
| 759 | } |
||
| 760 | |||
| 761 | $container = $this->getContainer([]); |
||
| 762 | $loader = new DoctrineExtension(); |
||
| 763 | $container->registerExtension($loader); |
||
| 764 | $container->addCompilerPass(new WellKnownSchemaFilterPass()); |
||
| 765 | $container->addCompilerPass(new DbalSchemaFilterPass()); |
||
| 766 | |||
| 767 | // ignore table1 table on "default" connection |
||
| 768 | $container->register('dummy_filter1', DummySchemaAssetsFilter::class) |
||
| 769 | ->setArguments(['table1']) |
||
| 770 | ->addTag('doctrine.dbal.schema_filter'); |
||
| 771 | |||
| 772 | // ignore table2 table on "connection2" connection |
||
| 773 | $container->register('dummy_filter2', DummySchemaAssetsFilter::class) |
||
| 774 | ->setArguments(['table2']) |
||
| 775 | ->addTag('doctrine.dbal.schema_filter', ['connection' => 'connection2']); |
||
| 776 | |||
| 777 | $this->loadFromFile($container, 'dbal_schema_filter'); |
||
| 778 | |||
| 779 | $assetNames = ['table1', 'table2', 'table3', 't_ignored']; |
||
| 780 | $expectedConnectionAssets = [ |
||
| 781 | // ignores table1 + schema_filter applies |
||
| 782 | 'connection1' => ['table2', 'table3'], |
||
| 783 | // ignores table2, no schema_filter applies |
||
| 784 | 'connection2' => ['table1', 'table3', 't_ignored'], |
||
| 785 | // connection3 has no ignores, handled separately |
||
| 786 | ]; |
||
| 787 | |||
| 788 | $this->compileContainer($container); |
||
| 789 | |||
| 790 | $getConfiguration = static function (string $connectionName) use ($container) : Configuration { |
||
| 791 | return $container->get(sprintf('doctrine.dbal.%s_connection', $connectionName))->getConfiguration(); |
||
| 792 | }; |
||
| 793 | |||
| 794 | foreach ($expectedConnectionAssets as $connectionName => $expectedTables) { |
||
| 795 | $connConfig = $getConfiguration($connectionName); |
||
| 796 | $this->assertSame($expectedTables, array_values(array_filter($assetNames, $connConfig->getSchemaAssetsFilter())), sprintf('Filtering for connection "%s"', $connectionName)); |
||
| 797 | } |
||
| 798 | |||
| 799 | $this->assertNull($connConfig = $getConfiguration('connection3')->getSchemaAssetsFilter()); |
||
| 800 | } |
||
| 801 | |||
| 802 | public function testWellKnownSchemaFilterDefaultTables() |
||
| 803 | { |
||
| 804 | if (! method_exists(Configuration::class, 'setSchemaAssetsFilter')) { |
||
| 805 | $this->markTestSkipped('Test requires doctrine/dbal 2.9 or higher'); |
||
| 806 | } |
||
| 807 | |||
| 808 | $container = $this->getContainer([]); |
||
| 809 | $loader = new DoctrineExtension(); |
||
| 810 | $container->registerExtension($loader); |
||
| 811 | $container->addCompilerPass(new WellKnownSchemaFilterPass()); |
||
| 812 | $container->addCompilerPass(new DbalSchemaFilterPass()); |
||
| 813 | |||
| 814 | $this->loadFromFile($container, 'well_known_schema_filter_default_tables'); |
||
| 815 | |||
| 816 | $this->compileContainer($container); |
||
| 817 | |||
| 818 | $definition = $container->getDefinition('doctrine.dbal.well_known_schema_asset_filter'); |
||
| 819 | |||
| 820 | $this->assertSame([['cache_items', 'lock_keys', 'sessions', 'messenger_messages']], $definition->getArguments()); |
||
| 821 | $this->assertSame([['connection' => 'connection1'], ['connection' => 'connection2'], ['connection' => 'connection3']], $definition->getTag('doctrine.dbal.schema_filter')); |
||
| 822 | |||
| 823 | $definition = $container->getDefinition('doctrine.dbal.connection1_schema_asset_filter_manager'); |
||
| 824 | |||
| 825 | $this->assertEquals([new Reference('doctrine.dbal.well_known_schema_asset_filter'), new Reference('doctrine.dbal.connection1_regex_schema_filter')], $definition->getArgument(0)); |
||
| 826 | |||
| 827 | $filter = $container->get('well_known_filter'); |
||
| 828 | |||
| 829 | $this->assertFalse($filter('sessions')); |
||
| 830 | $this->assertFalse($filter('cache_items')); |
||
| 831 | $this->assertFalse($filter('lock_keys')); |
||
| 832 | $this->assertFalse($filter('messenger_messages')); |
||
| 833 | $this->assertTrue($filter('anything_else')); |
||
| 834 | } |
||
| 835 | |||
| 836 | public function testWellKnownSchemaFilterOverriddenTables() |
||
| 837 | { |
||
| 838 | if (! method_exists(Configuration::class, 'setSchemaAssetsFilter')) { |
||
| 839 | $this->markTestSkipped('Test requires doctrine/dbal 2.9 or higher'); |
||
| 840 | } |
||
| 841 | |||
| 842 | $container = $this->getContainer([]); |
||
| 843 | $loader = new DoctrineExtension(); |
||
| 844 | $container->registerExtension($loader); |
||
| 845 | $container->addCompilerPass(new WellKnownSchemaFilterPass()); |
||
| 846 | $container->addCompilerPass(new DbalSchemaFilterPass()); |
||
| 847 | |||
| 848 | $this->loadFromFile($container, 'well_known_schema_filter_overridden_tables'); |
||
| 849 | |||
| 850 | $this->compileContainer($container); |
||
| 851 | |||
| 852 | $filter = $container->get('well_known_filter'); |
||
| 853 | |||
| 854 | $this->assertFalse($filter('app_session')); |
||
| 855 | $this->assertFalse($filter('app_cache')); |
||
| 856 | $this->assertFalse($filter('app_locks')); |
||
| 857 | $this->assertFalse($filter('app_messages')); |
||
| 858 | $this->assertTrue($filter('sessions')); |
||
| 859 | $this->assertTrue($filter('cache_items')); |
||
| 860 | $this->assertTrue($filter('lock_keys')); |
||
| 861 | $this->assertTrue($filter('messenger_messages')); |
||
| 862 | } |
||
| 863 | |||
| 864 | public function testEntityListenerResolver() |
||
| 880 | |||
| 881 | public function testAttachEntityListenerTag() |
||
| 882 | { |
||
| 883 | $container = $this->getContainer([]); |
||
| 884 | $loader = new DoctrineExtension(); |
||
| 885 | $container->registerExtension($loader); |
||
| 886 | $container->addCompilerPass(new EntityListenerPass()); |
||
| 887 | |||
| 888 | $this->loadFromFile($container, 'orm_attach_entity_listener_tag'); |
||
| 889 | |||
| 890 | $this->compileContainer($container); |
||
| 891 | |||
| 892 | $listener = $container->getDefinition('doctrine.orm.em1_entity_listener_resolver'); |
||
| 893 | $this->assertDICDefinitionMethodCallCount($listener, 'registerService', [ |
||
| 894 | ['EntityListener1', 'entity_listener1'], |
||
| 895 | ['Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'invokable_entity_listener'], |
||
| 896 | ['Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'invokable_entity_listener'], |
||
| 897 | ['ParentEntityListener', 'children_entity_listener'], |
||
| 898 | ], 4); |
||
| 899 | |||
| 900 | $listener = $container->getDefinition('doctrine.orm.em2_entity_listener_resolver'); |
||
| 901 | $this->assertDICDefinitionMethodCallOnce($listener, 'registerService', ['EntityListener2', 'entity_listener2']); |
||
| 902 | |||
| 903 | $attachListener = $container->getDefinition('doctrine.orm.em1_listeners.attach_entity_listeners'); |
||
| 904 | $this->assertDICDefinitionMethodCallAt(0, $attachListener, 'addEntityListener', ['My/Entity1', 'EntityListener1', 'postLoad']); |
||
| 905 | $this->assertDICDefinitionMethodCallAt(1, $attachListener, 'addEntityListener', ['My/Entity1', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'loadClassMetadata', '__invoke']); |
||
| 906 | $this->assertDICDefinitionMethodCallAt(2, $attachListener, 'addEntityListener', ['My/Entity1', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'postPersist']); |
||
| 907 | $this->assertDICDefinitionMethodCallAt(3, $attachListener, 'addEntityListener', ['My/Entity3', 'ParentEntityListener', 'postLoad']); |
||
| 908 | |||
| 909 | $attachListener = $container->getDefinition('doctrine.orm.em2_listeners.attach_entity_listeners'); |
||
| 910 | $this->assertDICDefinitionMethodCallOnce($attachListener, 'addEntityListener', ['My/Entity2', 'EntityListener2', 'preFlush', 'preFlushHandler']); |
||
| 911 | } |
||
| 912 | |||
| 913 | public function testAttachEntityListenersTwoConnections() |
||
| 914 | { |
||
| 932 | |||
| 933 | public function testAttachLazyEntityListener() |
||
| 959 | |||
| 960 | public function testAttachLazyEntityListenerForCustomResolver() |
||
| 977 | |||
| 978 | /** |
||
| 979 | * @expectedException \InvalidArgumentException |
||
| 980 | * @expectedExceptionMessage EntityListenerServiceResolver |
||
| 981 | */ |
||
| 982 | View Code Duplication | public function testLazyEntityListenerResolverWithoutCorrectInterface() |
|
| 993 | |||
| 994 | public function testPrivateLazyEntityListener() |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * @expectedException \InvalidArgumentException |
||
| 1010 | * @expectedExceptionMessageRegExp /The service ".*" must not be abstract as this entity listener is lazy-loaded/ |
||
| 1011 | */ |
||
| 1012 | View Code Duplication | public function testAbstractLazyEntityListener() |
|
| 1023 | |||
| 1024 | public function testRepositoryFactory() |
||
| 1031 | |||
| 1032 | private function loadContainer($fixture, array $bundles = ['YamlBundle'], CompilerPassInterface $compilerPass = null) |
||
| 1047 | |||
| 1048 | private function getContainer(array $bundles) |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Assertion on the Class of a DIC Service Definition. |
||
| 1078 | * |
||
| 1079 | * @param string $expectedClass |
||
| 1080 | */ |
||
| 1081 | private function assertDICDefinitionClass(Definition $definition, $expectedClass) |
||
| 1085 | |||
| 1086 | private function assertDICConstructorArguments(Definition $definition, $args) |
||
| 1090 | |||
| 1091 | View Code Duplication | private function assertDICDefinitionMethodCallAt($pos, Definition $definition, $methodName, array $params = null) |
|
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Assertion for the DI Container, check if the given definition contains a method call with the given parameters. |
||
| 1111 | * |
||
| 1112 | * @param string $methodName |
||
| 1113 | * @param array $params |
||
| 1114 | */ |
||
| 1115 | View Code Duplication | private function assertDICDefinitionMethodCallOnce(Definition $definition, $methodName, array $params = null) |
|
| 1139 | |||
| 1140 | private function assertDICDefinitionMethodCallCount(Definition $definition, $methodName, array $params = [], $nbCalls = 1) |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Assertion for the DI Container, check if the given definition does not contain a method call with the given parameters. |
||
| 1164 | * |
||
| 1165 | * @param string $methodName |
||
| 1166 | * @param array $params |
||
| 1167 | */ |
||
| 1168 | private function assertDICDefinitionNoMethodCall(Definition $definition, $methodName, array $params = null) |
||
| 1183 | |||
| 1184 | private function compileContainer(ContainerBuilder $container) |
||
| 1190 | } |
||
| 1191 | |||
| 1211 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.