class SqlsrvConditionTest in Drupal driver for SQL Server and SQL Azure 8.2
Same name and namespace in other branches
- 4.2.x tests/src/Unit/SqlsrvConditionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest
- 3.0.x tests/src/Unit/SqlsrvConditionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest
- 3.1.x tests/src/Unit/SqlsrvConditionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest
- 4.0.x tests/src/Unit/SqlsrvConditionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest
- 4.1.x tests/src/Unit/SqlsrvConditionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest
Test the behavior of the custom Condition class.
These tests are not expected to pass on other database drivers.
@group Database
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest
Expanded class hierarchy of SqlsrvConditionTest
File
- tests/
src/ Unit/ SqlsrvConditionTest.php, line 18
Namespace
Drupal\Tests\sqlsrv\UnitView source
class SqlsrvConditionTest extends UnitTestCase {
/**
* Test the escaping strategy of the LIKE operator.
*
* Mysql, Postgres, and sqlite all use '\' to escape '%' and '_'
* in the LIKE statement. SQL Server can also use a backslash with the syntax.
* field LIKE :text ESCAPE '\'.
* However, due to a bug in PDO (https://bugs.php.net/bug.php?id=79276), if a
* SQL statement has multiple LIKE statements, parameters are not correctly
* replaced if they are located between a pair of backslashes:
*
* "field1 LIKE :text1 ESCAPE '\' AND field2 LIKE :text2 ESCAPE '\'"
* :text2 will not be replaced.
*
* If the PDO bug is fixed, this test and the LIKE customization within the
* Condition class can be removed
*
* @dataProvider dataProviderForTestLike
*/
public function testLike($given, $expected) {
$connection = $this
->prophesize(Connection::class);
$connection
->escapeField(Argument::any())
->will(function ($args) {
return preg_replace('/[^A-Za-z0-9_.]+/', '', $args[0]);
});
$connection
->mapConditionOperator(Argument::any())
->willReturn([]);
$connection = $connection
->reveal();
$query_placeholder = $this
->prophesize(PlaceholderInterface::class);
$counter = 0;
$query_placeholder
->nextPlaceholder()
->will(function () use (&$counter) {
return $counter++;
});
$query_placeholder
->uniqueIdentifier()
->willReturn(4);
$query_placeholder = $query_placeholder
->reveal();
$condition = new Condition('AND');
$condition
->condition('name', $given, 'LIKE');
$condition
->compile($connection, $query_placeholder);
$this
->assertEquals('name LIKE :db_condition_placeholder_0', $condition
->__toString());
$this
->assertEquals([
':db_condition_placeholder_0' => $expected,
], $condition
->arguments());
}
/**
* Data Provider.
*/
public function dataProviderForTestLike() {
return [
[
'%',
'%',
],
[
'\\%',
'[%]',
],
[
'\\_',
'[_]',
],
[
'\\\\',
'\\',
],
[
'[\\%]',
'[[][%]]',
],
];
}
/**
* Test the REGEXP operator string replacement.
*
* @dataProvider dataProviderForTestRegexp
*/
public function testRegexp($expected, $field_name, $operator, $pattern) {
$connection = $this
->prophesize(Connection::class);
$connection
->escapeField($field_name)
->will(function ($args) {
return preg_replace('/[^A-Za-z0-9_.]+/', '', $args[0]);
});
$connection
->mapConditionOperator($operator)
->willReturn([
'operator' => $operator,
]);
$connection = $connection
->reveal();
$query_placeholder = $this
->prophesize(PlaceholderInterface::class);
$counter = 0;
$query_placeholder
->nextPlaceholder()
->will(function () use (&$counter) {
return $counter++;
});
$query_placeholder
->uniqueIdentifier()
->willReturn(4);
$query_placeholder = $query_placeholder
->reveal();
$condition = new Condition('AND');
$condition
->condition($field_name, $pattern, $operator);
$condition
->compile($connection, $query_placeholder);
$this
->assertEquals($expected, $condition
->__toString());
$this
->assertEquals([
':db_condition_placeholder_0' => $pattern,
], $condition
->arguments());
}
/**
* Provides a list of known operations and the expected output.
*/
public function dataProviderForTestRegexp() {
return [
[
'(REGEXP(:db_condition_placeholder_0, name) = 1)',
'name',
'REGEXP',
'^P',
],
[
'(REGEXP(:db_condition_placeholder_0, name123) = 1)',
'name-123',
'REGEXP',
's$',
],
[
'(REGEXP(:db_condition_placeholder_0, name) = 0)',
'name',
'NOT REGEXP',
'^\\$[a-z][a-zA-Z_]$',
],
[
'(REGEXP(:db_condition_placeholder_0, name123) = 0)',
'name-123',
'NOT REGEXP',
'^[a-z].*$',
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SqlsrvConditionTest:: |
public | function | Data Provider. | |
SqlsrvConditionTest:: |
public | function | Provides a list of known operations and the expected output. | |
SqlsrvConditionTest:: |
public | function | Test the escaping strategy of the LIKE operator. | |
SqlsrvConditionTest:: |
public | function | Test the REGEXP operator string replacement. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |