diff --git a/examples/query-language-and-conditions.php b/examples/query-language-and-conditions.php index 4f53b2b2c..ccf4eb71c 100644 --- a/examples/query-language-and-conditions.php +++ b/examples/query-language-and-conditions.php @@ -31,7 +31,7 @@ $dibi->test(' SELECT * FROM customers - %if', isset($name), 'WHERE name LIKE ?', $name, '%end' + %if', isset($name), 'WHERE name LIKE ?', $name, '%end', ); // -> SELECT * FROM customers WHERE name LIKE 'K%' @@ -54,7 +54,7 @@ WHERE %if', isset($name), 'name LIKE ?', $name, ' %if', $cond2, 'AND admin=1 %end - %else 1 LIMIT 10 %end' + %else 1 LIMIT 10 %end', ); // -> SELECT * FROM customers WHERE LIMIT 10 diff --git a/examples/query-language-basic-examples.php b/examples/query-language-basic-examples.php index b0efe523f..9e23eb2d1 100644 --- a/examples/query-language-basic-examples.php +++ b/examples/query-language-basic-examples.php @@ -28,7 +28,7 @@ SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE ?', $ipMask, ' - AND [date] > ', new Dibi\DateTime($timestamp) + AND [date] > ', new Dibi\DateTime($timestamp), ); // -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600 @@ -69,7 +69,7 @@ $dibi->test(' SELECT * FROM people - WHERE id IN (?)', $array + WHERE id IN (?)', $array, ); // -> SELECT * FROM people WHERE id IN ( 1, 2, 3 ) diff --git a/examples/using-datetime.php b/examples/using-datetime.php index 272a0ae16..fd3ca62c2 100644 --- a/examples/using-datetime.php +++ b/examples/using-datetime.php @@ -29,6 +29,6 @@ 'id' => 123, 'date' => new DateTime('12.3.2007'), 'stamp' => new DateTime('23.1.2007 10:23'), - ] + ], ); // -> INSERT INTO [mytable] ([id], [date], [stamp]) VALUES (123, '2007-03-12', '2007-01-23 10-23-00') diff --git a/examples/using-substitutions.php b/examples/using-substitutions.php index d4d066d99..d10d85a1e 100644 --- a/examples/using-substitutions.php +++ b/examples/using-substitutions.php @@ -54,6 +54,6 @@ function substFallBack($expr) $dibi->test(" UPDATE :account:user SET name='John Doe', status=:active: - WHERE id=", 7 + WHERE id=", 7, ); // -> UPDATE eshop_user SET name='John Doe', status=7 WHERE id= 7 diff --git a/src/Dibi/Bridges/Nette/DibiExtension22.php b/src/Dibi/Bridges/Nette/DibiExtension22.php index 789fbe382..906a5af61 100644 --- a/src/Dibi/Bridges/Nette/DibiExtension22.php +++ b/src/Dibi/Bridges/Nette/DibiExtension22.php @@ -66,7 +66,7 @@ public function loadConfiguration() if (class_exists(Tracy\Debugger::class)) { $connection->addSetup( [new Nette\DI\Statement('Tracy\Debugger::getBlueScreen'), 'addPanel'], - [[Dibi\Bridges\Tracy\Panel::class, 'renderException']] + [[Dibi\Bridges\Tracy\Panel::class, 'renderException']], ); } diff --git a/src/Dibi/Bridges/Tracy/Panel.php b/src/Dibi/Bridges/Tracy/Panel.php index 5577df4ba..fa89b17c2 100644 --- a/src/Dibi/Bridges/Tracy/Panel.php +++ b/src/Dibi/Bridges/Tracy/Panel.php @@ -174,7 +174,7 @@ public function getPanel(): ?string private function getConnectionName(Dibi\Connection $connection): string { $driver = $connection->getConfig('driver'); - return (is_object($driver) ? get_class($driver) : $driver) + return (is_object($driver) ? $driver::class : $driver) . ($connection->getConfig('name') ? '/' . $connection->getConfig('name') : '') . ($connection->getConfig('host') ? "\u{202f}@\u{202f}" . $connection->getConfig('host') : ''); } diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 83cb099d9..ba51e18a5 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -75,7 +75,7 @@ public function __construct(array $config, ?string $name = null) Helpers::alias($config, 'host', 'hostname'); Helpers::alias($config, 'result|formatDate', 'resultDate'); Helpers::alias($config, 'result|formatDateTime', 'resultDateTime'); - $config['driver'] = $config['driver'] ?? 'mysqli'; + $config['driver'] ??= 'mysqli'; $config['name'] = $name; $this->config = $config; @@ -93,7 +93,7 @@ public function __construct(array $config, ?string $name = null) $this->onEvent[] = [new Loggers\FileLogger($config['profiler']['file'], $filter, $errorsOnly), 'logEvent']; } - $this->substitutes = new HashMap(function (string $expr) { return ":$expr:"; }); + $this->substitutes = new HashMap(fn(string $expr) => ":$expr:"); if (!empty($config['substitutes'])) { foreach ($config['substitutes'] as $key => $value) { $this->substitutes->$key = $value; @@ -253,7 +253,7 @@ final public function test(...$args): bool if ($e->getSql()) { Helpers::dump($e->getSql()); } else { - echo get_class($e) . ': ' . $e->getMessage() . (PHP_SAPI === 'cli' ? "\n" : '
'); + echo $e::class . ': ' . $e->getMessage() . (PHP_SAPI === 'cli' ? "\n" : '
'); } return false; @@ -529,7 +529,7 @@ public function substitute(string $value): string { return strpos($value, ':') === false ? $value - : preg_replace_callback('#:([^:\s]*):#', function (array $m) { return $this->substitutes->{$m[1]}; }, $value); + : preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value); } diff --git a/src/Dibi/DataSource.php b/src/Dibi/DataSource.php index 884af67e5..81e538b55 100644 --- a/src/Dibi/DataSource.php +++ b/src/Dibi/DataSource.php @@ -241,7 +241,7 @@ public function __toString(): string $this->sorting ? ['ORDER BY %by', $this->sorting] : null, "\n%ofs %lmt", $this->offset, - $this->limit + $this->limit, ); } catch (\Throwable $e) { trigger_error($e->getMessage(), E_USER_ERROR); @@ -261,7 +261,7 @@ public function count(): int if ($this->count === null) { $this->count = $this->conds || $this->offset || $this->limit ? Helpers::intVal($this->connection->nativeQuery( - 'SELECT COUNT(*) FROM (' . $this->__toString() . ') t' + 'SELECT COUNT(*) FROM (' . $this->__toString() . ') t', )->fetchSingle()) : $this->getTotalCount(); } @@ -277,7 +277,7 @@ public function getTotalCount(): int { if ($this->totalCount === null) { $this->totalCount = Helpers::intVal($this->connection->nativeQuery( - 'SELECT COUNT(*) FROM ' . $this->sql + 'SELECT COUNT(*) FROM ' . $this->sql, )->fetchSingle()); } diff --git a/src/Dibi/Drivers/FirebirdReflector.php b/src/Dibi/Drivers/FirebirdReflector.php index 7c3283729..88e1e1457 100644 --- a/src/Dibi/Drivers/FirebirdReflector.php +++ b/src/Dibi/Drivers/FirebirdReflector.php @@ -242,7 +242,7 @@ public function getTriggersMeta(?string $table = null): array END AS TRIGGER_ENABLED FROM RDB\$TRIGGERS WHERE RDB\$SYSTEM_FLAG = 0" - . ($table === null ? ';' : " AND RDB\$RELATION_NAME = UPPER('$table');") + . ($table === null ? ';' : " AND RDB\$RELATION_NAME = UPPER('$table');"), ); $triggers = []; while ($row = $res->fetch(true)) { diff --git a/src/Dibi/Drivers/MySqliDriver.php b/src/Dibi/Drivers/MySqliDriver.php index e2bb8f2f0..f9d42eccb 100644 --- a/src/Dibi/Drivers/MySqliDriver.php +++ b/src/Dibi/Drivers/MySqliDriver.php @@ -96,7 +96,7 @@ public function __construct(array $config) $config['database'] ?? '', $config['port'] ?? 0, $config['socket'], - $config['flags'] ?? 0 + $config['flags'] ?? 0, ); if ($this->connection->connect_errno) { diff --git a/src/Dibi/Drivers/SqliteDriver.php b/src/Dibi/Drivers/SqliteDriver.php index 68148da2f..9b1cad773 100644 --- a/src/Dibi/Drivers/SqliteDriver.php +++ b/src/Dibi/Drivers/SqliteDriver.php @@ -291,7 +291,7 @@ public function registerAggregateFunction( string $name, callable $rowCallback, callable $agrCallback, - int $numArgs = -1 + int $numArgs = -1, ): void { $this->connection->createAggregate($name, $rowCallback, $agrCallback, $numArgs); diff --git a/src/Dibi/Drivers/SqliteResult.php b/src/Dibi/Drivers/SqliteResult.php index 2eb659832..e09e610ad 100644 --- a/src/Dibi/Drivers/SqliteResult.php +++ b/src/Dibi/Drivers/SqliteResult.php @@ -76,7 +76,7 @@ public function getResultColumns(): array { $count = $this->resultSet->numColumns(); $columns = []; - static $types = [SQLITE3_INTEGER => 'int', SQLITE3_FLOAT => 'float', SQLITE3_TEXT => 'text', SQLITE3_BLOB => 'blob', SQLITE3_NULL => 'null']; + $types = [SQLITE3_INTEGER => 'int', SQLITE3_FLOAT => 'float', SQLITE3_TEXT => 'text', SQLITE3_BLOB => 'blob', SQLITE3_NULL => 'null']; for ($i = 0; $i < $count; $i++) { $columns[] = [ 'name' => $this->resultSet->columnName($i), diff --git a/src/Dibi/Drivers/SqlsrvDriver.php b/src/Dibi/Drivers/SqlsrvDriver.php index 5cdcfd442..9f13f5e05 100644 --- a/src/Dibi/Drivers/SqlsrvDriver.php +++ b/src/Dibi/Drivers/SqlsrvDriver.php @@ -60,7 +60,7 @@ public function __construct(array $config) $options = $config['options']; // Default values - $options['CharacterSet'] = $options['CharacterSet'] ?? 'UTF-8'; + $options['CharacterSet'] ??= 'UTF-8'; $options['PWD'] = (string) $options['PWD']; $options['UID'] = (string) $options['UID']; $options['Database'] = (string) $options['Database']; diff --git a/src/Dibi/Event.php b/src/Dibi/Event.php index 0d0c7af41..ae4d307b3 100644 --- a/src/Dibi/Event.php +++ b/src/Dibi/Event.php @@ -61,7 +61,7 @@ public function __construct(Connection $connection, int $type, ?string $sql = nu $this->time = -microtime(true); if ($type === self::QUERY && preg_match('#\(?\s*(SELECT|UPDATE|INSERT|DELETE)#iA', $this->sql, $matches)) { - static $types = [ + $types = [ 'SELECT' => self::SELECT, 'UPDATE' => self::UPDATE, 'INSERT' => self::INSERT, 'DELETE' => self::DELETE, ]; diff --git a/src/Dibi/Helpers.php b/src/Dibi/Helpers.php index 0c1cddfdb..ffe3ed071 100644 --- a/src/Dibi/Helpers.php +++ b/src/Dibi/Helpers.php @@ -75,8 +75,8 @@ public static function dump($sql = null, bool $return = false): ?string $sql = \dibi::$sql; } - static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|FETCH\s+NEXT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE|START\s+TRANSACTION|BEGIN|COMMIT|ROLLBACK(?:\s+TO\s+SAVEPOINT)?|(?:RELEASE\s+)?SAVEPOINT'; - static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|RLIKE|REGEXP|TRUE|FALSE'; + $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|FETCH\s+NEXT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE|START\s+TRANSACTION|BEGIN|COMMIT|ROLLBACK(?:\s+TO\s+SAVEPOINT)?|(?:RELEASE\s+)?SAVEPOINT'; + $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|RLIKE|REGEXP|TRUE|FALSE'; // insert new lines $sql = " $sql "; @@ -162,7 +162,7 @@ public static function getSuggestion(array $items, string $value): ?string /** @internal */ public static function escape(Driver $driver, $value, string $type): string { - static $types = [ + $types = [ Type::TEXT => 'text', Type::BINARY => 'binary', Type::BOOL => 'bool', @@ -184,7 +184,7 @@ public static function escape(Driver $driver, $value, string $type): string */ public static function detectType(string $type): ?string { - static $patterns = [ + $patterns = [ '^_' => Type::TEXT, // PostgreSQL arrays 'RANGE$' => Type::TEXT, // PostgreSQL range types 'BYTEA|BLOB|BIN' => Type::BINARY, diff --git a/src/Dibi/Loggers/FileLogger.php b/src/Dibi/Loggers/FileLogger.php index 70d22d94e..5c53c387b 100644 --- a/src/Dibi/Loggers/FileLogger.php +++ b/src/Dibi/Loggers/FileLogger.php @@ -58,7 +58,7 @@ public function logEvent(Dibi\Event $event): void $this->writeToFile( $event, "ERROR: $message" - . "\n-- SQL: " . $event->sql + . "\n-- SQL: " . $event->sql, ); } else { $this->writeToFile( @@ -66,7 +66,7 @@ public function logEvent(Dibi\Event $event): void 'OK: ' . $event->sql . ($event->count ? ";\n-- rows: " . $event->count : '') . "\n-- takes: " . sprintf('%0.3f ms', $event->time * 1000) - . "\n-- source: " . implode(':', $event->source) + . "\n-- source: " . implode(':', $event->source), ); } } @@ -76,7 +76,7 @@ private function writeToFile(Dibi\Event $event, string $message): void { $driver = $event->connection->getConfig('driver'); $message .= - "\n-- driver: " . (is_object($driver) ? get_class($driver) : $driver) . '/' . $event->connection->getConfig('name') + "\n-- driver: " . (is_object($driver) ? $driver::class : $driver) . '/' . $event->connection->getConfig('name') . "\n-- " . date('Y-m-d H:i:s') . "\n\n"; file_put_contents($this->file, $message, FILE_APPEND | LOCK_EX); diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index 3257986f0..076c2ea32 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -206,7 +206,7 @@ final public function fetchSingle() */ final public function fetchAll(?int $offset = null, ?int $limit = null): array { - $limit = $limit ?? -1; + $limit ??= -1; $this->seek($offset ?: 0); $row = $this->fetch(); if (!$row) { diff --git a/src/Dibi/Strict.php b/src/Dibi/Strict.php index 17804f505..4d307c393 100644 --- a/src/Dibi/Strict.php +++ b/src/Dibi/Strict.php @@ -31,7 +31,7 @@ public function __call(string $name, array $args) { $class = method_exists($this, $name) ? 'parent' : static::class; $items = (new ReflectionClass($this))->getMethods(ReflectionMethod::IS_PUBLIC); - $items = array_map(function ($item) { return $item->getName(); }, $items); + $items = array_map(fn($item) => $item->getName(), $items); $hint = ($t = Helpers::getSuggestion($items, $name)) ? ", did you mean $t()?" : '.'; @@ -46,8 +46,8 @@ public function __call(string $name, array $args) public static function __callStatic(string $name, array $args) { $rc = new ReflectionClass(static::class); - $items = array_filter($rc->getMethods(\ReflectionMethod::IS_STATIC), function ($m) { return $m->isPublic(); }); - $items = array_map(function ($item) { return $item->getName(); }, $items); + $items = array_filter($rc->getMethods(\ReflectionMethod::IS_STATIC), fn($m) => $m->isPublic()); + $items = array_map(fn($item) => $item->getName(), $items); $hint = ($t = Helpers::getSuggestion($items, $name)) ? ", did you mean $t()?" : '.'; @@ -69,8 +69,8 @@ public function &__get(string $name) } $rc = new ReflectionClass($this); - $items = array_filter($rc->getProperties(ReflectionProperty::IS_PUBLIC), function ($p) { return !$p->isStatic(); }); - $items = array_map(function ($item) { return $item->getName(); }, $items); + $items = array_filter($rc->getProperties(ReflectionProperty::IS_PUBLIC), fn($p) => !$p->isStatic()); + $items = array_map(fn($item) => $item->getName(), $items); $hint = ($t = Helpers::getSuggestion($items, $name)) ? ", did you mean $$t?" : '.'; @@ -85,8 +85,8 @@ public function &__get(string $name) public function __set(string $name, $value) { $rc = new ReflectionClass($this); - $items = array_filter($rc->getProperties(ReflectionProperty::IS_PUBLIC), function ($p) { return !$p->isStatic(); }); - $items = array_map(function ($item) { return $item->getName(); }, $items); + $items = array_filter($rc->getProperties(ReflectionProperty::IS_PUBLIC), fn($p) => !$p->isStatic()); + $items = array_map(fn($item) => $item->getName(), $items); $hint = ($t = Helpers::getSuggestion($items, $name)) ? ", did you mean $$t?" : '.'; diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index d28bcdb19..ce72a08b7 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -96,22 +96,21 @@ public function translate(array $args): string // note: this can change $this->args & $this->cursor & ... . preg_replace_callback( <<<'XX' - / - (?=[`['":%?]) ## speed-up - (?: - `(.+?)`| ## 1) `identifier` - \[(.+?)\]| ## 2) [identifier] - (')((?:''|[^'])*)'| ## 3,4) string - (")((?:""|[^"])*)"| ## 5,6) "string" - ('|")| ## 7) lone quote - :(\S*?:)([a-zA-Z0-9._]?)| ## 8,9) :substitution: - %([a-zA-Z~][a-zA-Z0-9~]{0,5})| ## 10) modifier - (\?) ## 11) placeholder - )/xs -XX - , + / + (?=[`['":%?]) ## speed-up + (?: + `(.+?)`| ## 1) `identifier` + \[(.+?)\]| ## 2) [identifier] + (')((?:''|[^'])*)'| ## 3,4) string + (")((?:""|[^"])*)"| ## 5,6) "string" + ('|")| ## 7) lone quote + :(\S*?:)([a-zA-Z0-9._]?)| ## 8,9) :substitution: + %([a-zA-Z~][a-zA-Z0-9~]{0,5})| ## 10) modifier + (\?) ## 11) placeholder + )/xs + XX, [$this, 'cb'], - substr($arg, $toSkip) + substr($arg, $toSkip), ); if (preg_last_error()) { throw new PcreException; @@ -280,7 +279,7 @@ public function formatValue($value, ?string $modifier): string $proto = array_keys($v); } } else { - return $this->errors[] = '**Unexpected type ' . (is_object($v) ? get_class($v) : gettype($v)) . '**'; + return $this->errors[] = '**Unexpected type ' . (is_object($v) ? $v::class : gettype($v)) . '**'; } $pair = explode('%', $k, 2); // split into identifier & modifier @@ -349,7 +348,7 @@ public function formatValue($value, ?string $modifier): string ) { // continue } else { - $type = is_object($value) ? get_class($value) : gettype($value); + $type = is_object($value) ? $value::class : gettype($value); return $this->errors[] = "**Invalid combination of type $type and modifier %$modifier**"; } } @@ -437,20 +436,19 @@ public function formatValue($value, ?string $modifier): string $value = substr($value, 0, $toSkip) . preg_replace_callback( <<<'XX' - / - (?=[`['":]) - (?: - `(.+?)`| - \[(.+?)\]| - (')((?:''|[^'])*)'| - (")((?:""|[^"])*)"| - ('|")| - :(\S*?:)([a-zA-Z0-9._]?) - )/sx -XX - , + / + (?=[`['":]) + (?: + `(.+?)`| + \[(.+?)]| + (')((?:''|[^'])*)'| + (")((?:""|[^"])*)"| + (['"])| + :(\S*?:)([a-zA-Z0-9._]?) + )/sx + XX, [$this, 'cb'], - substr($value, $toSkip) + substr($value, $toSkip), ); if (preg_last_error()) { throw new PcreException; @@ -516,7 +514,7 @@ public function formatValue($value, ?string $modifier): string return $this->connection->translate(...$value->getValues()); } else { - $type = is_object($value) ? get_class($value) : gettype($value); + $type = is_object($value) ? $value::class : gettype($value); return $this->errors[] = "**Unexpected $type**"; } } diff --git a/src/Dibi/dibi.php b/src/Dibi/dibi.php index 821e9792f..3de249394 100644 --- a/src/Dibi/dibi.php +++ b/src/Dibi/dibi.php @@ -164,7 +164,7 @@ public static function dump($sql = null, bool $return = false): ?string */ public static function stripMicroseconds(DateTimeInterface $dt): DateTimeInterface { - $class = get_class($dt); + $class = $dt::class; return new $class($dt->format('Y-m-d H:i:s'), $dt->getTimezone()); } } diff --git a/tests/dibi/Connection.connect.phpt b/tests/dibi/Connection.connect.phpt index bfcc4e7aa..85cf56b52 100644 --- a/tests/dibi/Connection.connect.phpt +++ b/tests/dibi/Connection.connect.phpt @@ -73,24 +73,29 @@ test('', function () use ($config) { test('', function () use ($config) { - Assert::exception(function () use ($config) { - new Connection($config + ['onConnect' => '']); - }, InvalidArgumentException::class, "Configuration option 'onConnect' must be array."); - - $e = Assert::exception(function () use ($config) { - new Connection($config + ['onConnect' => ['STOP']]); - }, Dibi\DriverException::class); + Assert::exception( + fn() => new Connection($config + ['onConnect' => '']), + InvalidArgumentException::class, + "Configuration option 'onConnect' must be array.", + ); + + $e = Assert::exception( + fn() => new Connection($config + ['onConnect' => ['STOP']]), + Dibi\DriverException::class, + ); Assert::same('STOP', $e->getSql()); - $e = Assert::exception(function () use ($config) { - new Connection($config + ['onConnect' => [['STOP %i', 123]]]); - }, Dibi\DriverException::class); + $e = Assert::exception( + fn() => new Connection($config + ['onConnect' => [['STOP %i', 123]]]), + Dibi\DriverException::class, + ); Assert::same('STOP 123', $e->getSql()); // lazy $conn = new Connection($config + ['lazy' => true, 'onConnect' => ['STOP']]); - $e = Assert::exception(function () use ($conn) { - $conn->query('SELECT 1'); - }, Dibi\DriverException::class); + $e = Assert::exception( + fn() => $conn->query('SELECT 1'), + Dibi\DriverException::class, + ); Assert::same('STOP', $e->getSql()); }); diff --git a/tests/dibi/Connection.fetch.phpt b/tests/dibi/Connection.fetch.phpt index 7b2a01e84..12c1751a5 100644 --- a/tests/dibi/Connection.fetch.phpt +++ b/tests/dibi/Connection.fetch.phpt @@ -33,13 +33,13 @@ Assert::equal([ $res = $conn->query('SELECT * FROM [products] ORDER BY product_id'); Assert::same( [1 => 'Chair', 'Table', 'Computer'], - $res->fetchPairs('product_id', 'title') + $res->fetchPairs('product_id', 'title'), ); $res = $conn->query('SELECT * FROM [products] ORDER BY product_id'); Assert::same( [1 => 'Chair', 'Table', 'Computer'], - $res->fetchPairs() + $res->fetchPairs(), ); diff --git a/tests/dibi/Connection.substitutions.phpt b/tests/dibi/Connection.substitutions.phpt index 2c1a98f2f..a50fa5f82 100644 --- a/tests/dibi/Connection.substitutions.phpt +++ b/tests/dibi/Connection.substitutions.phpt @@ -14,27 +14,27 @@ $conn->getSubstitutes()->blog = 'wp_'; Assert::same( reformat('UPDATE wp_items SET [val]=1'), - $conn->translate('UPDATE :blog:items SET [val]=1') + $conn->translate('UPDATE :blog:items SET [val]=1'), ); Assert::same( reformat('UPDATE [wp_items] SET [val]=1'), - $conn->translate('UPDATE [:blog:items] SET [val]=1') + $conn->translate('UPDATE [:blog:items] SET [val]=1'), ); Assert::same( reformat("UPDATE 'wp_' SET [val]=1"), - $conn->translate('UPDATE :blog: SET [val]=1') + $conn->translate('UPDATE :blog: SET [val]=1'), ); Assert::same( reformat("UPDATE ':blg:' SET [val]=1"), - $conn->translate('UPDATE :blg: SET [val]=1') + $conn->translate('UPDATE :blg: SET [val]=1'), ); Assert::same( reformat("UPDATE table SET [text]=':blog:a'"), - $conn->translate("UPDATE table SET [text]=':blog:a'") + $conn->translate("UPDATE table SET [text]=':blog:a'"), ); @@ -43,16 +43,14 @@ $conn->getSubstitutes()->{''} = 'my_'; Assert::same( reformat('UPDATE my_table SET [val]=1'), - $conn->translate('UPDATE ::table SET [val]=1') + $conn->translate('UPDATE ::table SET [val]=1'), ); // create substitutions using fallback callback -$conn->getSubstitutes()->setCallback(function ($expr) { - return '_' . $expr . '_'; -}); +$conn->getSubstitutes()->setCallback(fn($expr) => '_' . $expr . '_'); Assert::same( reformat('UPDATE _account_user SET [val]=1'), - $conn->translate('UPDATE :account:user SET [val]=1') + $conn->translate('UPDATE :account:user SET [val]=1'), ); diff --git a/tests/dibi/Connection.transactions.phpt b/tests/dibi/Connection.transactions.phpt index bc01c617f..b021fab1a 100644 --- a/tests/dibi/Connection.transactions.phpt +++ b/tests/dibi/Connection.transactions.phpt @@ -15,18 +15,22 @@ $conn = new Dibi\Connection($config); $conn->loadFile(__DIR__ . "/data/$config[system].sql"); -/*Assert::exception(function () use ($conn) { - $conn->rollback(); -}, Dibi\Exception::class); +/* +Assert::exception( + fn() => $conn->rollback(), + Dibi\Exception::class, +); -Assert::exception(function () use ($conn) { - $conn->commit(); -}, Dibi\Exception::class); +Assert::exception( + fn() => $conn->commit(), + Dibi\Exception::class, +); $conn->begin(); -Assert::exception(function () use ($conn) { - $conn->begin(); -}, Dibi\Exception::class); +Assert::exception( + fn() => $conn->begin(), + Dibi\Exception::class, +); */ @@ -53,14 +57,16 @@ test('begin() & commit()', function () use ($conn) { test('transaction() fail', function () use ($conn) { - Assert::exception(function () use ($conn) { - $conn->transaction(function (Dibi\Connection $connection) { + Assert::exception( + fn() => $conn->transaction(function (Dibi\Connection $connection) { $connection->query('INSERT INTO [products]', [ 'title' => 'Test product', ]); throw new Exception('my exception'); - }); - }, Throwable::class, 'my exception'); + }), + Throwable::class, + 'my exception', + ); Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle()); }); @@ -76,8 +82,8 @@ test('transaction() success', function () use ($conn) { test('nested transaction() call fail', function () use ($conn) { - Assert::exception(function () use ($conn) { - $conn->transaction(function (Dibi\Connection $connection) { + Assert::exception( + fn() => $conn->transaction(function (Dibi\Connection $connection) { $connection->query('INSERT INTO [products]', [ 'title' => 'Test product', ]); @@ -88,8 +94,10 @@ test('nested transaction() call fail', function () use ($conn) { ]); throw new Exception('my exception'); }); - }); - }, Throwable::class, 'my exception'); + }), + Throwable::class, + 'my exception', + ); Assert::same(5, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle()); }); @@ -111,21 +119,27 @@ test('nested transaction() call success', function () use ($conn) { test('begin(), commit() & rollback() calls are forbidden in transaction()', function () use ($conn) { - Assert::exception(function () use ($conn) { - $conn->transaction(function (Dibi\Connection $connection) { + Assert::exception( + fn() => $conn->transaction(function (Dibi\Connection $connection) { $connection->begin(); - }); - }, LogicException::class, Dibi\Connection::class . '::begin() call is forbidden inside a transaction() callback'); + }), + LogicException::class, + Dibi\Connection::class . '::begin() call is forbidden inside a transaction() callback', + ); - Assert::exception(function () use ($conn) { - $conn->transaction(function (Dibi\Connection $connection) { + Assert::exception( + fn() => $conn->transaction(function (Dibi\Connection $connection) { $connection->commit(); - }); - }, LogicException::class, Dibi\Connection::class . '::commit() call is forbidden inside a transaction() callback'); + }), + LogicException::class, + Dibi\Connection::class . '::commit() call is forbidden inside a transaction() callback', + ); - Assert::exception(function () use ($conn) { - $conn->transaction(function (Dibi\Connection $connection) { + Assert::exception( + fn() => $conn->transaction(function (Dibi\Connection $connection) { $connection->rollback(); - }); - }, LogicException::class, Dibi\Connection::class . '::rollback() call is forbidden inside a transaction() callback'); + }), + LogicException::class, + Dibi\Connection::class . '::rollback() call is forbidden inside a transaction() callback', + ); }); diff --git a/tests/dibi/DataSource.phpt b/tests/dibi/DataSource.phpt index c71907180..044815fdc 100644 --- a/tests/dibi/DataSource.phpt +++ b/tests/dibi/DataSource.phpt @@ -17,7 +17,7 @@ Assert::match( reformat(' SELECT * FROM (SELECT * FROM products) t'), - (string) $ds + (string) $ds, ); @@ -25,7 +25,7 @@ Assert::same(3, $ds->count()); Assert::same(3, $ds->getTotalCount()); Assert::same( reformat('SELECT COUNT(*) FROM (SELECT * FROM products) t'), - dibi::$sql + dibi::$sql, ); @@ -39,7 +39,7 @@ FROM (SELECT * FROM products) t WHERE (title like '%a%') ORDER BY [title] DESC "), - (string) $ds + (string) $ds, ); @@ -53,7 +53,7 @@ FROM (SELECT * FROM products) t WHERE (title like '%a%') AND (product_id = 1) ORDER BY [title] DESC, [product_id] ASC "), - (string) $ds + (string) $ds, ); @@ -67,7 +67,7 @@ FROM (SELECT * FROM products) t WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1) ORDER BY [product_id] ASC "), - (string) $ds + (string) $ds, ); @@ -95,7 +95,7 @@ FROM (SELECT * FROM products) t WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1) ORDER BY [product_id] ASC "), - dibi::$sql + dibi::$sql, ); @@ -108,7 +108,7 @@ FROM (SELECT * FROM products) t WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1) ORDER BY [product_id] ASC ) t"), - (string) $fluent + (string) $fluent, ); @@ -117,7 +117,7 @@ Assert::match( reformat(' SELECT * FROM (SELECT [title] FROM [products]) t'), - (string) $ds + (string) $ds, ); Assert::equal(new Row([ @@ -129,7 +129,7 @@ Assert::same(1, $conn->dataSource('SELECT * FROM products ORDER BY product_id')- Assert::same( [1 => 'Chair', 'Table', 'Computer'], - $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchPairs() + $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchPairs(), ); Assert::equal([ @@ -154,7 +154,7 @@ Assert::match( reformat(' SELECT * FROM [products]'), - (string) $ds + (string) $ds, ); Assert::same(3, $ds->count()); diff --git a/tests/dibi/DateTime.phpt b/tests/dibi/DateTime.phpt index 281be3c8e..523bef91c 100644 --- a/tests/dibi/DateTime.phpt +++ b/tests/dibi/DateTime.phpt @@ -10,10 +10,10 @@ require __DIR__ . '/bootstrap.php'; date_default_timezone_set('Europe/Prague'); -Assert::same('1978-01-23 11:40:00.000000', (string) new DateTime(254400000)); -Assert::same('1978-01-23 11:40:00.000000', (string) (new DateTime)->setTimestamp(254400000)); -Assert::same(254400000, (new DateTime(254400000))->getTimestamp()); +Assert::same('1978-01-23 11:40:00.000000', (string) new DateTime(254_400_000)); +Assert::same('1978-01-23 11:40:00.000000', (string) (new DateTime)->setTimestamp(254_400_000)); +Assert::same(254_400_000, (new DateTime(254_400_000))->getTimestamp()); -Assert::same(is_int(2544000000) ? 2544000000 : '2544000000', (new DateTime(2544000000))->getTimestamp()); // 64 bit +Assert::same(is_int(2_544_000_000) ? 2_544_000_000 : '2544000000', (new DateTime(2_544_000_000))->getTimestamp()); // 64 bit Assert::same('1978-05-05 00:00:00.000000', (string) new DateTime('1978-05-05')); diff --git a/tests/dibi/Fluent.delete.phpt b/tests/dibi/Fluent.delete.phpt index 174f3ab32..23fd79f0a 100644 --- a/tests/dibi/Fluent.delete.phpt +++ b/tests/dibi/Fluent.delete.phpt @@ -15,33 +15,33 @@ $fluent = $conn->delete('table')->as('bAlias') Assert::same( reformat('DELETE IGNORE FROM [table] AS [bAlias]'), - (string) $fluent + (string) $fluent, ); $fluent->removeClause('from')->from('anotherTable'); Assert::same( reformat('DELETE IGNORE FROM [anotherTable]'), - (string) $fluent + (string) $fluent, ); $fluent->using('thirdTable'); Assert::same( reformat('DELETE IGNORE FROM [anotherTable] USING [thirdTable]'), - (string) $fluent + (string) $fluent, ); $fluent->setFlag('IGNORE', false); Assert::same( reformat('DELETE FROM [anotherTable] USING [thirdTable]'), - (string) $fluent + (string) $fluent, ); $fluent->limit(10); Assert::same( reformat('DELETE FROM [anotherTable] USING [thirdTable] LIMIT 10'), - (string) $fluent + (string) $fluent, ); diff --git a/tests/dibi/Fluent.fetch.limit.mssql.phpt b/tests/dibi/Fluent.fetch.limit.mssql.phpt index fc5b85870..7df79eb10 100644 --- a/tests/dibi/Fluent.fetch.limit.mssql.phpt +++ b/tests/dibi/Fluent.fetch.limit.mssql.phpt @@ -58,28 +58,28 @@ $fluent = $conn->select('*') Assert::same( reformat('SELECT TOP (1) * FROM (SELECT * FROM [customers] ORDER BY [customer_id]) t'), - (string) $fluent + (string) $fluent, ); $fluent->fetch(); Assert::same( 'SELECT TOP (1) * FROM (SELECT * FROM [customers] ORDER BY [customer_id]) t', - dibi::$sql + dibi::$sql, ); $fluent->fetchSingle(); Assert::same( reformat('SELECT TOP (1) * FROM (SELECT * FROM [customers] ORDER BY [customer_id]) t'), - dibi::$sql + dibi::$sql, ); $fluent->fetchAll(0, 3); Assert::same( reformat('SELECT TOP (3) * FROM (SELECT * FROM [customers] ORDER BY [customer_id]) t'), - dibi::$sql + dibi::$sql, ); Assert::same( reformat('SELECT TOP (1) * FROM (SELECT * FROM [customers] ORDER BY [customer_id]) t'), - (string) $fluent + (string) $fluent, ); @@ -87,16 +87,16 @@ $fluent->limit(0); $fluent->fetch(); Assert::same( reformat('SELECT TOP (0) * FROM (SELECT * FROM [customers] ORDER BY [customer_id]) t'), - dibi::$sql + dibi::$sql, ); $fluent->fetchSingle(); Assert::same( reformat('SELECT TOP (0) * FROM (SELECT * FROM [customers] ORDER BY [customer_id]) t'), - dibi::$sql + dibi::$sql, ); Assert::same( reformat('SELECT TOP (0) * FROM (SELECT * FROM [customers] ORDER BY [customer_id]) t'), - (string) $fluent + (string) $fluent, ); @@ -105,14 +105,14 @@ $fluent->removeClause('offset'); $fluent->fetch(); Assert::same( reformat('SELECT TOP (1) * FROM (SELECT * FROM [customers] ORDER BY [customer_id]) t'), - dibi::$sql + dibi::$sql, ); $fluent->fetchSingle(); Assert::same( reformat('SELECT TOP (1) * FROM (SELECT * FROM [customers] ORDER BY [customer_id]) t'), - dibi::$sql + dibi::$sql, ); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id]'), - (string) $fluent + (string) $fluent, ); diff --git a/tests/dibi/Fluent.fetch.limit.phpt b/tests/dibi/Fluent.fetch.limit.phpt index f8f4f5d52..dd533e1ec 100644 --- a/tests/dibi/Fluent.fetch.limit.phpt +++ b/tests/dibi/Fluent.fetch.limit.phpt @@ -23,28 +23,28 @@ $fluent = $conn->select('*') Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), - (string) $fluent + (string) $fluent, ); $fluent->fetch(); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), - dibi::$sql + dibi::$sql, ); $fluent->fetchSingle(); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), - dibi::$sql + dibi::$sql, ); $fluent->fetchAll(2, 3); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 3 OFFSET 2'), - dibi::$sql + dibi::$sql, ); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), - (string) $fluent + (string) $fluent, ); @@ -52,16 +52,16 @@ $fluent->limit(0); $fluent->fetch(); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), - dibi::$sql + dibi::$sql, ); $fluent->fetchSingle(); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), - dibi::$sql + dibi::$sql, ); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), - (string) $fluent + (string) $fluent, ); @@ -69,16 +69,16 @@ $fluent->removeClause('limit'); $fluent->fetch(); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), - dibi::$sql + dibi::$sql, ); $fluent->fetchSingle(); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), - dibi::$sql + dibi::$sql, ); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 18446744073709551615 OFFSET 3'), - (string) $fluent + (string) $fluent, ); @@ -86,14 +86,14 @@ $fluent->removeClause('offset'); $fluent->fetch(); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), - dibi::$sql + dibi::$sql, ); $fluent->fetchSingle(); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), - dibi::$sql + dibi::$sql, ); Assert::same( reformat('SELECT * FROM [customers] ORDER BY [customer_id]'), - (string) $fluent + (string) $fluent, ); diff --git a/tests/dibi/Fluent.insert.phpt b/tests/dibi/Fluent.insert.phpt index d43b65b02..a68aa7e85 100644 --- a/tests/dibi/Fluent.insert.phpt +++ b/tests/dibi/Fluent.insert.phpt @@ -21,40 +21,40 @@ $fluent = $conn->insert('table', $arr) Assert::same( reformat('INSERT IGNORE DELAYED INTO [table] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL)'), - (string) $fluent + (string) $fluent, ); $fluent->setFlag('IGNORE', false); Assert::same( reformat('INSERT DELAYED INTO [table] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL)'), - (string) $fluent + (string) $fluent, ); $fluent->setFlag('HIGH_priority'); Assert::same( reformat('INSERT DELAYED HIGH_PRIORITY INTO [table] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL)'), - (string) $fluent + (string) $fluent, ); $fluent->into('anotherTable'); Assert::same( reformat('INSERT DELAYED HIGH_PRIORITY INTO [anotherTable] VALUES (\'Super Product\', 12, NULL)'), - (string) $fluent + (string) $fluent, ); $fluent->values('%l', $arr); Assert::same( reformat('INSERT DELAYED HIGH_PRIORITY INTO [anotherTable] VALUES (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL)'), - (string) $fluent + (string) $fluent, ); $fluent->values($arr); Assert::same( reformat('INSERT DELAYED HIGH_PRIORITY INTO [anotherTable] VALUES (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL)'), - (string) $fluent + (string) $fluent, ); diff --git a/tests/dibi/Fluent.select.phpt b/tests/dibi/Fluent.select.phpt index 17a1f914c..7b9ba1855 100644 --- a/tests/dibi/Fluent.select.phpt +++ b/tests/dibi/Fluent.select.phpt @@ -25,7 +25,7 @@ $fluent = $conn->select('*') Assert::same( reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d]'), - (string) $fluent + (string) $fluent, ); $fluent->from('table')->as('table.Alias') @@ -34,21 +34,21 @@ $fluent->from('table')->as('table.Alias') Assert::same( reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [table] AS [table.Alias] INNER JOIN [table1] ON table.col = table1.col INNER JOIN [table2] ON table.col = table2.col'), - (string) $fluent + (string) $fluent, ); $fluent->from('anotherTable'); Assert::same( reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [table] AS [table.Alias] INNER JOIN [table1] ON table.col = table1.col INNER JOIN [table2] ON table.col = table2.col , [anotherTable]'), - (string) $fluent + (string) $fluent, ); $fluent->removeClause('from')->from('anotherTable'); Assert::same( reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable]'), - (string) $fluent + (string) $fluent, ); $fluent->as('anotherAlias') @@ -58,7 +58,7 @@ $fluent->as('anotherAlias') Assert::same( reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col'), - (string) $fluent + (string) $fluent, ); $fluent->where('col > %i', $max) @@ -71,14 +71,14 @@ $fluent->where('col > %i', $max) Assert::same( reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3) ORDER BY [val] ASC , [val2] DESC , [val3] DESC'), - (string) $fluent + (string) $fluent, ); $fluent->orderBy(Dibi\Fluent::REMOVE); Assert::same( reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3)'), - (string) $fluent + (string) $fluent, ); @@ -86,7 +86,7 @@ $fluent = $conn->select('*') ->select( $conn->select('count(*)') ->from('precteni')->as('P') - ->where('P.id_clanku', '=', 'C.id_clanku') + ->where('P.id_clanku', '=', 'C.id_clanku'), ) ->from('clanky')->as('C') ->where('id_clanku=%i', 123) @@ -99,7 +99,7 @@ Assert::same( 'sqlsrv' => 'SELECT * , (SELECT count(*) FROM [precteni] AS [P] WHERE P.id_clanku = C.id_clanku) FROM [clanky] AS [C] WHERE id_clanku=123 OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY', 'SELECT * , (SELECT count(*) FROM [precteni] AS [P] WHERE P.id_clanku = C.id_clanku) FROM [clanky] AS [C] WHERE id_clanku=123 LIMIT 1', ]), - (string) $fluent + (string) $fluent, ); @@ -112,7 +112,7 @@ $fluent = $conn->select('*') Assert::same( reformat('SELECT * , [x] AS [xAlias] FROM [products] INNER JOIN [orders] USING (product_id) INNER JOIN [customers] USING ([customer_id]) INNER JOIN [items] USING ([customer_id], [order_id])'), - (string) $fluent + (string) $fluent, ); @@ -124,7 +124,7 @@ $fluent = $conn->command()->select() Assert::same( reformat('SELECT * FROM [products] INNER JOIN [orders] USING (product_id)'), - (string) $fluent + (string) $fluent, ); @@ -138,7 +138,7 @@ Assert::same( 'sqlsrv' => "SELECT * FROM [me] AS [t] WHERE col > 10 AND ([x] = N'a') AND (b) AND (c)", "SELECT * FROM [me] AS [t] WHERE col > 10 AND ([x] = 'a') AND (b) AND (c)", ]), - (string) $fluent + (string) $fluent, ); @@ -158,5 +158,5 @@ $fluent = $conn->select('*')->from('abc') Assert::same( reformat('SELECT * FROM [abc] WHERE x IN ((SELECT [id] FROM [xyz]))'), - (string) $fluent + (string) $fluent, ); diff --git a/tests/dibi/Fluent.update.phpt b/tests/dibi/Fluent.update.phpt index af560dd1c..822078574 100644 --- a/tests/dibi/Fluent.update.phpt +++ b/tests/dibi/Fluent.update.phpt @@ -21,14 +21,14 @@ $fluent = $conn->update('table', $arr) Assert::same( reformat('UPDATE IGNORE DELAYED [table] SET [title]=\'Super Product\', [price]=12, [brand]=NULL'), - (string) $fluent + (string) $fluent, ); $fluent->set(['another' => 123]); Assert::same( reformat('UPDATE IGNORE DELAYED [table] SET [title]=\'Super Product\', [price]=12, [brand]=NULL , [another]=123'), - (string) $fluent + (string) $fluent, ); @@ -40,5 +40,5 @@ $arr = [ $fluent = $conn->update(['table1', 'table2'], $arr); Assert::same( reformat('UPDATE [table1], [table2] SET [table1].[title]=\'Super Product\', [table2].[price]=12, [table2].[brand]=NULL'), - (string) $fluent + (string) $fluent, ); diff --git a/tests/dibi/HashMap.phpt b/tests/dibi/HashMap.phpt index f14cfc1ec..582ce63e4 100644 --- a/tests/dibi/HashMap.phpt +++ b/tests/dibi/HashMap.phpt @@ -6,9 +6,7 @@ use Tester\Assert; require __DIR__ . '/bootstrap.php'; -$hash = new Dibi\HashMap(function ($v) { - return "b-$v-e"; -}); +$hash = new Dibi\HashMap(fn($v) => "b-$v-e"); Assert::same('b-X-e', $hash->{'X'}); Assert::same('b--e', $hash->{''}); diff --git a/tests/dibi/Helpers.intVal().phpt b/tests/dibi/Helpers.intVal().phpt index 42daf9114..bc350bafe 100644 --- a/tests/dibi/Helpers.intVal().phpt +++ b/tests/dibi/Helpers.intVal().phpt @@ -12,22 +12,32 @@ Assert::same(0, Helpers::intVal(0)); Assert::same(0, Helpers::intVal('0')); Assert::same(-10, Helpers::intVal('-10')); -Assert::exception(function () { - Helpers::intVal('12345678901234567890123456879'); -}, Dibi\Exception::class, 'Number 12345678901234567890123456879 is greater than integer.'); - -Assert::exception(function () { - Helpers::intVal('-12345678901234567890123456879'); -}, Dibi\Exception::class, 'Number -12345678901234567890123456879 is greater than integer.'); - -Assert::exception(function () { - Helpers::intVal(''); -}, Dibi\Exception::class, "Expected number, '' given."); - -Assert::exception(function () { - Helpers::intVal('not number'); -}, Dibi\Exception::class, "Expected number, 'not number' given."); - -Assert::exception(function () { - Helpers::intVal(null); -}, Dibi\Exception::class, "Expected number, '' given."); +Assert::exception( + fn() => Helpers::intVal('12345678901234567890123456879'), + Dibi\Exception::class, + 'Number 12345678901234567890123456879 is greater than integer.', +); + +Assert::exception( + fn() => Helpers::intVal('-12345678901234567890123456879'), + Dibi\Exception::class, + 'Number -12345678901234567890123456879 is greater than integer.', +); + +Assert::exception( + fn() => Helpers::intVal(''), + Dibi\Exception::class, + "Expected number, '' given.", +); + +Assert::exception( + fn() => Helpers::intVal('not number'), + Dibi\Exception::class, + "Expected number, 'not number' given.", +); + +Assert::exception( + fn() => Helpers::intVal(null), + Dibi\Exception::class, + "Expected number, '' given.", +); diff --git a/tests/dibi/PdoDriver.providedConnection.phpt b/tests/dibi/PdoDriver.providedConnection.phpt index dfec0a747..41b0d5137 100644 --- a/tests/dibi/PdoDriver.providedConnection.phpt +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -19,17 +19,22 @@ function buildPdoDriver(?int $errorMode) // PDO error mode: exception -Assert::exception(function () { - buildPdoDriver(PDO::ERRMODE_EXCEPTION); -}, Dibi\DriverException::class, 'PDO connection in exception or warning error mode is not supported.'); +Assert::exception( + fn() => buildPdoDriver(PDO::ERRMODE_EXCEPTION), + Dibi\DriverException::class, + 'PDO connection in exception or warning error mode is not supported.', +); // PDO error mode: warning -Assert::exception(function () { - buildPdoDriver(PDO::ERRMODE_WARNING); -}, Dibi\DriverException::class, 'PDO connection in exception or warning error mode is not supported.'); +Assert::exception( + fn() => buildPdoDriver(PDO::ERRMODE_WARNING), + Dibi\DriverException::class, + 'PDO connection in exception or warning error mode is not supported.', +); -test('PDO error mode: explicitly set silent', function () { - buildPdoDriver(PDO::ERRMODE_SILENT); -}); +test( + 'PDO error mode: explicitly set silent', + fn() => buildPdoDriver(PDO::ERRMODE_SILENT) +); diff --git a/tests/dibi/Result.meta.phpt b/tests/dibi/Result.meta.phpt index d3310f603..9aa66ce3d 100644 --- a/tests/dibi/Result.meta.phpt +++ b/tests/dibi/Result.meta.phpt @@ -28,14 +28,14 @@ Assert::same(4, $res->getColumnCount()); Assert::same( ['product_id', 'order_id', 'name', 'xXx'], - $info->getColumnNames() + $info->getColumnNames(), ); if (!in_array($config['driver'], ['sqlite', 'sqlite3', 'pdo', 'sqlsrv'], true)) { Assert::same( ['products.product_id', 'orders.order_id', 'customers.name', 'xXx'], - $info->getColumnNames(true) + $info->getColumnNames(true), ); } diff --git a/tests/dibi/Result.normalize.phpt b/tests/dibi/Result.normalize.phpt index 2c810f530..cd8484656 100644 --- a/tests/dibi/Result.normalize.phpt +++ b/tests/dibi/Result.normalize.phpt @@ -162,9 +162,10 @@ test('', function () { if (PHP_VERSION_ID < 80000) { Assert::same(['col' => 0], @$result->test(['col' => ''])); // triggers warning since PHP 7.1 } else { - Assert::exception(function () use ($result) { - Assert::same(['col' => 0], $result->test(['col' => ''])); - }, TypeError::class); + Assert::exception( + fn() => Assert::same(['col' => 0], $result->test(['col' => ''])), + TypeError::class, + ); } Assert::same(['col' => 0], $result->test(['col' => '0'])); @@ -189,9 +190,10 @@ test('', function () { $result->setType('col', Type::DATETIME); Assert::same(['col' => null], $result->test(['col' => null])); - Assert::exception(function () use ($result) { - $result->test(['col' => true]); - }, TypeError::class); + Assert::exception( + fn() => $result->test(['col' => true]), + TypeError::class, + ); Assert::same(['col' => null], $result->test(['col' => false])); Assert::same(['col' => null], $result->test(['col' => ''])); @@ -208,9 +210,10 @@ test('', function () { $result->setFormat(Type::DATETIME, 'Y-m-d H:i:s'); Assert::same(['col' => null], $result->test(['col' => null])); - Assert::exception(function () use ($result) { - $result->test(['col' => true]); - }, TypeError::class); + Assert::exception( + fn() => $result->test(['col' => true]), + TypeError::class, + ); Assert::same(['col' => null], $result->test(['col' => false])); Assert::same(['col' => null], $result->test(['col' => ''])); @@ -226,9 +229,10 @@ test('', function () { $result->setType('col', Type::DATE); Assert::same(['col' => null], $result->test(['col' => null])); - Assert::exception(function () use ($result) { - $result->test(['col' => true]); - }, TypeError::class); + Assert::exception( + fn() => $result->test(['col' => true]), + TypeError::class, + ); Assert::same(['col' => null], $result->test(['col' => false])); Assert::same(['col' => null], $result->test(['col' => ''])); @@ -242,9 +246,10 @@ test('', function () { $result->setType('col', Type::TIME); Assert::same(['col' => null], $result->test(['col' => null])); - Assert::exception(function () use ($result) { - $result->test(['col' => true]); - }, TypeError::class); + Assert::exception( + fn() => $result->test(['col' => true]), + TypeError::class, + ); Assert::same(['col' => null], $result->test(['col' => false])); Assert::same(['col' => null], $result->test(['col' => ''])); diff --git a/tests/dibi/Sqlsrv.insert.phpt b/tests/dibi/Sqlsrv.insert.phpt index e0334c9e2..30947a5f5 100644 --- a/tests/dibi/Sqlsrv.insert.phpt +++ b/tests/dibi/Sqlsrv.insert.phpt @@ -25,7 +25,7 @@ $conn->query( 'CREATE TRIGGER %n ON %n AFTER INSERT AS INSERT INTO %n DEFAULT VALUES', 'UpdAAB', 'aab', - 'aaa' + 'aaa', ); $conn->query('INSERT INTO %n DEFAULT VALUES', 'aab'); diff --git a/tests/dibi/Sqlsrv.limits.phpt b/tests/dibi/Sqlsrv.limits.phpt index 3b0203428..f70ea1971 100644 --- a/tests/dibi/Sqlsrv.limits.phpt +++ b/tests/dibi/Sqlsrv.limits.phpt @@ -21,19 +21,19 @@ $tests = function ($conn) { // Limit and offset Assert::same( 'SELECT 1 OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY', - $conn->translate('SELECT 1 %ofs %lmt', 10, 10) + $conn->translate('SELECT 1 %ofs %lmt', 10, 10), ); // Limit only Assert::same( 'SELECT 1 OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY', - $conn->translate('SELECT 1 %lmt', 10) + $conn->translate('SELECT 1 %lmt', 10), ); // Offset only Assert::same( 'SELECT 1 OFFSET 10 ROWS', - $conn->translate('SELECT 1 %ofs', 10) + $conn->translate('SELECT 1 %ofs', 10), ); // Offset invalid @@ -42,7 +42,7 @@ $tests = function ($conn) { $conn->translate('SELECT 1 %ofs', -10); }, Dibi\NotSupportedException::class, - 'Negative offset or limit.' + 'Negative offset or limit.', ); // Limit invalid @@ -51,7 +51,7 @@ $tests = function ($conn) { $conn->translate('SELECT 1 %lmt', -10); }, Dibi\NotSupportedException::class, - 'Negative offset or limit.' + 'Negative offset or limit.', ); // Limit invalid, offset valid @@ -60,7 +60,7 @@ $tests = function ($conn) { $conn->translate('SELECT 1 %ofs %lmt', 10, -10); }, Dibi\NotSupportedException::class, - 'Negative offset or limit.' + 'Negative offset or limit.', ); // Limit valid, offset invalid @@ -69,22 +69,23 @@ $tests = function ($conn) { $conn->translate('SELECT 1 %ofs %lmt', -10, 10); }, Dibi\NotSupportedException::class, - 'Negative offset or limit.' + 'Negative offset or limit.', ); } else { Assert::same( 'SELECT TOP (1) * FROM (SELECT 1) t', - $conn->translate('SELECT 1 %lmt', 1) + $conn->translate('SELECT 1 %lmt', 1), ); Assert::same( 'SELECT 1', - $conn->translate('SELECT 1 %lmt', -10) + $conn->translate('SELECT 1 %lmt', -10), ); - Assert::exception(function () { - $conn->translate('SELECT 1 %ofs %lmt', 10, 10); - }, Dibi\NotSupportedException::class); + Assert::exception( + fn() => $conn->translate('SELECT 1 %ofs %lmt', 10, 10), + Dibi\NotSupportedException::class, + ); } }; diff --git a/tests/dibi/Translator.DateInterval.phpt b/tests/dibi/Translator.DateInterval.phpt index 0e7d295c6..bfd1b739a 100644 --- a/tests/dibi/Translator.DateInterval.phpt +++ b/tests/dibi/Translator.DateInterval.phpt @@ -13,13 +13,16 @@ switch ($config['system']) { case 'mysql': Assert::equal('10:20:30.0', $translator->formatValue(new DateInterval('PT10H20M30S'), null)); Assert::equal('-1:00:00.0', $translator->formatValue(DateInterval::createFromDateString('-1 hour'), null)); - Assert::exception(function () use ($translator) { - $translator->formatValue(new DateInterval('P2Y4DT6H8M'), null); - }, Dibi\NotSupportedException::class, 'Only time interval is supported.'); + Assert::exception( + fn() => $translator->formatValue(new DateInterval('P2Y4DT6H8M'), null), + Dibi\NotSupportedException::class, + 'Only time interval is supported.', + ); break; default: - Assert::exception(function () use ($translator) { - $translator->formatValue(new DateInterval('PT10H20M30S'), null); - }, Dibi\Exception::class); + Assert::exception( + fn() => $translator->formatValue(new DateInterval('PT10H20M30S'), null), + Dibi\Exception::class, + ); } diff --git a/tests/dibi/Translator.conditions.phpt b/tests/dibi/Translator.conditions.phpt index 0f356ca17..90123944c 100644 --- a/tests/dibi/Translator.conditions.phpt +++ b/tests/dibi/Translator.conditions.phpt @@ -27,8 +27,8 @@ FROM [customers] isset($name), 'WHERE [name] LIKE %s', 'xxx', - '%end' - ) + '%end', + ), ); @@ -42,8 +42,8 @@ FROM [customers] /* ... */'), SELECT * FROM %if', true, - '[customers] %else [products]' - ) + '[customers] %else [products]', + ), ); @@ -62,7 +62,7 @@ FROM [people] WHERE [id] > 0 %if', false, 'AND [foo]=%i', 1, ' %else %if', true, 'AND [bar]=%i', 1, ' -') +'), ); @@ -97,8 +97,8 @@ WHERE %if', false, 'AND [admin]=1 %end - %else 1 LIMIT 10 %end' - ) + %else 1 LIMIT 10 %end', + ), ); @@ -113,6 +113,6 @@ Assert::same( 3, '%ofs', 5, - '%end' - ) + '%end', + ), ); diff --git a/tests/dibi/Translator.identifiers.phpt b/tests/dibi/Translator.identifiers.phpt index d842bc083..d86344124 100644 --- a/tests/dibi/Translator.identifiers.phpt +++ b/tests/dibi/Translator.identifiers.phpt @@ -15,35 +15,35 @@ $conn = new Dibi\Connection($config); Assert::same( reformat('SELECT * FROM where WHERE select < 2'), - $conn->translate('SELECT * FROM where WHERE select < 2') + $conn->translate('SELECT * FROM where WHERE select < 2'), ); Assert::same( reformat('SELECT * FROM [where] WHERE where.select < 2'), - $conn->translate('SELECT * FROM [where] WHERE where.select < 2') + $conn->translate('SELECT * FROM [where] WHERE where.select < 2'), ); Assert::same( reformat('SELECT * FROM [where] WHERE [where].[select] < 2'), - $conn->translate('SELECT * FROM [where] WHERE [where.select] < 2') + $conn->translate('SELECT * FROM [where] WHERE [where.select] < 2'), ); Assert::same( reformat('SELECT * FROM [where] as [temp] WHERE [temp].[select] < 2'), - $conn->translate('SELECT * FROM [where] as [temp] WHERE [temp.select] < 2') + $conn->translate('SELECT * FROM [where] as [temp] WHERE [temp.select] < 2'), ); Assert::same( reformat('SELECT * FROM [where] WHERE [quot\'n\' space] > 2'), - $conn->translate("SELECT * FROM [where] WHERE [quot'n' space] > 2") + $conn->translate("SELECT * FROM [where] WHERE [quot'n' space] > 2"), ); Assert::same( reformat('SELECT * FROM [where] WHERE [where].[quot\'n\' space] > 2'), - $conn->translate("SELECT * FROM [where] WHERE [where.quot'n' space] > 2") + $conn->translate("SELECT * FROM [where] WHERE [where.quot'n' space] > 2"), ); diff --git a/tests/dibi/Translator.phpt b/tests/dibi/Translator.phpt index 389b7f836..0393f3148 100644 --- a/tests/dibi/Translator.phpt +++ b/tests/dibi/Translator.phpt @@ -23,7 +23,7 @@ Assert::same( $conn->translate('REPLACE INTO [products]', [ 'title' => 'Drticka', 'price' => 318, - ]) + ]), ); @@ -38,7 +38,7 @@ Assert::same( 'sqlsrv' => "INSERT INTO [products] ([title], [price], [brand]) VALUES (N'Super Product', 12, NULL) , (N'Super Product', 12, NULL) , (N'Super Product', 12, NULL)", "INSERT INTO [products] ([title], [price], [brand]) VALUES ('Super Product', 12, NULL) , ('Super Product', 12, NULL) , ('Super Product', 12, NULL)", ]), - $conn->translate('INSERT INTO [products]', $array, $array, $array) + $conn->translate('INSERT INTO [products]', $array, $array, $array), ); @@ -53,7 +53,7 @@ Assert::same( 'sqlsrv' => "INSERT INTO [products] ([pole], [bit]) VALUES (N'hodnota1', 1) , (N'hodnota2', 1) , (N'hodnota3', 1)", "INSERT INTO [products] ([pole], [bit]) VALUES ('hodnota1', 1) , ('hodnota2', 1) , ('hodnota3', 1)", ]), - $conn->translate('INSERT INTO [products] %ex', $array) + $conn->translate('INSERT INTO [products] %ex', $array), ); @@ -66,7 +66,7 @@ Assert::same( $conn->translate('UPDATE [colors] SET', [ 'color' => 'blue', 'order' => 12, - ], 'WHERE [id]=%i', 123) + ], 'WHERE [id]=%i', 123), ); @@ -74,26 +74,28 @@ Assert::same( $array = [1, 2, 3]; Assert::same( reformat('SELECT * FROM [people] WHERE [id] IN ( 1, 2, 3 )'), - $conn->translate('SELECT * FROM [people] WHERE [id] IN (', $array, ')') + $conn->translate('SELECT * FROM [people] WHERE [id] IN (', $array, ')'), ); // long numbers Assert::same( reformat('SELECT -123456789123456789123456789'), - $conn->translate('SELECT %i', '-123456789123456789123456789') + $conn->translate('SELECT %i', '-123456789123456789123456789'), ); // long float numbers Assert::same( reformat('SELECT -.12345678912345678912345678e10'), - $conn->translate('SELECT %f', '-.12345678912345678912345678e10') + $conn->translate('SELECT %f', '-.12345678912345678912345678e10'), ); // invalid input -$e = Assert::exception(function () use ($conn) { - $conn->translate('SELECT %s', (object) [123], ', %m', 123); -}, Dibi\Exception::class, 'SQL translate error: Invalid combination of type stdClass and modifier %s'); +$e = Assert::exception( + fn() => $conn->translate('SELECT %s', (object) [123], ', %m', 123), + Dibi\Exception::class, + 'SQL translate error: Invalid combination of type stdClass and modifier %s', +); Assert::same('SELECT **Invalid combination of type stdClass and modifier %s** , **Unknown or unexpected modifier %m**', $e->getSql()); Assert::same( @@ -101,7 +103,7 @@ Assert::same( 'sqlsrv' => "SELECT * FROM [table] WHERE id=10 AND name=N'ahoj'", "SELECT * FROM [table] WHERE id=10 AND name='ahoj'", ]), - $conn->translate('SELECT * FROM [table] WHERE id=%i AND name=%s', 10, 'ahoj') + $conn->translate('SELECT * FROM [table] WHERE id=%i AND name=%s', 10, 'ahoj'), ); Assert::same( @@ -109,7 +111,7 @@ Assert::same( 'sqlsrv' => "TEST ([cond] > 2) OR ([cond2] = N'3') OR (cond3 < RAND())", "TEST ([cond] > 2) OR ([cond2] = '3') OR (cond3 < RAND())", ]), - $conn->translate('TEST %or', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()']) + $conn->translate('TEST %or', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()']), ); Assert::same( @@ -117,7 +119,7 @@ Assert::same( 'sqlsrv' => "TEST ([cond] > 2) AND ([cond2] = N'3') AND (cond3 < RAND())", "TEST ([cond] > 2) AND ([cond2] = '3') AND (cond3 < RAND())", ]), - $conn->translate('TEST %and', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()']) + $conn->translate('TEST %and', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()']), ); @@ -126,7 +128,7 @@ $where[] = '[age] > 20'; $where[] = '[email] IS NOT NULL'; Assert::same( reformat('SELECT * FROM [table] WHERE ([age] > 20) AND ([email] IS NOT NULL)'), - $conn->translate('SELECT * FROM [table] WHERE %and', $where) + $conn->translate('SELECT * FROM [table] WHERE %and', $where), ); @@ -139,14 +141,14 @@ Assert::same( 'sqlsrv' => "SELECT * FROM [table] WHERE ([age] IS NULL) AND ([email] = N'ahoj') AND ([id] IN (10, 20, 30))", "SELECT * FROM [table] WHERE ([age] IS NULL) AND ([email] = 'ahoj') AND ([id] IN (10, 20, 30))", ]), - $conn->translate('SELECT * FROM [table] WHERE %and', $where) + $conn->translate('SELECT * FROM [table] WHERE %and', $where), ); $where = []; Assert::same( reformat('SELECT * FROM [table] WHERE 1=1'), - $conn->translate('SELECT * FROM [table] WHERE %and', $where) + $conn->translate('SELECT * FROM [table] WHERE %and', $where), ); @@ -161,7 +163,7 @@ $order = [ ]; Assert::same( reformat('SELECT * FROM [people] ORDER BY [field1] ASC, [field2] DESC, [field3] ASC, [field4] DESC, [field5] ASC, [field6] DESC'), - $conn->translate('SELECT * FROM [people] ORDER BY %by', $order) + $conn->translate('SELECT * FROM [people] ORDER BY %by', $order), ); @@ -172,13 +174,14 @@ Assert::same( 'sqlsrv' => 'SELECT * FROM [products] OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY', 'SELECT * FROM [products] LIMIT 2', ]), - $conn->translate('SELECT * FROM [products] %lmt', 2) + $conn->translate('SELECT * FROM [products] %lmt', 2), ); if ($config['system'] === 'odbc') { - Assert::exception(function () use ($conn) { - $conn->translate('SELECT * FROM [products] %lmt %ofs', 2, 1); - }, Dibi\Exception::class); + Assert::exception( + fn() => $conn->translate('SELECT * FROM [products] %lmt %ofs', 2, 1), + Dibi\Exception::class, + ); } else { // with limit = 2, offset = 1 Assert::same( @@ -186,7 +189,7 @@ if ($config['system'] === 'odbc') { 'sqlsrv' => 'SELECT * FROM [products] OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY', 'SELECT * FROM [products] LIMIT 2 OFFSET 1', ]), - $conn->translate('SELECT * FROM [products] %lmt %ofs', 2, 1) + $conn->translate('SELECT * FROM [products] %lmt %ofs', 2, 1), ); // with offset = 50 @@ -197,7 +200,7 @@ if ($config['system'] === 'odbc') { 'sqlsrv' => 'SELECT * FROM [products] OFFSET 50 ROWS', 'SELECT * FROM [products] LIMIT -1 OFFSET 50', ]), - $conn->translate('SELECT * FROM [products] %ofs', 50) + $conn->translate('SELECT * FROM [products] %ofs', 50), ); } @@ -223,12 +226,14 @@ Assert::same( 'b8%d' => null, 'b9%t' => null, 'c1%t' => new DateTime('1212-09-26 16:51:34.0124'), - ]) + ]), ); -Assert::exception(function () use ($conn) { - $conn->translate('SELECT %s', new DateTime('1212-09-26')); -}, Dibi\Exception::class, 'SQL translate error: Invalid combination of type Dibi\DateTime and modifier %s'); +Assert::exception( + fn() => $conn->translate('SELECT %s', new DateTime('1212-09-26')), + Dibi\Exception::class, + 'SQL translate error: Invalid combination of type Dibi\DateTime and modifier %s', +); @@ -247,13 +252,13 @@ if ($config['system'] === 'postgre') { $conn->query('SET standard_conforming_strings = off'); Assert::same( "SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a\n\\\\%\\\\_\\\\\\\\''\"%'", - $conn->translate($args[0], $args[1], $args[2], $args[3]) + $conn->translate($args[0], $args[1], $args[2], $args[3]), ); $conn->query('SET standard_conforming_strings = on'); Assert::same( "SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a\n\\%\\_\\\\''\"%'", - $conn->translate($args[0], $args[1], $args[2], $args[3]) + $conn->translate($args[0], $args[1], $args[2], $args[3]), ); } elseif ($config['driver'] !== 'sqlite') { // sqlite2 Assert::same( @@ -263,49 +268,56 @@ if ($config['system'] === 'postgre') { 'sqlsrv' => "SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a\n[%][_]\\''\"%'", "SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a\\n\\%\\_\\\\\\\\\\'\"%'", ]), - $conn->translate($args[0], $args[1], $args[2], $args[3]) + $conn->translate($args[0], $args[1], $args[2], $args[3]), ); } -$e = Assert::exception(function () use ($conn) { - $conn->translate("SELECT '"); -}, Dibi\Exception::class, 'SQL translate error: Alone quote'); +$e = Assert::exception( + fn() => $conn->translate("SELECT '"), + Dibi\Exception::class, + 'SQL translate error: Alone quote', +); Assert::same('SELECT **Alone quote**', $e->getSql()); Assert::match( - reformat([ - 'mysql' => "SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT -CONCAT(last_name, ', ', first_name) AS full_name -GROUP BY `user` -HAVING MAX(salary) > %i 123 -INTO OUTFILE '/tmp/result\\'.txt' -FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\\\"' -LINES TERMINATED BY '\\\\n' -", - 'sqlsrv' => "SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT -CONCAT(last_name, N', ', first_name) AS full_name -GROUP BY [user] -HAVING MAX(salary) > %i 123 -INTO OUTFILE N'/tmp/result''.txt' -FIELDS TERMINATED BY N',' OPTIONALLY ENCLOSED BY N'\"' -LINES TERMINATED BY N'\\n'", "SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT -CONCAT(last_name, ', ', first_name) AS full_name -GROUP BY [user] -HAVING MAX(salary) > %i 123 -INTO OUTFILE '/tmp/result''.txt' -FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' -LINES TERMINATED BY '\\n' -", + pattern: reformat([ + 'mysql' => <<<'XX' + SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT + CONCAT(last_name, ', ', first_name) AS full_name + GROUP BY `user` + HAVING MAX(salary) > %i 123 + INTO OUTFILE '/tmp/result\'.txt' + FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' + LINES TERMINATED BY '\\n' + XX, + 'sqlsrv' => <<<'XX' + SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT + CONCAT(last_name, N', ', first_name) AS full_name + GROUP BY [user] + HAVING MAX(salary) > %i 123 + INTO OUTFILE N'/tmp/result''.txt' + FIELDS TERMINATED BY N',' OPTIONALLY ENCLOSED BY N'"' + LINES TERMINATED BY N'\n' + XX, + <<<'XX' + SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT + CONCAT(last_name, ', ', first_name) AS full_name + GROUP BY [user] + HAVING MAX(salary) > %i 123 + INTO OUTFILE '/tmp/result''.txt' + FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' + LINES TERMINATED BY '\n' + XX, ]), - $conn->translate('%sql', 'SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT + actual: $conn->translate('%sql', 'SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT CONCAT(last_name, ", ", first_name) AS full_name GROUP BY [user] HAVING MAX(salary) > %i', 123, " INTO OUTFILE '/tmp/result''.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n' -") +"), ); @@ -330,114 +342,124 @@ $array5 = ['RAND()', '[col1] > [col2]']; Assert::match( - reformat([ - 'mysql' => "SELECT * -FROM `db`.`table` -WHERE (`test`.`a` LIKE '1995-03-01' - OR `b1` IN ( 1, 2, 3 ) - OR `b2` IN ('1', '2', '3' ) - OR `b3` IN ( ) - OR `b4` IN ( 'one', 'two', 'three' ) - OR `b5` IN (`col1` AS `one`, `col2` AS `two`, `col3` AS `thr.ee` ) - OR `b6` IN ('one', 'two', 'thr.ee') - OR `b7` IN (NULL) - OR `b8` IN (RAND() `col1` > `col2` ) - OR `b9` IN (RAND(), [col1] > [col2] ) - OR `b10` IN ( ) - AND `c` = 'embedded \\' string' - OR `d`=10 - OR `e`=NULL - OR `true`= 1 - OR `false`= 0 - OR `str_null`=NULL - OR `str_not_null`='hello' -LIMIT 10", - 'sqlsrv' => "SELECT * -FROM [db].[table] -WHERE ([test].[a] LIKE '1995-03-01' - OR [b1] IN ( 1, 2, 3 ) - OR [b2] IN (N'1', N'2', N'3' ) - OR [b3] IN ( ) - OR [b4] IN ( N'one', N'two', N'three' ) - OR [b5] IN ([col1] AS [one], [col2] AS [two], [col3] AS [thr.ee] ) - OR [b6] IN (N'one', N'two', N'thr.ee') - OR [b7] IN (NULL) - OR [b8] IN (RAND() [col1] > [col2] ) - OR [b9] IN (RAND(), [col1] > [col2] ) - OR [b10] IN ( ) - AND [c] = N'embedded '' string' - OR [d]=10 - OR [e]=NULL - OR [true]= 1 - OR [false]= 0 - OR [str_null]=NULL - OR [str_not_null]=N'hello' -LIMIT 10", - 'postgre' => 'SELECT * -FROM "db"."table" -WHERE ("test"."a" LIKE \'1995-03-01\' - OR "b1" IN ( 1, 2, 3 ) - OR "b2" IN (\'1\', \'2\', \'3\' ) - OR "b3" IN ( ) - OR "b4" IN ( \'one\', \'two\', \'three\' ) - OR "b5" IN ("col1" AS "one", "col2" AS "two", "col3" AS "thr.ee" ) - OR "b6" IN (\'one\', \'two\', \'thr.ee\') - OR "b7" IN (NULL) - OR "b8" IN (RAND() "col1" > "col2" ) - OR "b9" IN (RAND(), [col1] > [col2] ) - OR "b10" IN ( ) - AND "c" = \'embedded \'\' string\' - OR "d"=10 - OR "e"=NULL - OR "true"= TRUE - OR "false"= FALSE - OR "str_null"=NULL - OR "str_not_null"=\'hello\' -LIMIT 10', - 'odbc' => "SELECT * -FROM [db].[table] -WHERE ([test].[a] LIKE #03/01/1995# - OR [b1] IN ( 1, 2, 3 ) - OR [b2] IN ('1', '2', '3' ) - OR [b3] IN ( ) - OR [b4] IN ( 'one', 'two', 'three' ) - OR [b5] IN ([col1] AS [one], [col2] AS [two], [col3] AS [thr.ee] ) - OR [b6] IN ('one', 'two', 'thr.ee') - OR [b7] IN (NULL) - OR [b8] IN (RAND() [col1] > [col2] ) - OR [b9] IN (RAND(), [col1] > [col2] ) - OR [b10] IN ( ) - AND [c] = 'embedded '' string' - OR [d]=10 - OR [e]=NULL - OR [true]= 1 - OR [false]= 0 - OR [str_null]=NULL - OR [str_not_null]='hello' -LIMIT 10", - "SELECT * -FROM [db].[table] -WHERE ([test].[a] LIKE '1995-03-01' - OR [b1] IN ( 1, 2, 3 ) - OR [b2] IN ('1', '2', '3' ) - OR [b3] IN ( ) - OR [b4] IN ( 'one', 'two', 'three' ) - OR [b5] IN ([col1] AS [one], [col2] AS [two], [col3] AS [thr.ee] ) - OR [b6] IN ('one', 'two', 'thr.ee') - OR [b7] IN (NULL) - OR [b8] IN (RAND() [col1] > [col2] ) - OR [b9] IN (RAND(), [col1] > [col2] ) - OR [b10] IN ( ) - AND [c] = 'embedded '' string' - OR [d]=10 - OR [e]=NULL - OR [true]= 1 - OR [false]= 0 - OR [str_null]=NULL - OR [str_not_null]='hello' -LIMIT 10", + pattern: reformat([ + 'mysql' => <<<'XX' + SELECT * + FROM `db`.`table` + WHERE (`test`.`a` LIKE '1995-03-01' + OR `b1` IN ( 1, 2, 3 ) + OR `b2` IN ('1', '2', '3' ) + OR `b3` IN ( ) + OR `b4` IN ( 'one', 'two', 'three' ) + OR `b5` IN (`col1` AS `one`, `col2` AS `two`, `col3` AS `thr.ee` ) + OR `b6` IN ('one', 'two', 'thr.ee') + OR `b7` IN (NULL) + OR `b8` IN (RAND() `col1` > `col2` ) + OR `b9` IN (RAND(), [col1] > [col2] ) + OR `b10` IN ( ) + AND `c` = 'embedded \' string' + OR `d`=10 + OR `e`=NULL + OR `true`= 1 + OR `false`= 0 + OR `str_null`=NULL + OR `str_not_null`='hello' + LIMIT 10 + XX, + 'sqlsrv' => <<<'XX' + SELECT * + FROM [db].[table] + WHERE ([test].[a] LIKE '1995-03-01' + OR [b1] IN ( 1, 2, 3 ) + OR [b2] IN (N'1', N'2', N'3' ) + OR [b3] IN ( ) + OR [b4] IN ( N'one', N'two', N'three' ) + OR [b5] IN ([col1] AS [one], [col2] AS [two], [col3] AS [thr.ee] ) + OR [b6] IN (N'one', N'two', N'thr.ee') + OR [b7] IN (NULL) + OR [b8] IN (RAND() [col1] > [col2] ) + OR [b9] IN (RAND(), [col1] > [col2] ) + OR [b10] IN ( ) + AND [c] = N'embedded '' string' + OR [d]=10 + OR [e]=NULL + OR [true]= 1 + OR [false]= 0 + OR [str_null]=NULL + OR [str_not_null]=N'hello' + LIMIT 10 + XX, + 'postgre' => <<<'XX' + SELECT * + FROM "db"."table" + WHERE ("test"."a" LIKE '1995-03-01' + OR "b1" IN ( 1, 2, 3 ) + OR "b2" IN ('1', '2', '3' ) + OR "b3" IN ( ) + OR "b4" IN ( 'one', 'two', 'three' ) + OR "b5" IN ("col1" AS "one", "col2" AS "two", "col3" AS "thr.ee" ) + OR "b6" IN ('one', 'two', 'thr.ee') + OR "b7" IN (NULL) + OR "b8" IN (RAND() "col1" > "col2" ) + OR "b9" IN (RAND(), [col1] > [col2] ) + OR "b10" IN ( ) + AND "c" = 'embedded '' string' + OR "d"=10 + OR "e"=NULL + OR "true"= TRUE + OR "false"= FALSE + OR "str_null"=NULL + OR "str_not_null"='hello' + LIMIT 10 + XX, + 'odbc' => <<<'XX' + SELECT * + FROM [db].[table] + WHERE ([test].[a] LIKE #03/01/1995# + OR [b1] IN ( 1, 2, 3 ) + OR [b2] IN ('1', '2', '3' ) + OR [b3] IN ( ) + OR [b4] IN ( 'one', 'two', 'three' ) + OR [b5] IN ([col1] AS [one], [col2] AS [two], [col3] AS [thr.ee] ) + OR [b6] IN ('one', 'two', 'thr.ee') + OR [b7] IN (NULL) + OR [b8] IN (RAND() [col1] > [col2] ) + OR [b9] IN (RAND(), [col1] > [col2] ) + OR [b10] IN ( ) + AND [c] = 'embedded '' string' + OR [d]=10 + OR [e]=NULL + OR [true]= 1 + OR [false]= 0 + OR [str_null]=NULL + OR [str_not_null]='hello' + LIMIT 10 + XX, + <<<'XX' + SELECT * + FROM [db].[table] + WHERE ([test].[a] LIKE '1995-03-01' + OR [b1] IN ( 1, 2, 3 ) + OR [b2] IN ('1', '2', '3' ) + OR [b3] IN ( ) + OR [b4] IN ( 'one', 'two', 'three' ) + OR [b5] IN ([col1] AS [one], [col2] AS [two], [col3] AS [thr.ee] ) + OR [b6] IN ('one', 'two', 'thr.ee') + OR [b7] IN (NULL) + OR [b8] IN (RAND() [col1] > [col2] ) + OR [b9] IN (RAND(), [col1] > [col2] ) + OR [b10] IN ( ) + AND [c] = 'embedded '' string' + OR [d]=10 + OR [e]=NULL + OR [true]= 1 + OR [false]= 0 + OR [str_null]=NULL + OR [str_not_null]='hello' + LIMIT 10 + XX, ]), - $conn->translate('SELECT * + actual: $conn->translate('SELECT * FROM [db.table] WHERE ([test.a] LIKE %d', '1995-03-01', ' OR [b1] IN (', $array1, ') @@ -457,7 +479,7 @@ WHERE ([test.a] LIKE %d', '1995-03-01', ' OR [false]=', false, ' OR [str_null]=%sn', '', ' OR [str_not_null]=%sn', 'hello', ' -LIMIT 10') +LIMIT 10'), ); @@ -466,19 +488,19 @@ Assert::same( 'sqlsrv' => "TEST [cond] > 2 [cond2] = N'3' cond3 < RAND() 123", "TEST [cond] > 2 [cond2] = '3' cond3 < RAND() 123", ]), - $conn->translate('TEST %ex', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'], 123) + $conn->translate('TEST %ex', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'], 123), ); Assert::same( reformat('TEST ([cond] > 2) OR ([cond2] > 3) OR ([cond3] = 10 + 1)'), - $conn->translate('TEST %or', ['`cond` > 2', ['[cond2] > %i', '3'], 'cond3%sql' => ['10 + 1']]) + $conn->translate('TEST %or', ['`cond` > 2', ['[cond2] > %i', '3'], 'cond3%sql' => ['10 + 1']]), ); Assert::same( reformat('TEST ([cond] = 2) OR ([cond3] = RAND())'), - $conn->translate('TEST %or', ['cond' => 2, 'cond3%sql' => 'RAND()']) + $conn->translate('TEST %or', ['cond' => 2, 'cond3%sql' => 'RAND()']), ); @@ -487,7 +509,7 @@ Assert::same( 'sqlsrv' => "TEST ([cond1] 3) OR ([cond2] RAND()) OR ([cond3] LIKE N'string')", "TEST ([cond1] 3) OR ([cond2] RAND()) OR ([cond3] LIKE 'string')", ]), - $conn->translate('TEST %or', ['cond1%ex' => 3, 'cond2%ex' => 'RAND()', 'cond3%ex' => ['LIKE %s', 'string']]) + $conn->translate('TEST %or', ['cond1%ex' => 3, 'cond2%ex' => 'RAND()', 'cond3%ex' => ['LIKE %s', 'string']]), ); @@ -497,7 +519,7 @@ Assert::same( 'sqlsrv' => 'SELECT * FROM [test] WHERE [id] LIKE N\'%d%t\' OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY', 'SELECT * FROM [test] WHERE [id] LIKE \'%d%t\' LIMIT 10', ]), - $conn->translate("SELECT * FROM [test] WHERE %n LIKE '%d%t' %lmt", 'id', 10) + $conn->translate("SELECT * FROM [test] WHERE %n LIKE '%d%t' %lmt", 'id', 10), ); @@ -506,13 +528,13 @@ $where = [ ]; Assert::same( reformat('SELECT * FROM [tablename] WHERE ([tablename].[column] = 1)'), - $conn->translate('SELECT * FROM [tablename] WHERE %and', $where) + $conn->translate('SELECT * FROM [tablename] WHERE %and', $where), ); Assert::same( reformat('SELECT FROM ...'), - $conn->translate('SELECT FROM ... %lmt', null) + $conn->translate('SELECT FROM ... %lmt', null), ); Assert::same( @@ -520,7 +542,7 @@ Assert::same( 'sqlsrv' => "SELECT N'%i'", "SELECT '%i'", ]), - $conn->translate("SELECT '%i'") + $conn->translate("SELECT '%i'"), ); Assert::same( @@ -528,7 +550,7 @@ Assert::same( 'sqlsrv' => "SELECT N'%i'", "SELECT '%i'", ]), - $conn->translate('SELECT "%i"') + $conn->translate('SELECT "%i"'), ); @@ -543,7 +565,7 @@ Assert::same( ], [ 'product_id' => 1, 'title' => new Dibi\Expression('SHA1(%s)', 'Test product'), - ]) + ]), ); Assert::same( @@ -554,7 +576,7 @@ Assert::same( $conn->translate('UPDATE [products]', [ 'product_id' => 1, 'title' => new Dibi\Expression('SHA1(%s)', 'Test product'), - ]) + ]), ); Assert::same( @@ -565,7 +587,7 @@ Assert::same( $conn->translate('UPDATE [products]', [ 'product_id' => 1, 'title' => new Dibi\Expression('SHA1(%s)', 'Test product'), - ]) + ]), ); Assert::same( @@ -576,7 +598,7 @@ Assert::same( $conn->translate('SELECT * FROM [products] WHERE', [ 'product_id' => 1, 'title' => new Dibi\Expression('SHA1(%s)', 'Test product'), - ]) + ]), ); @@ -588,7 +610,7 @@ Assert::same( 'top' => 2, ]), new Dibi\Expression('number < %i', 100), - ]) + ]), ); @@ -613,7 +635,7 @@ Assert::same( 'sqlsrv' => "INSERT INTO test ([id], [text], [num]) VALUES (1, N'ahoj', 1), (2, N'jak', -1), (3, N'se', 10), (4, SUM(5), 1)", "INSERT INTO test ([id], [text], [num]) VALUES (1, 'ahoj', 1), (2, 'jak', -1), (3, 'se', 10), (4, SUM(5), 1)", ]), - $conn->translate('INSERT INTO test %m', $array6) + $conn->translate('INSERT INTO test %m', $array6), ); @@ -624,42 +646,46 @@ $by = [ Assert::same( reformat('SELECT * FROM table ORDER BY funkce(nazev_pole) ASC, [jine_pole] DESC'), - $conn->translate('SELECT * FROM table ORDER BY %by', $by) + $conn->translate('SELECT * FROM table ORDER BY %by', $by), ); Assert::same( reformat('INSERT INTO [test].*'), - $conn->translate('INSERT INTO [test.*]') + $conn->translate('INSERT INTO [test.*]'), ); -Assert::exception(function () use ($conn) { - $conn->translate('INSERT INTO %i', 'ahoj'); -}, Dibi\Exception::class, "Expected number, 'ahoj' given."); +Assert::exception( + fn() => $conn->translate('INSERT INTO %i', 'ahoj'), + Dibi\Exception::class, + "Expected number, 'ahoj' given.", +); -Assert::exception(function () use ($conn) { - $conn->translate('INSERT INTO %f', 'ahoj'); -}, Dibi\Exception::class, "Expected number, 'ahoj' given."); +Assert::exception( + fn() => $conn->translate('INSERT INTO %f', 'ahoj'), + Dibi\Exception::class, + "Expected number, 'ahoj' given.", +); Assert::same( reformat('SELECT * FROM table'), - $conn->translate('SELECT', new Dibi\Literal('* FROM table')) + $conn->translate('SELECT', new Dibi\Literal('* FROM table')), ); Assert::same( reformat('SELECT * FROM table'), - $conn->translate('SELECT %SQL', new Dibi\Literal('* FROM table')) + $conn->translate('SELECT %SQL', new Dibi\Literal('* FROM table')), ); Assert::same( reformat('SELECT * FROM table'), - $conn->translate(new Dibi\Literal('SELECT * FROM table')) + $conn->translate(new Dibi\Literal('SELECT * FROM table')), ); Assert::same( reformat('SELECT [a].[b] AS [c.d]'), - $conn->translate('SELECT %n AS %N', 'a.b', 'c.d') + $conn->translate('SELECT %n AS %N', 'a.b', 'c.d'), ); @@ -677,5 +703,5 @@ Assert::same( 'spec2%f' => 1000.00, 'spec3%i' => 10000, 'spec4' => 10000, - ], 'WHERE [price]=%f', 123.5) + ], 'WHERE [price]=%f', 123.5), ); diff --git a/tests/dibi/exceptions.mysql.phpt b/tests/dibi/exceptions.mysql.phpt index a25f6c812..ca133a015 100644 --- a/tests/dibi/exceptions.mysql.phpt +++ b/tests/dibi/exceptions.mysql.phpt @@ -15,41 +15,54 @@ $conn = new Dibi\Connection($config); $conn->loadFile(__DIR__ . "/data/$config[system].sql"); -$e = Assert::exception(function () use ($conn) { - new Dibi\Connection([ +$e = Assert::exception( + fn() => new Dibi\Connection([ 'driver' => 'mysqli', 'host' => 'localhost', 'username' => 'unknown', 'password' => 'unknown', - ]); -}, Dibi\DriverException::class); + ]), + Dibi\DriverException::class, +); Assert::null($e->getSql()); -$e = Assert::exception(function () use ($conn) { - $conn->query('SELECT'); -}, Dibi\DriverException::class, '%a% error in your SQL syntax;%a%', 1064); +$e = Assert::exception( + fn() => $conn->query('SELECT'), + Dibi\DriverException::class, + '%a% error in your SQL syntax;%a%', + 1064, +); Assert::same('SELECT', $e->getSql()); -$e = Assert::exception(function () use ($conn) { - $conn->query('INSERT INTO products (product_id, title) VALUES (1, "New")'); -}, Dibi\UniqueConstraintViolationException::class, "%a?%Duplicate entry '1' for key '%a?%PRIMARY'", 1062); +$e = Assert::exception( + fn() => $conn->query('INSERT INTO products (product_id, title) VALUES (1, "New")'), + Dibi\UniqueConstraintViolationException::class, + "%a?%Duplicate entry '1' for key '%a?%PRIMARY'", + 1062, +); Assert::same("INSERT INTO products (product_id, title) VALUES (1, 'New')", $e->getSql()); -$e = Assert::exception(function () use ($conn) { - $conn->query('INSERT INTO products (title) VALUES (NULL)'); -}, Dibi\NotNullConstraintViolationException::class, "%a?%Column 'title' cannot be null", 1048); +$e = Assert::exception( + fn() => $conn->query('INSERT INTO products (title) VALUES (NULL)'), + Dibi\NotNullConstraintViolationException::class, + "%a?%Column 'title' cannot be null", + 1048, +); Assert::same('INSERT INTO products (title) VALUES (NULL)', $e->getSql()); -$e = Assert::exception(function () use ($conn) { - $conn->query('INSERT INTO orders (customer_id, product_id, amount) VALUES (100, 1, 1)'); -}, Dibi\ForeignKeyConstraintViolationException::class, '%a% a foreign key constraint fails %a%', 1452); +$e = Assert::exception( + fn() => $conn->query('INSERT INTO orders (customer_id, product_id, amount) VALUES (100, 1, 1)'), + Dibi\ForeignKeyConstraintViolationException::class, + '%a% a foreign key constraint fails %a%', + 1452, +); Assert::same('INSERT INTO orders (customer_id, product_id, amount) VALUES (100, 1, 1)', $e->getSql()); diff --git a/tests/dibi/exceptions.postgre.phpt b/tests/dibi/exceptions.postgre.phpt index 9ac8e0180..39ced4082 100644 --- a/tests/dibi/exceptions.postgre.phpt +++ b/tests/dibi/exceptions.postgre.phpt @@ -15,29 +15,40 @@ $conn = new Dibi\Connection($config); $conn->loadFile(__DIR__ . "/data/$config[system].sql"); -$e = Assert::exception(function () use ($conn) { - $conn->query('SELECT INTO'); -}, Dibi\DriverException::class, '%a?%syntax error %A%'); +$e = Assert::exception( + fn() => $conn->query('SELECT INTO'), + Dibi\DriverException::class, + '%a?%syntax error %A%', +); Assert::same('SELECT INTO', $e->getSql()); -$e = Assert::exception(function () use ($conn) { - $conn->query('INSERT INTO products (product_id, title) VALUES (1, "New")'); -}, Dibi\UniqueConstraintViolationException::class, '%a% violates unique constraint %A%', '23505'); +$e = Assert::exception( + fn() => $conn->query('INSERT INTO products (product_id, title) VALUES (1, "New")'), + Dibi\UniqueConstraintViolationException::class, + '%a% violates unique constraint %A%', + '23505', +); Assert::same("INSERT INTO products (product_id, title) VALUES (1, 'New')", $e->getSql()); -$e = Assert::exception(function () use ($conn) { - $conn->query('INSERT INTO products (title) VALUES (NULL)'); -}, Dibi\NotNullConstraintViolationException::class, '%a?%null value in column "title"%a%violates not-null constraint%A?%', '23502'); +$e = Assert::exception( + fn() => $conn->query('INSERT INTO products (title) VALUES (NULL)'), + Dibi\NotNullConstraintViolationException::class, + '%a?%null value in column "title"%a%violates not-null constraint%A?%', + '23502', +); Assert::same('INSERT INTO products (title) VALUES (NULL)', $e->getSql()); -$e = Assert::exception(function () use ($conn) { - $conn->query('INSERT INTO orders (customer_id, product_id, amount) VALUES (100, 1, 1)'); -}, Dibi\ForeignKeyConstraintViolationException::class, '%a% violates foreign key constraint %A%', '23503'); +$e = Assert::exception( + fn() => $conn->query('INSERT INTO orders (customer_id, product_id, amount) VALUES (100, 1, 1)'), + Dibi\ForeignKeyConstraintViolationException::class, + '%a% violates foreign key constraint %A%', + '23503', +); Assert::same('INSERT INTO orders (customer_id, product_id, amount) VALUES (100, 1, 1)', $e->getSql()); diff --git a/tests/dibi/exceptions.sqlite.phpt b/tests/dibi/exceptions.sqlite.phpt index 232e6cf1d..b7071ecb3 100644 --- a/tests/dibi/exceptions.sqlite.phpt +++ b/tests/dibi/exceptions.sqlite.phpt @@ -15,23 +15,32 @@ $conn = new Dibi\Connection($config); $conn->loadFile(__DIR__ . "/data/$config[system].sql"); -$e = Assert::exception(function () use ($conn) { - $conn->query('SELECT'); -}, Dibi\DriverException::class, '%a%', 1); +$e = Assert::exception( + fn() => $conn->query('SELECT'), + Dibi\DriverException::class, + '%a%', + 1, +); Assert::same('SELECT', $e->getSql()); -$e = Assert::exception(function () use ($conn) { - $conn->query('INSERT INTO products (product_id, title) VALUES (1, "New")'); -}, Dibi\UniqueConstraintViolationException::class, null, 19); +$e = Assert::exception( + fn() => $conn->query('INSERT INTO products (product_id, title) VALUES (1, "New")'), + Dibi\UniqueConstraintViolationException::class, + null, + 19, +); Assert::same("INSERT INTO products (product_id, title) VALUES (1, 'New')", $e->getSql()); -$e = Assert::exception(function () use ($conn) { - $conn->query('INSERT INTO products (title) VALUES (NULL)'); -}, Dibi\NotNullConstraintViolationException::class, null, 19); +$e = Assert::exception( + fn() => $conn->query('INSERT INTO products (title) VALUES (NULL)'), + Dibi\NotNullConstraintViolationException::class, + null, + 19, +); Assert::same('INSERT INTO products (title) VALUES (NULL)', $e->getSql());