-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
tests/Doctrine/Tests/DBAL/Functional/Schema/DefaultValueTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\DBAL\Functional\Schema; | ||
|
||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\Tests\DbalFunctionalTestCase; | ||
use function sprintf; | ||
|
||
class DefaultValueTest extends DbalFunctionalTestCase | ||
{ | ||
/** @var bool */ | ||
private static $initialized = false; | ||
|
||
protected function setUp() : void | ||
{ | ||
parent::setUp(); | ||
|
||
if (self::$initialized) { | ||
return; | ||
} | ||
|
||
self::$initialized = true; | ||
|
||
$table = new Table('default_value'); | ||
$table->addColumn('id', 'integer'); | ||
|
||
foreach (self::columnProvider() as [$name, $default]) { | ||
$table->addColumn($name, 'string', [ | ||
'default' => $default, | ||
'notnull' => false, | ||
]); | ||
} | ||
|
||
$this->connection->getSchemaManager() | ||
->dropAndCreateTable($table); | ||
|
||
$this->connection->insert('default_value', ['id' => 1]); | ||
} | ||
|
||
/** | ||
* @dataProvider columnProvider | ||
*/ | ||
public function testEscapedDefaultValueCanBeIntrospected(string $name, $expectedDefault) : void | ||
{ | ||
self::assertSame( | ||
$expectedDefault, | ||
$this->connection | ||
->getSchemaManager() | ||
->listTableDetails('default_value') | ||
->getColumn($name) | ||
->getDefault() | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider columnProvider | ||
*/ | ||
public function testEscapedDefaultValueCanBeInserted(string $name, $expectedDefault) : void | ||
{ | ||
$value = $this->connection->fetchColumn( | ||
sprintf('SELECT %s FROM default_value', $name) | ||
); | ||
|
||
self::assertSame($expectedDefault, $value); | ||
} | ||
|
||
/** | ||
* Returns potential escaped literals from all platforms combined. | ||
* | ||
* @see https://dev.mysql.com/doc/refman/5.7/en/string-literals.html | ||
* @see http://www.sqlite.org/lang_expr.html | ||
* @see https://www.postgresql.org/docs/9.6/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE | ||
* | ||
* @return mixed[][] | ||
*/ | ||
public static function columnProvider() : iterable | ||
{ | ||
return [ | ||
'Single quote' => [ | ||
'single_quote', | ||
"foo'bar", | ||
], | ||
'Single quote, doubled' => [ | ||
'single_quote_doubled', | ||
"foo''bar", | ||
], | ||
'Double quote' => [ | ||
'double_quote', | ||
'foo"bar', | ||
], | ||
'Double quote, doubled' => [ | ||
'double_quote_doubled', | ||
'foo""bar', | ||
], | ||
'Backspace' => [ | ||
'backspace', | ||
"foo\x08bar", | ||
], | ||
'New line' => [ | ||
'new_line', | ||
"foo\nbar", | ||
], | ||
'Carriage return' => [ | ||
'carriage_return', | ||
"foo\rbar", | ||
], | ||
'Tab' => [ | ||
'tab', | ||
"foo\tbar", | ||
], | ||
'Substitute' => [ | ||
'substitute', | ||
"foo\x1abar", | ||
], | ||
'Backslash' => [ | ||
'backslash', | ||
'foo\\bar', | ||
], | ||
'Backslash, doubled' => [ | ||
'backslash_doubled', | ||
'foo\\\\bar', | ||
], | ||
'Percent' => [ | ||
'percent_sign', | ||
'foo%bar', | ||
], | ||
'Underscore' => [ | ||
'underscore', | ||
'foo_bar', | ||
], | ||
'NULL string' => [ | ||
'null_string', | ||
'NULL', | ||
], | ||
'NULL value' => [ | ||
'null_value', | ||
null, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters