class PostgresqlDateSqlTest in Drupal 8
Same name and namespace in other branches
- 9 core/modules/views/tests/src/Unit/Plugin/views/query/PostgresqlDateSqlTest.php \Drupal\Tests\views\Unit\Plugin\views\query\PostgresqlDateSqlTest
- 10 core/modules/views/tests/src/Unit/Plugin/views/query/PostgresqlDateSqlTest.php \Drupal\Tests\views\Unit\Plugin\views\query\PostgresqlDateSqlTest
Tests the PostgreSQL-specific date query handler.
@coversDefaultClass \Drupal\views\Plugin\views\query\PostgresqlDateSql
@group views
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\views\Unit\Plugin\views\query\PostgresqlDateSqlTest
Expanded class hierarchy of PostgresqlDateSqlTest
File
- core/
modules/ views/ tests/ src/ Unit/ Plugin/ views/ query/ PostgresqlDateSqlTest.php, line 16
Namespace
Drupal\Tests\views\Unit\Plugin\views\queryView source
class PostgresqlDateSqlTest extends UnitTestCase {
/**
* The mocked database service.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->database = $this
->prophesize(Connection::class)
->reveal();
}
/**
* Tests the getDateField method.
*
* @covers ::getDateField
*/
public function testGetDateField() {
$date_sql = new PostgresqlDateSql($this->database);
$expected = "TO_TIMESTAMP(foo.field, 'YYYY-MM-DD\"T\"HH24:MI:SS')";
$this
->assertEquals($expected, $date_sql
->getDateField('foo.field', TRUE));
$expected = 'TO_TIMESTAMP(foo.field)';
$this
->assertEquals($expected, $date_sql
->getDateField('foo.field', FALSE));
}
/**
* Tests date formatting replacement.
*
* @covers ::getDateFormat
*
* @dataProvider providerTestGetDateFormat
*/
public function testGetDateFormat($field, $format, $expected_format) {
$date_sql = new PostgresqlDateSql($this->database);
$this
->assertEquals("TO_CHAR({$field}, '{$expected_format}')", $date_sql
->getDateFormat($field, $format));
}
/**
* Provider for date formatting test.
*/
public function providerTestGetDateFormat() {
return [
[
'foo.field',
'Y-y-M-m',
'YYYY-YY-Mon-MM',
],
[
'bar.field',
'n-F D d l',
'MM-Month Dy DD Day',
],
[
'baz.bar_field',
'j/W/H-h i s A',
'DD/IW/HH24-HH12 MI SS AM',
],
];
}
/**
* Tests timezone offset formatting.
*
* @covers ::setFieldTimezoneOffset
*/
public function testSetFieldTimezoneOffset() {
$date_sql = new PostgresqlDateSql($this->database);
$field = 'foobar.field';
$date_sql
->setFieldTimezoneOffset($field, 42);
$this
->assertEquals("(foobar.field + INTERVAL '42 SECONDS')", $field);
}
/**
* Tests setting the database offset.
*
* @covers ::setTimezoneOffset
*/
public function testSetTimezoneOffset() {
$database = $this
->prophesize(Connection::class);
$database
->query("SET TIME ZONE INTERVAL '42' HOUR TO MINUTE")
->shouldBeCalledTimes(1);
$date_sql = new PostgresqlDateSql($database
->reveal());
$date_sql
->setTimezoneOffset(42);
}
}
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. | |
PostgresqlDateSqlTest:: |
protected | property | The mocked database service. | |
PostgresqlDateSqlTest:: |
public | function | Provider for date formatting test. | |
PostgresqlDateSqlTest:: |
public | function |
Overrides UnitTestCase:: |
|
PostgresqlDateSqlTest:: |
public | function | Tests the getDateField method. | |
PostgresqlDateSqlTest:: |
public | function | Tests date formatting replacement. | |
PostgresqlDateSqlTest:: |
public | function | Tests timezone offset formatting. | |
PostgresqlDateSqlTest:: |
public | function | Tests setting the database offset. | |
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. |