|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Dbal\Tests; |
| 4 | + |
| 5 | +use Enqueue\Dbal\DbalConnectionFactory; |
| 6 | +use Enqueue\Test\ClassExtensionTrait; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +/** |
| 10 | + * The class contains the factory tests dedicated to configuration. |
| 11 | + */ |
| 12 | +class DbalConnectionFactoryConfigTest extends TestCase |
| 13 | +{ |
| 14 | + use ClassExtensionTrait; |
| 15 | + |
| 16 | + public function testThrowNeitherArrayStringNorNullGivenAsConfig() |
| 17 | + { |
| 18 | + $this->expectException(\LogicException::class); |
| 19 | + $this->expectExceptionMessage('The config must be either an array of options, a DSN string or null'); |
| 20 | + |
| 21 | + new DbalConnectionFactory(new \stdClass()); |
| 22 | + } |
| 23 | + |
| 24 | + public function testThrowIfSchemeIsNotSupported() |
| 25 | + { |
| 26 | + $this->expectException(\LogicException::class); |
| 27 | + $this->expectExceptionMessage('The given DSN schema "http" is not supported. There are supported schemes: "db2", "ibm_db2", "mssql", "pdo_sqlsrv", "mysql", "mysql2", "pdo_mysql", "pgsql", "postgres", "postgresql", "pdo_pgsql", "sqlite", "sqlite3", "pdo_sqlite"'); |
| 28 | + |
| 29 | + new DbalConnectionFactory('http://example.com'); |
| 30 | + } |
| 31 | + |
| 32 | + public function testThrowIfDsnCouldNotBeParsed() |
| 33 | + { |
| 34 | + $this->expectException(\LogicException::class); |
| 35 | + $this->expectExceptionMessage('The given DSN "invalidDSN" is not valid. Must contain "://".'); |
| 36 | + |
| 37 | + new DbalConnectionFactory('invalidDSN'); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @dataProvider provideConfigs |
| 42 | + * |
| 43 | + * @param mixed $config |
| 44 | + * @param mixed $expectedConfig |
| 45 | + */ |
| 46 | + public function testShouldParseConfigurationAsExpected($config, $expectedConfig) |
| 47 | + { |
| 48 | + $factory = new DbalConnectionFactory($config); |
| 49 | + |
| 50 | + $this->assertAttributeEquals($expectedConfig, 'config', $factory); |
| 51 | + } |
| 52 | + |
| 53 | + public static function provideConfigs() |
| 54 | + { |
| 55 | + yield [ |
| 56 | + null, |
| 57 | + [ |
| 58 | + 'lazy' => true, |
| 59 | + 'connection' => [ |
| 60 | + 'url' => 'mysql://root@localhost', |
| 61 | + ], |
| 62 | + ], |
| 63 | + ]; |
| 64 | + |
| 65 | + yield [ |
| 66 | + 'mysql://', |
| 67 | + [ |
| 68 | + 'lazy' => true, |
| 69 | + 'connection' => [ |
| 70 | + 'url' => 'mysql://root@localhost', |
| 71 | + ], |
| 72 | + ], |
| 73 | + ]; |
| 74 | + |
| 75 | + yield [ |
| 76 | + 'pgsql://', |
| 77 | + [ |
| 78 | + 'lazy' => true, |
| 79 | + 'connection' => [ |
| 80 | + 'url' => 'pgsql://root@localhost', |
| 81 | + ], |
| 82 | + ], |
| 83 | + ]; |
| 84 | + |
| 85 | + yield [ |
| 86 | + 'mysql://user:pass@host:10000/db', |
| 87 | + [ |
| 88 | + 'lazy' => true, |
| 89 | + 'connection' => [ |
| 90 | + 'url' => 'mysql://user:pass@host:10000/db', |
| 91 | + ], |
| 92 | + ], |
| 93 | + ]; |
| 94 | + |
| 95 | + yield [ |
| 96 | + [], |
| 97 | + [ |
| 98 | + 'lazy' => true, |
| 99 | + 'connection' => [ |
| 100 | + 'url' => 'mysql://root@localhost', |
| 101 | + ], |
| 102 | + ], |
| 103 | + ]; |
| 104 | + |
| 105 | + yield [ |
| 106 | + ['table_name' => 'a_queue_table', 'connection' => ['foo' => 'fooVal', 'bar' => 'barVal']], |
| 107 | + ['table_name' => 'a_queue_table', 'connection' => ['foo' => 'fooVal', 'bar' => 'barVal']], |
| 108 | + ]; |
| 109 | + } |
| 110 | +} |
0 commit comments