|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @dataProvider ../databases.ini |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use Tester\Assert; |
| 10 | + |
| 11 | +require __DIR__ . '/bootstrap.php'; |
| 12 | + |
| 13 | +$conn = new Dibi\Connection($config + ['formatDateTime' => "'Y-m-d H:i:s.u'", 'formatDate' => "'Y-m-d'"]); |
| 14 | + |
| 15 | + |
| 16 | +class Email |
| 17 | +{ |
| 18 | + public $address = 'address@example.com'; |
| 19 | +} |
| 20 | + |
| 21 | +class Time extends DateTimeImmutable |
| 22 | +{ |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +test('Without object translator', function () use ($conn) { |
| 27 | + Assert::exception(function () use ($conn) { |
| 28 | + $conn->translate('?', new Email); |
| 29 | + }, Dibi\Exception::class, 'SQL translate error: Unexpected Email'); |
| 30 | +}); |
| 31 | + |
| 32 | + |
| 33 | +test('Basics', function () use ($conn) { |
| 34 | + $conn->setObjectTranslator(fn(Email $email) => new Dibi\Expression('?', $email->address)); |
| 35 | + Assert::same( |
| 36 | + reformat([ |
| 37 | + 'sqlsrv' => "N'address@example.com'", |
| 38 | + "'address@example.com'", |
| 39 | + ]), |
| 40 | + $conn->translate('?', new Email), |
| 41 | + ); |
| 42 | +}); |
| 43 | + |
| 44 | + |
| 45 | +test('DateTime', function () use ($conn) { |
| 46 | + $stamp = Time::createFromFormat('Y-m-d H:i:s', '2022-11-22 12:13:14'); |
| 47 | + |
| 48 | + // Without object translator, DateTime child is translated by driver |
| 49 | + Assert::same( |
| 50 | + $conn->getDriver()->escapeDateTime($stamp), |
| 51 | + $conn->translate('?', $stamp), |
| 52 | + ); |
| 53 | + |
| 54 | + |
| 55 | + // With object translator |
| 56 | + $conn->setObjectTranslator(fn(Time $time) => new Dibi\Expression('OwnTime(?)', $time->format('H:i:s'))); |
| 57 | + Assert::same( |
| 58 | + reformat([ |
| 59 | + 'sqlsrv' => "OwnTime(N'12:13:14')", |
| 60 | + "OwnTime('12:13:14')", |
| 61 | + ]), |
| 62 | + $conn->translate('?', $stamp), |
| 63 | + ); |
| 64 | + |
| 65 | + |
| 66 | + // With modifier, it is still translated by driver |
| 67 | + Assert::same( |
| 68 | + $conn->getDriver()->escapeDateTime($stamp), |
| 69 | + $conn->translate('%dt', $stamp), |
| 70 | + ); |
| 71 | + Assert::same( |
| 72 | + $conn->getDriver()->escapeDateTime($stamp), |
| 73 | + $conn->translate('%t', $stamp), |
| 74 | + ); |
| 75 | + Assert::same( |
| 76 | + $conn->getDriver()->escapeDate($stamp), |
| 77 | + $conn->translate('%d', $stamp), |
| 78 | + ); |
| 79 | + |
| 80 | + |
| 81 | + // DateTimeImmutable as a Time parent is not affected and still translated by driver |
| 82 | + $dt = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2022-11-22 12:13:14'); |
| 83 | + Assert::same( |
| 84 | + $conn->getDriver()->escapeDateTime($dt), |
| 85 | + $conn->translate('?', $dt), |
| 86 | + ); |
| 87 | + |
| 88 | + // But DateTime translation can be overloaded |
| 89 | + $conn->setObjectTranslator(fn(DateTimeInterface $dt) => new Dibi\Expression('OwnDateTime')); |
| 90 | + Assert::same( |
| 91 | + 'OwnDateTime', |
| 92 | + $conn->translate('?', $dt), |
| 93 | + ); |
| 94 | +}); |
| 95 | + |
| 96 | + |
| 97 | +test('Complex structures', function () use ($conn) { |
| 98 | + $conn->setObjectTranslator(fn(Email $email) => new Dibi\Expression('?', $email->address)); |
| 99 | + $conn->setObjectTranslator(fn(Time $time) => new Dibi\Expression('OwnTime(?)', $time->format('H:i:s'))); |
| 100 | + $conn->setObjectTranslator(fn(DateTimeInterface $dt) => new Dibi\Expression('OwnDateTime')); |
| 101 | + |
| 102 | + $time = Time::createFromFormat('Y-m-d H:i:s', '2022-11-22 12:13:14'); |
| 103 | + Assert::same( |
| 104 | + reformat([ |
| 105 | + 'sqlsrv' => "([a], [b], [c], [d], [e], [f], [g]) VALUES (OwnTime(N'12:13:14'), '2022-11-22', CONVERT(DATETIME2(7), '2022-11-22 12:13:14.000000'), CONVERT(DATETIME2(7), '2022-11-22 12:13:14.000000'), N'address@example.com', OwnDateTime, OwnDateTime)", |
| 106 | + 'odbc' => "([a], [b], [c], [d], [e], [f], [g]) VALUES (OwnTime('12:13:14'), #11/22/2022#, #11/22/2022 12:13:14.000000#, #11/22/2022 12:13:14.000000#, 'address@example.com', OwnDateTime, OwnDateTime)", |
| 107 | + "([a], [b], [c], [d], [e], [f], [g]) VALUES (OwnTime('12:13:14'), '2022-11-22', '2022-11-22 12:13:14.000000', '2022-11-22 12:13:14.000000', 'address@example.com', OwnDateTime, OwnDateTime)", |
| 108 | + ]), |
| 109 | + $conn->translate('%v', [ |
| 110 | + 'a' => $time, |
| 111 | + 'b%d' => $time, |
| 112 | + 'c%t' => $time, |
| 113 | + 'd%dt' => $time, |
| 114 | + 'e' => new Email, |
| 115 | + 'f' => new DateTime, |
| 116 | + 'g' => new DateTimeImmutable, |
| 117 | + ]), |
| 118 | + ); |
| 119 | +}); |
| 120 | + |
| 121 | + |
| 122 | +test('Invalid translator', function () use ($conn) { |
| 123 | + Assert::exception( |
| 124 | + fn() => $conn->setObjectTranslator(fn($email) => 'foo'), |
| 125 | + Dibi\Exception::class, "Object translator must have exactly one parameter with class typehint.", |
| 126 | + ); |
| 127 | + |
| 128 | + Assert::exception( |
| 129 | + fn() => $conn->setObjectTranslator(fn(string $email) => 'foo'), |
| 130 | + Dibi\Exception::class, "Object translator must have exactly one parameter with non-nullable class typehint, got 'string'.", |
| 131 | + ); |
| 132 | + |
| 133 | + Assert::exception( |
| 134 | + fn() => $conn->setObjectTranslator(fn(Email|bool $email) => 'foo'), |
| 135 | + Dibi\Exception::class, "Object translator must have exactly one parameter with non-nullable class typehint, got 'bool'.", |
| 136 | + ); |
| 137 | + |
| 138 | + Assert::exception( |
| 139 | + fn() => $conn->setObjectTranslator(fn(Email|null $email) => 'foo'), |
| 140 | + Dibi\Exception::class, "Object translator must have exactly one parameter with non-nullable class typehint, got '?Email'.", |
| 141 | + ); |
| 142 | + |
| 143 | + $conn->setObjectTranslator(fn(Email $email) => 'foo'); |
| 144 | + Assert::exception( |
| 145 | + fn() => $conn->translate('?', new Email), |
| 146 | + Dibi\Exception::class, "Object translator for class 'Email' returned 'string' but Dibi\Expression expected.", |
| 147 | + ); |
| 148 | +}); |
0 commit comments