|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +use Contributte\Tester\Toolkit; |
| 4 | +use Contributte\Tester\Utils\ContainerBuilder; |
| 5 | +use Contributte\Tester\Utils\Neonkit; |
| 6 | +use Nette\DI\Compiler; |
| 7 | +use Nette\DI\CompilerExtension; |
| 8 | +use Nette\DI\Definitions\ServiceDefinition; |
| 9 | +use Nettrine\DBAL\DI\DbalExtension; |
| 10 | +use Nettrine\ORM\DI\Helpers\BuilderMan; |
| 11 | +use Nettrine\ORM\DI\OrmExtension; |
| 12 | +use Nettrine\ORM\DI\Pass\AbstractPass; |
| 13 | +use Nettrine\ORM\Exception\LogicalException; |
| 14 | +use Tester\Assert; |
| 15 | +use Tests\Toolkit\Tests; |
| 16 | + |
| 17 | +require_once __DIR__ . '/../../../bootstrap.php'; |
| 18 | + |
| 19 | +// Get connection by name - exists |
| 20 | +Toolkit::test(function (): void { |
| 21 | + ContainerBuilder::of() |
| 22 | + ->withCompiler(function (Compiler $compiler): void { |
| 23 | + $compiler->addExtension('nettrine.dbal', new DbalExtension()); |
| 24 | + $compiler->addExtension('nettrine.orm', new OrmExtension()); |
| 25 | + $compiler->addExtension('test', new class extends CompilerExtension { |
| 26 | + |
| 27 | + public function beforeCompile(): void |
| 28 | + { |
| 29 | + $pass = new class ($this) extends AbstractPass { |
| 30 | + }; |
| 31 | + |
| 32 | + $builderMan = BuilderMan::of($pass); |
| 33 | + $connection = $builderMan->getConnectionByName('default'); |
| 34 | + |
| 35 | + Assert::type(ServiceDefinition::class, $connection); |
| 36 | + } |
| 37 | + |
| 38 | + }); |
| 39 | + $compiler->addConfig([ |
| 40 | + 'parameters' => [ |
| 41 | + 'tempDir' => Tests::TEMP_PATH, |
| 42 | + ], |
| 43 | + ]); |
| 44 | + $compiler->addConfig(Neonkit::load( |
| 45 | + <<<'NEON' |
| 46 | + nettrine.dbal: |
| 47 | + connections: |
| 48 | + default: |
| 49 | + driver: pdo_sqlite |
| 50 | + password: test |
| 51 | + user: test |
| 52 | + path: ":memory:" |
| 53 | + nettrine.orm: |
| 54 | + managers: |
| 55 | + default: |
| 56 | + connection: default |
| 57 | + mapping: |
| 58 | + App: |
| 59 | + type: attributes |
| 60 | + directories: [app/Database] |
| 61 | + namespace: App\Database |
| 62 | + NEON |
| 63 | + )); |
| 64 | + }) |
| 65 | + ->build(); |
| 66 | +}); |
| 67 | + |
| 68 | +// Get connection by name - not found |
| 69 | +Toolkit::test(function (): void { |
| 70 | + Assert::exception(function (): void { |
| 71 | + ContainerBuilder::of() |
| 72 | + ->withCompiler(function (Compiler $compiler): void { |
| 73 | + $compiler->addExtension('nettrine.dbal', new DbalExtension()); |
| 74 | + $compiler->addExtension('nettrine.orm', new OrmExtension()); |
| 75 | + $compiler->addExtension('test', new class extends CompilerExtension { |
| 76 | + |
| 77 | + public function beforeCompile(): void |
| 78 | + { |
| 79 | + $pass = new class ($this) extends AbstractPass { |
| 80 | + }; |
| 81 | + |
| 82 | + $builderMan = BuilderMan::of($pass); |
| 83 | + $builderMan->getConnectionByName('nonexistent'); |
| 84 | + } |
| 85 | + |
| 86 | + }); |
| 87 | + $compiler->addConfig([ |
| 88 | + 'parameters' => [ |
| 89 | + 'tempDir' => Tests::TEMP_PATH, |
| 90 | + ], |
| 91 | + ]); |
| 92 | + $compiler->addConfig(Neonkit::load( |
| 93 | + <<<'NEON' |
| 94 | + nettrine.dbal: |
| 95 | + connections: |
| 96 | + default: |
| 97 | + driver: pdo_sqlite |
| 98 | + password: test |
| 99 | + user: test |
| 100 | + path: ":memory:" |
| 101 | + nettrine.orm: |
| 102 | + managers: |
| 103 | + default: |
| 104 | + connection: default |
| 105 | + mapping: |
| 106 | + App: |
| 107 | + type: attributes |
| 108 | + directories: [app/Database] |
| 109 | + namespace: App\Database |
| 110 | + NEON |
| 111 | + )); |
| 112 | + }) |
| 113 | + ->build(); |
| 114 | + }, LogicalException::class, 'Connection "nonexistent" not found'); |
| 115 | +}); |
| 116 | + |
| 117 | +// Get connections map |
| 118 | +Toolkit::test(function (): void { |
| 119 | + ContainerBuilder::of() |
| 120 | + ->withCompiler(function (Compiler $compiler): void { |
| 121 | + $compiler->addExtension('nettrine.dbal', new DbalExtension()); |
| 122 | + $compiler->addExtension('nettrine.orm', new OrmExtension()); |
| 123 | + $compiler->addExtension('test', new class extends CompilerExtension { |
| 124 | + |
| 125 | + public function beforeCompile(): void |
| 126 | + { |
| 127 | + $pass = new class ($this) extends AbstractPass { |
| 128 | + }; |
| 129 | + |
| 130 | + $builderMan = BuilderMan::of($pass); |
| 131 | + $map = $builderMan->getConnectionsMap(); |
| 132 | + |
| 133 | + Assert::count(2, $map); |
| 134 | + Assert::true(isset($map['default'])); |
| 135 | + Assert::true(isset($map['second'])); |
| 136 | + } |
| 137 | + |
| 138 | + }); |
| 139 | + $compiler->addConfig([ |
| 140 | + 'parameters' => [ |
| 141 | + 'tempDir' => Tests::TEMP_PATH, |
| 142 | + ], |
| 143 | + ]); |
| 144 | + $compiler->addConfig(Neonkit::load( |
| 145 | + <<<'NEON' |
| 146 | + nettrine.dbal: |
| 147 | + connections: |
| 148 | + default: |
| 149 | + driver: pdo_sqlite |
| 150 | + password: test |
| 151 | + user: test |
| 152 | + path: ":memory:" |
| 153 | + second: |
| 154 | + driver: pdo_sqlite |
| 155 | + password: test |
| 156 | + user: test |
| 157 | + path: ":memory:" |
| 158 | + nettrine.orm: |
| 159 | + managers: |
| 160 | + default: |
| 161 | + connection: default |
| 162 | + mapping: |
| 163 | + App: |
| 164 | + type: attributes |
| 165 | + directories: [app/Database] |
| 166 | + namespace: App\Database |
| 167 | + NEON |
| 168 | + )); |
| 169 | + }) |
| 170 | + ->build(); |
| 171 | +}); |
| 172 | + |
| 173 | +// Get managers map |
| 174 | +Toolkit::test(function (): void { |
| 175 | + ContainerBuilder::of() |
| 176 | + ->withCompiler(function (Compiler $compiler): void { |
| 177 | + $compiler->addExtension('nettrine.dbal', new DbalExtension()); |
| 178 | + $compiler->addExtension('nettrine.orm', new OrmExtension()); |
| 179 | + $compiler->addExtension('test', new class extends CompilerExtension { |
| 180 | + |
| 181 | + public function beforeCompile(): void |
| 182 | + { |
| 183 | + $pass = new class ($this) extends AbstractPass { |
| 184 | + }; |
| 185 | + |
| 186 | + $builderMan = BuilderMan::of($pass); |
| 187 | + $map = $builderMan->getManagersMap(); |
| 188 | + |
| 189 | + Assert::count(2, $map); |
| 190 | + Assert::true(isset($map['default'])); |
| 191 | + Assert::true(isset($map['second'])); |
| 192 | + } |
| 193 | + |
| 194 | + }); |
| 195 | + $compiler->addConfig([ |
| 196 | + 'parameters' => [ |
| 197 | + 'tempDir' => Tests::TEMP_PATH, |
| 198 | + ], |
| 199 | + ]); |
| 200 | + $compiler->addConfig(Neonkit::load( |
| 201 | + <<<'NEON' |
| 202 | + nettrine.dbal: |
| 203 | + connections: |
| 204 | + default: |
| 205 | + driver: pdo_sqlite |
| 206 | + password: test |
| 207 | + user: test |
| 208 | + path: ":memory:" |
| 209 | + second: |
| 210 | + driver: pdo_sqlite |
| 211 | + password: test |
| 212 | + user: test |
| 213 | + path: ":memory:" |
| 214 | + nettrine.orm: |
| 215 | + managers: |
| 216 | + default: |
| 217 | + connection: default |
| 218 | + mapping: |
| 219 | + App: |
| 220 | + type: attributes |
| 221 | + directories: [app/Database] |
| 222 | + namespace: App\Database |
| 223 | + second: |
| 224 | + connection: second |
| 225 | + mapping: |
| 226 | + App: |
| 227 | + type: attributes |
| 228 | + directories: [app/Database] |
| 229 | + namespace: App\Database |
| 230 | + NEON |
| 231 | + )); |
| 232 | + }) |
| 233 | + ->build(); |
| 234 | +}); |
| 235 | + |
| 236 | +// Get managers map with decorator |
| 237 | +Toolkit::test(function (): void { |
| 238 | + ContainerBuilder::of() |
| 239 | + ->withCompiler(function (Compiler $compiler): void { |
| 240 | + $compiler->addExtension('nettrine.dbal', new DbalExtension()); |
| 241 | + $compiler->addExtension('nettrine.orm', new OrmExtension()); |
| 242 | + $compiler->addExtension('test', new class extends CompilerExtension { |
| 243 | + |
| 244 | + public function beforeCompile(): void |
| 245 | + { |
| 246 | + $pass = new class ($this) extends AbstractPass { |
| 247 | + }; |
| 248 | + |
| 249 | + $builderMan = BuilderMan::of($pass); |
| 250 | + $map = $builderMan->getManagersMap(); |
| 251 | + |
| 252 | + // With decorator, the decorator replaces the manager in the map |
| 253 | + Assert::count(1, $map); |
| 254 | + Assert::true(isset($map['default'])); |
| 255 | + Assert::contains('entityManagerDecorator', $map['default']); |
| 256 | + } |
| 257 | + |
| 258 | + }); |
| 259 | + $compiler->addConfig([ |
| 260 | + 'parameters' => [ |
| 261 | + 'tempDir' => Tests::TEMP_PATH, |
| 262 | + ], |
| 263 | + ]); |
| 264 | + $compiler->addConfig(Neonkit::load( |
| 265 | + <<<'NEON' |
| 266 | + nettrine.dbal: |
| 267 | + connections: |
| 268 | + default: |
| 269 | + driver: pdo_sqlite |
| 270 | + password: test |
| 271 | + user: test |
| 272 | + path: ":memory:" |
| 273 | + nettrine.orm: |
| 274 | + managers: |
| 275 | + default: |
| 276 | + connection: default |
| 277 | + entityManagerDecoratorClass: Tests\Mocks\DummyEntityManagerDecorator |
| 278 | + mapping: |
| 279 | + App: |
| 280 | + type: attributes |
| 281 | + directories: [app/Database] |
| 282 | + namespace: App\Database |
| 283 | + NEON |
| 284 | + )); |
| 285 | + }) |
| 286 | + ->build(); |
| 287 | +}); |
| 288 | + |
| 289 | +// Get all connections |
| 290 | +Toolkit::test(function (): void { |
| 291 | + ContainerBuilder::of() |
| 292 | + ->withCompiler(function (Compiler $compiler): void { |
| 293 | + $compiler->addExtension('nettrine.dbal', new DbalExtension()); |
| 294 | + $compiler->addExtension('nettrine.orm', new OrmExtension()); |
| 295 | + $compiler->addExtension('test', new class extends CompilerExtension { |
| 296 | + |
| 297 | + public function beforeCompile(): void |
| 298 | + { |
| 299 | + $pass = new class ($this) extends AbstractPass { |
| 300 | + }; |
| 301 | + |
| 302 | + $builderMan = BuilderMan::of($pass); |
| 303 | + $connections = $builderMan->getConnections(); |
| 304 | + |
| 305 | + Assert::count(2, $connections); |
| 306 | + Assert::type(ServiceDefinition::class, $connections['default']); |
| 307 | + Assert::type(ServiceDefinition::class, $connections['second']); |
| 308 | + } |
| 309 | + |
| 310 | + }); |
| 311 | + $compiler->addConfig([ |
| 312 | + 'parameters' => [ |
| 313 | + 'tempDir' => Tests::TEMP_PATH, |
| 314 | + ], |
| 315 | + ]); |
| 316 | + $compiler->addConfig(Neonkit::load( |
| 317 | + <<<'NEON' |
| 318 | + nettrine.dbal: |
| 319 | + connections: |
| 320 | + default: |
| 321 | + driver: pdo_sqlite |
| 322 | + password: test |
| 323 | + user: test |
| 324 | + path: ":memory:" |
| 325 | + second: |
| 326 | + driver: pdo_sqlite |
| 327 | + password: test |
| 328 | + user: test |
| 329 | + path: ":memory:" |
| 330 | + nettrine.orm: |
| 331 | + managers: |
| 332 | + default: |
| 333 | + connection: default |
| 334 | + mapping: |
| 335 | + App: |
| 336 | + type: attributes |
| 337 | + directories: [app/Database] |
| 338 | + namespace: App\Database |
| 339 | + NEON |
| 340 | + )); |
| 341 | + }) |
| 342 | + ->build(); |
| 343 | +}); |
0 commit comments