|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: DatabaseExtension row mapping configuration. |
| 5 | + */ |
| 6 | + |
| 7 | +use Nette\Bridges\DatabaseDI\DatabaseExtension; |
| 8 | +use Nette\DI; |
| 9 | +use Tester\Assert; |
| 10 | + |
| 11 | +require __DIR__ . '/../bootstrap.php'; |
| 12 | + |
| 13 | + |
| 14 | +test('string shortcut mapping', function () { |
| 15 | + $loader = new DI\Config\Loader; |
| 16 | + $config = $loader->load(Tester\FileMock::create(' |
| 17 | + database: |
| 18 | + dsn: "sqlite::memory:" |
| 19 | + mapping: App\Entity\*Row |
| 20 | + debugger: no |
| 21 | +
|
| 22 | + services: |
| 23 | + cache: Nette\Caching\Storages\DevNullStorage |
| 24 | + ', 'neon')); |
| 25 | + |
| 26 | + $compiler = new DI\Compiler; |
| 27 | + $compiler->addExtension('database', new DatabaseExtension(false)); |
| 28 | + $code = $compiler->addConfig($config)->setClassName('Container1')->compile(); |
| 29 | + eval($code); |
| 30 | + |
| 31 | + $container = new Container1; |
| 32 | + $container->initialize(); |
| 33 | + |
| 34 | + $explorer = $container->getService('database.default.explorer'); |
| 35 | + Assert::type(Nette\Database\Explorer::class, $explorer); |
| 36 | + |
| 37 | + // verify the mapping closure was set by checking generated code |
| 38 | + Assert::contains('createRowMapping', $code); |
| 39 | +}); |
| 40 | + |
| 41 | + |
| 42 | +test('full mapping with convention and tables', function () { |
| 43 | + $loader = new DI\Config\Loader; |
| 44 | + $config = $loader->load(Tester\FileMock::create(' |
| 45 | + database: |
| 46 | + dsn: "sqlite::memory:" |
| 47 | + mapping: |
| 48 | + convention: App\Entity\*Row |
| 49 | + tables: |
| 50 | + special: App\Entity\SpecialRow |
| 51 | + debugger: no |
| 52 | +
|
| 53 | + services: |
| 54 | + cache: Nette\Caching\Storages\DevNullStorage |
| 55 | + ', 'neon')); |
| 56 | + |
| 57 | + $compiler = new DI\Compiler; |
| 58 | + $compiler->addExtension('database', new DatabaseExtension(false)); |
| 59 | + $code = $compiler->addConfig($config)->setClassName('Container2')->compile(); |
| 60 | + eval($code); |
| 61 | + |
| 62 | + $container = new Container2; |
| 63 | + $container->initialize(); |
| 64 | + |
| 65 | + $explorer = $container->getService('database.default.explorer'); |
| 66 | + Assert::type(Nette\Database\Explorer::class, $explorer); |
| 67 | + Assert::contains('createRowMapping', $code); |
| 68 | +}); |
| 69 | + |
| 70 | + |
| 71 | +test('no mapping by default', function () { |
| 72 | + $loader = new DI\Config\Loader; |
| 73 | + $config = $loader->load(Tester\FileMock::create(' |
| 74 | + database: |
| 75 | + dsn: "sqlite::memory:" |
| 76 | + debugger: no |
| 77 | +
|
| 78 | + services: |
| 79 | + cache: Nette\Caching\Storages\DevNullStorage |
| 80 | + ', 'neon')); |
| 81 | + |
| 82 | + $compiler = new DI\Compiler; |
| 83 | + $compiler->addExtension('database', new DatabaseExtension(false)); |
| 84 | + $code = $compiler->addConfig($config)->setClassName('Container3')->compile(); |
| 85 | + eval($code); |
| 86 | + |
| 87 | + $container = new Container3; |
| 88 | + $container->initialize(); |
| 89 | + |
| 90 | + $explorer = $container->getService('database.default.explorer'); |
| 91 | + Assert::type(Nette\Database\Explorer::class, $explorer); |
| 92 | + |
| 93 | + // no mapping should be set |
| 94 | + Assert::notContains('createRowMapping', $code); |
| 95 | +}); |
| 96 | + |
| 97 | + |
| 98 | +test('createRowMapping() with convention', function () { |
| 99 | + $mapping = DatabaseExtension::createRowMapping('App\Entity\*Row', []); |
| 100 | + |
| 101 | + // unknown class -> fallback to ActiveRow |
| 102 | + Assert::same(Nette\Database\Table\ActiveRow::class, $mapping('nonexistent')); |
| 103 | +}); |
| 104 | + |
| 105 | + |
| 106 | +test('createRowMapping() with explicit tables', function () { |
| 107 | + $mapping = DatabaseExtension::createRowMapping('', [ |
| 108 | + 'my_table' => 'Nette\Database\Table\ActiveRow', |
| 109 | + ]); |
| 110 | + |
| 111 | + Assert::same(Nette\Database\Table\ActiveRow::class, $mapping('my_table')); |
| 112 | + |
| 113 | + // not in tables and no convention -> fallback |
| 114 | + Assert::same(Nette\Database\Table\ActiveRow::class, $mapping('other')); |
| 115 | +}); |
| 116 | + |
| 117 | + |
| 118 | +test('createRowMapping() tables override convention', function () { |
| 119 | + $mapping = DatabaseExtension::createRowMapping('Some\*Row', [ |
| 120 | + 'special' => 'Nette\Database\Table\ActiveRow', |
| 121 | + ]); |
| 122 | + |
| 123 | + // explicit override wins |
| 124 | + Assert::same(Nette\Database\Table\ActiveRow::class, $mapping('special')); |
| 125 | +}); |
| 126 | + |
| 127 | + |
| 128 | +test('createRowMapping() snake_case to PascalCase', function () { |
| 129 | + $mapping = DatabaseExtension::createRowMapping('*', []); |
| 130 | + |
| 131 | + // We can't test actual entity classes (they don't exist in test env), |
| 132 | + // but we can verify the fallback for non-existent classes |
| 133 | + Assert::same(Nette\Database\Table\ActiveRow::class, $mapping('some_table')); |
| 134 | + |
| 135 | + // Verify convention produces correct class names by using a class that exists |
| 136 | + $mapping = DatabaseExtension::createRowMapping('Nette\Database\Table\*', []); |
| 137 | + Assert::same('Nette\Database\Table\ActiveRow', $mapping('active_row')); |
| 138 | +}); |
0 commit comments