From a8691eb8f5a343bf1c6dd5c2b949f5721ac922dc Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 26 Nov 2015 12:18:45 +0100 Subject: [PATCH] Connection::substitute() fixed [Closes #197] --- src/Dibi/Connection.php | 2 +- tests/dibi/Connection.substitutions.phpt | 42 ++++++++++++++++++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 70287300..0a074582 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -545,7 +545,7 @@ class Connection { return strpos($value, ':') === FALSE ? $value - : preg_replace_callback('#:([^:\s]*):#', function ($m) { $this->substitutes->{$m[1]}; }, $value); + : preg_replace_callback('#:([^:\s]*):#', function ($m) { return $this->substitutes->{$m[1]}; }, $value); } diff --git a/tests/dibi/Connection.substitutions.phpt b/tests/dibi/Connection.substitutions.phpt index 31657df5..2e652da2 100644 --- a/tests/dibi/Connection.substitutions.phpt +++ b/tests/dibi/Connection.substitutions.phpt @@ -11,16 +11,46 @@ $conn = new Dibi\Connection($config); $conn->getSubstitutes()->blog = 'wp_'; Assert::same( - reformat('UPDATE wp_items SET [text]=\'Hello World\''), - $conn->translate("UPDATE :blog:items SET [text]='Hello World'") + reformat('UPDATE wp_items SET [val]=1'), + $conn->translate('UPDATE :blog:items SET [val]=1') ); Assert::same( - reformat('UPDATE \'wp_\' SET [text]=\'Hello World\''), - $conn->translate("UPDATE :blog: SET [text]='Hello World'") + reformat('UPDATE [wp_items] SET [val]=1'), + $conn->translate('UPDATE [:blog:items] SET [val]=1') ); Assert::same( - reformat('UPDATE \':blg:\' SET [text]=\'Hello World\''), - $conn->translate("UPDATE :blg: SET [text]='Hello World'") + reformat("UPDATE 'wp_' 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') +); + +Assert::same( + reformat("UPDATE table SET [text]=':blog:a'"), + $conn->translate("UPDATE table SET [text]=':blog:a'") +); + + +// create new substitution :: (empty) ==> my_ +$conn->getSubstitutes()->{''} = 'my_'; + +Assert::same( + reformat('UPDATE my_table SET [val]=1'), + $conn->translate('UPDATE ::table SET [val]=1') +); + + +// create substitutions using fallback callback +$conn->getSubstitutes()->setCallback(function ($expr) { + return '_' . $expr . '_'; +}); + +Assert::same( + reformat('UPDATE _account_user SET [val]=1'), + $conn->translate('UPDATE :account:user SET [val]=1') );