silverstripe-postgresql/tests/PostgreSQLConnectorTest.php

51 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\PostgreSQL\Tests;
2016-09-01 06:22:17 +02:00
use SilverStripe\Dev\SapphireTest;
2016-07-04 02:44:09 +02:00
use SilverStripe\PostgreSQL\PostgreSQLConnector;
2015-12-17 19:18:01 +01:00
class PostgreSQLConnectorTest extends SapphireTest
{
public function testSubstitutesPlaceholders()
{
$connector = new PostgreSQLConnector();
2015-12-17 19:18:01 +01:00
// basic case
$this->assertEquals(
"SELECT * FROM Table WHERE ID = $1",
$connector->replacePlaceholders("SELECT * FROM Table WHERE ID = ?")
);
2015-12-17 19:18:01 +01:00
// Multiple variables
$this->assertEquals(
"SELECT * FROM Table WHERE ID = $1 AND Name = $2",
$connector->replacePlaceholders("SELECT * FROM Table WHERE ID = ? AND Name = ?")
);
2015-12-17 19:18:01 +01:00
// Ignoring question mark placeholders within string literals
$this->assertEquals(
"SELECT * FROM Table WHERE ID = $1 AND Name = $2 AND Content = '<p>What is love?</p>'",
2015-12-17 19:18:01 +01:00
$connector->replacePlaceholders(
"SELECT * FROM Table WHERE ID = ? AND Name = ? AND Content = '<p>What is love?</p>'"
)
);
2015-12-17 19:18:01 +01:00
// Ignoring question mark placeholders within string literals with escaped slashes
$this->assertEquals(
"SELECT * FROM Table WHERE ID = $1 AND Title = '\\'' AND Content = '<p>What is love?</p>' AND Name = $2",
2015-12-17 19:18:01 +01:00
$connector->replacePlaceholders(
"SELECT * FROM Table WHERE ID = ? AND Title = '\\'' AND Content = '<p>What is love?</p>' AND Name = ?"
)
);
2015-12-17 19:18:01 +01:00
// same as above, but use double single quote escape syntax
$this->assertEquals(
"SELECT * FROM Table WHERE ID = $1 AND Title = '''' AND Content = '<p>What is love?</p>' AND Name = $2",
2015-12-17 19:18:01 +01:00
$connector->replacePlaceholders(
"SELECT * FROM Table WHERE ID = ? AND Title = '''' AND Content = '<p>What is love?</p>' AND Name = ?"
)
);
}
}