class MysqlDateSqlTest

Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Unit/Plugin/views/query/MysqlDateSqlTest.php \Drupal\Tests\views\Unit\Plugin\views\query\MysqlDateSqlTest
  2. 10 core/modules/views/tests/src/Unit/Plugin/views/query/MysqlDateSqlTest.php \Drupal\Tests\views\Unit\Plugin\views\query\MysqlDateSqlTest
  3. 11.x core/modules/views/tests/src/Unit/Plugin/views/query/MysqlDateSqlTest.php \Drupal\Tests\views\Unit\Plugin\views\query\MysqlDateSqlTest

Tests the MySQL-specific date query handler.

@coversDefaultClass \Drupal\views\Plugin\views\query\MysqlDateSql

@group views

Hierarchy

Expanded class hierarchy of MysqlDateSqlTest

File

core/modules/views/tests/src/Unit/Plugin/views/query/MysqlDateSqlTest.php, line 16

Namespace

Drupal\Tests\views\Unit\Plugin\views\query
View source
class MysqlDateSqlTest extends UnitTestCase {
    
    /**
     * The mocked database service.
     *
     * @var \Drupal\Core\Database\Connection
     */
    protected $database;
    
    /**
     * {@inheritdoc}
     */
    public function setUp() : void {
        parent::setUp();
        $this->database = $this->prophesize(Connection::class)
            ->reveal();
    }
    
    /**
     * Tests the getDateField method.
     *
     * @covers ::getDateField
     */
    public function testGetDateField() {
        $date_sql = new MysqlDateSql($this->database);
        $expected = 'foo.field';
        $this->assertEquals($expected, $date_sql->getDateField('foo.field', TRUE));
        $expected = "DATE_ADD('19700101', INTERVAL foo.field SECOND)";
        $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 MysqlDateSql($this->database);
        $this->assertEquals("DATE_FORMAT({$field}, '{$expected_format}')", $date_sql->getDateFormat($field, $format));
    }
    
    /**
     * Provider for date formatting test.
     */
    public function providerTestGetDateFormat() {
        return [
            [
                'foo.field',
                'Y-y-M-m',
                '%Y-%y-%b-%m',
            ],
            [
                'bar.field',
                'n-F D d l',
                '%c-%M %a %d %W',
            ],
            [
                'baz.bar_field',
                'j/W/H-h i s A',
                '%e/%v/%H-%h %i %s %p',
            ],
        ];
    }
    
    /**
     * Tests timezone offset formatting.
     *
     * @covers ::setFieldTimezoneOffset
     */
    public function testSetFieldTimezoneOffset() {
        $date_sql = new MysqlDateSql($this->database);
        $field = 'foobar.field';
        $date_sql->setFieldTimezoneOffset($field, 42);
        $this->assertEquals("(foobar.field + INTERVAL 42 SECOND)", $field);
    }
    
    /**
     * Tests setting the database offset.
     *
     * @covers ::setTimezoneOffset
     */
    public function testSetTimezoneOffset() {
        $database = $this->prophesize(Connection::class);
        $database->query("SET @@session.time_zone = '42'")
            ->shouldBeCalledTimes(1);
        $date_sql = new MysqlDateSql($database->reveal());
        $date_sql->setTimezoneOffset(42);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
MysqlDateSqlTest::$database protected property The mocked database service.
MysqlDateSqlTest::providerTestGetDateFormat public function Provider for date formatting test.
MysqlDateSqlTest::setUp public function Overrides UnitTestCase::setUp
MysqlDateSqlTest::testGetDateField public function Tests the getDateField method.
MysqlDateSqlTest::testGetDateFormat public function Tests date formatting replacement.
MysqlDateSqlTest::testSetFieldTimezoneOffset public function Tests timezone offset formatting.
MysqlDateSqlTest::testSetTimezoneOffset public function Tests setting the database offset.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.