You are here

class DurationServiceTest in Duration Field 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/Service/DurationServiceTest.php \Drupal\Tests\duration_field\Unit\Service\DurationServiceTest
  2. 3.0.x tests/src/Unit/Service/DurationServiceTest.php \Drupal\Tests\duration_field\Unit\Service\DurationServiceTest

@coversDefaultClass \Drupal\duration_field\Service\DurationService @group duration_field

Hierarchy

Expanded class hierarchy of DurationServiceTest

File

tests/src/Unit/Service/DurationServiceTest.php, line 14

Namespace

Drupal\Tests\duration_field\Unit\Service
View source
class DurationServiceTest extends UnitTestCase {

  /**
   * @covers ::checkDurationInvalid
   * @dataProvider checkDurationInvalidDataProvider
   */
  public function testCheckDurationInvalid($pattern, $expectedResponse, $message) {
    $duration_service = new DurationService();
    $duration_service
      ->setStringTranslation($this
      ->getStringTranslationStub());
    if ($expectedResponse) {
      $this
        ->expectException('Drupal\\duration_field\\Exception\\InvalidDurationException');
      $duration_service
        ->checkDurationInvalid($pattern);
    }
    else {
      $response = $duration_service
        ->checkDurationInvalid($pattern);
      $this
        ->assertTrue((bool) $response == $expectedResponse, $message);
    }
  }

  /**
   * Data provider for testCheckDurationInvalid().
   */
  public function checkDurationInvalidDataProvider() {
    return [
      [
        'PY1D',
        TRUE,
        'PY1D correctly tested as invalid',
      ],
      [
        'P1Y2M3DT4H',
        FALSE,
        'P1Y2M3DT4H correctly tested as valid',
      ],
    ];
  }

  /**
   * @covers ::convertDateArrayToDurationString
   * @dataProvider convertDateArrayToDurationStringDataProvider
   */
  public function testconvertDateArrayToDurationString($input, $expectedResponse, $message) {
    $duration_service = new DurationService();
    $response = $duration_service
      ->convertDateArrayToDurationString($input);
    $this
      ->assertSame($response, $expectedResponse, $message);
  }

  /**
   * Data provider for testconvertDateArrayToDurationString().
   */
  public function convertDateArrayToDurationStringDataProvider() {
    return [
      [
        [
          'y' => 1,
          'm' => 2,
          'd' => 3,
          'h' => 4,
          'i' => 5,
          's' => 6,
        ],
        'P1Y2M3DT4H5M6S',
        'P1Y2M3DT4H5M6S was correctly validated',
      ],
      [
        [
          'y' => 1,
          'm' => 2,
          'd' => 3,
        ],
        'P1Y2M3D',
        '
        P1Y2M3D was correctly validated',
      ],
      [
        [
          'h' => 4,
          'i' => 5,
          's' => 6,
        ],
        'PT4H5M6S',
        'PT4H5M6S was correctly validated',
      ],
      [
        [
          'y' => 1,
          'h' => 4,
        ],
        'P1YT4H',
        'P1YT4H was correctly validated',
      ],
      [
        [],
        'P0M',
        'empty string was correctly validated',
      ],
    ];
  }

  /**
   * @covers ::getDurationStringFromDateInterval
   * @dataProvider getDurationStringFromDateIntervalDataProvider
   */
  public function testgetDurationStringFromDateInterval($input, $expectedResponse, $message) {
    $duration_service = new DurationService();
    $response = $duration_service
      ->getDurationStringFromDateInterval($input);
    $this
      ->assertSame($response, $expectedResponse, $message);
  }

  /**
   * Data provider for testgetDurationStringFromDateInterval().
   */
  public function getDurationStringFromDateIntervalDataProvider() {
    return [
      [
        new DateInterval('P0M'),
        'P0M',
        'P0M correctly retrieved as duration string from DateInterval',
      ],
      [
        new DateInterval('P1Y2M3DT4H5M6S'),
        'P1Y2M3DT4H5M6S',
        'P1Y2M3DT4H5M6S correctly retrieved as duration string from DateInterval',
      ],
    ];
  }

  /**
   * @covers ::convertDateArrayToDateInterval
   * @dataProvider convertDateArrayToDateIntervalDataProvider
   */
  public function testconvertDateArrayToDateInterval($input, $expectedResponse, $message) {
    $duration_service = new DurationService();
    $response = $duration_service
      ->convertDateArrayToDateInterval($input);
    $this
      ->assertEquals($response, $expectedResponse, $message);
    $this
      ->assertSame($duration_service
      ->getDurationStringFromDateInterval($response), $duration_service
      ->getDurationStringFromDateInterval($expectedResponse), 'Converted date array contains correct value');
  }

  /**
   * Data provider for testgetDurationStringFromDateInterval().
   */
  public function convertDateArrayToDateIntervalDataProvider() {
    return [
      [
        [
          'y' => 1,
          'm' => 2,
          'd' => 3,
          'h' => 4,
          'i' => 5,
          's' => 6,
        ],
        new DateInterval('P1Y2M3DT4H5M6S'),
        'P1Y2M3DT4H5M6S was correctly converted to a DateInterval',
      ],
      [
        [
          'y' => 0,
          'm' => 0,
          'd' => 0,
          'h' => 0,
          'i' => 0,
          's' => 0,
        ],
        new DateInterval('P0M'),
        'P0M was correctly converted to a DateInterval',
      ],
    ];
  }

  /**
   * @covers ::createEmptyDateInterval
   */
  public function testcreateEmptyDateInterval() {
    $duration_service = new DurationService();
    $response = $duration_service
      ->createEmptyDateInterval();
    $expected_response = new DateInterval('P0M');
    $this
      ->assertEquals($response, $expected_response, 'An empty date interval was successfully created');
  }

  /**
   * @covers ::getHumanReadableStringFromDateInterval
   * @dataProvider getHumanReadableStringFromDateIntervalDataProvider
   */
  public function testgetHumanReadableStringFromDateInterval($input, $expectedResponse, $message) {
    $duration_service = new DurationService();
    $container = new ContainerBuilder();
    $container
      ->set('string_translation', $this
      ->getStringTranslationStub());
    \Drupal::setContainer($container);
    $response = $duration_service
      ->getHumanReadableStringFromDateInterval($input['value'], $input['granularity']);
    if (is_a($response, 'Drupal\\Core\\StringTranslation\\TranslatableMarkup')) {
      $response = $response
        ->__toString();
    }
    $this
      ->assertSame($response, $expectedResponse, $message);
  }

  /**
   * Data provider for testgetHumanReadableStringFromDateInterval().
   */
  public function getHumanReadableStringFromDateIntervalDataProvider() {
    return [
      [
        [
          'value' => new DateInterval('P0M'),
          'granularity' => [
            'y' => TRUE,
            'm' => TRUE,
            'd' => TRUE,
            'h' => TRUE,
            'i' => TRUE,
            's' => TRUE,
          ],
        ],
        'Empty',
        'An empty DateInterval correctly outputted a response of "Empty"',
      ],
      [
        [
          'value' => new DateInterval('P1Y'),
          'granularity' => [
            'y' => TRUE,
            'm' => TRUE,
            'd' => TRUE,
            'h' => TRUE,
            'i' => TRUE,
            's' => TRUE,
          ],
        ],
        '1 year',
        'A date interval of P1Y correctly turned into text "1 year"',
      ],
      [
        [
          'value' => new DateInterval('P1Y1M'),
          'granularity' => [
            'y' => FALSE,
            'm' => TRUE,
            'd' => TRUE,
            'h' => TRUE,
            'i' => TRUE,
            's' => TRUE,
          ],
        ],
        '1 month',
        'A date interval of P1Y1M correctly turned into text "1 month", excluding the year as it is not part of the granularity',
      ],
      [
        [
          'value' => new DateInterval('P1Y2M3DT4H5M6S'),
          'granularity' => [
            'y' => TRUE,
            'm' => TRUE,
            'd' => TRUE,
            'h' => TRUE,
            'i' => TRUE,
            's' => TRUE,
          ],
        ],
        '1 year 2 months 3 days 4 hours 5 minutes 6 seconds',
        'A date interval of P1Y2M3DT4H5M6S correctly turned into text "1 year 2 months 3 days 4 hours 5 minutes 6 seconds"',
      ],
    ];
  }

  /**
   * @covers ::getSecondsFromDateInterval
   * @dataProvider getSecondsFromDateIntervalDataProvider
   */
  public function testgetSecondsFromDateInterval($input, $expectedResponse, $message) {
    $duration_service = new DurationService();
    $response = $duration_service
      ->getSecondsFromDateInterval($input);
    $this
      ->assertSame($response, $expectedResponse, $message);
  }

  /**
   * Data provider for testgetSecondsFromDateInterval().
   */
  public function getSecondsFromDateIntervalDataProvider() {
    return [
      [
        new DateInterval('P0M'),
        0,
        'Correctly got zero seconds for a duration of P0M',
      ],
      [
        new DateInterval('P1Y'),
        31536000,
        'Correctly got zero seconds for a duration of P0M',
      ],
      [
        new DateInterval('PT1H'),
        3600,
        'Correctly got zero seconds for a duration of P0M',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DurationServiceTest::checkDurationInvalidDataProvider public function Data provider for testCheckDurationInvalid().
DurationServiceTest::convertDateArrayToDateIntervalDataProvider public function Data provider for testgetDurationStringFromDateInterval().
DurationServiceTest::convertDateArrayToDurationStringDataProvider public function Data provider for testconvertDateArrayToDurationString().
DurationServiceTest::getDurationStringFromDateIntervalDataProvider public function Data provider for testgetDurationStringFromDateInterval().
DurationServiceTest::getHumanReadableStringFromDateIntervalDataProvider public function Data provider for testgetHumanReadableStringFromDateInterval().
DurationServiceTest::getSecondsFromDateIntervalDataProvider public function Data provider for testgetSecondsFromDateInterval().
DurationServiceTest::testCheckDurationInvalid public function @covers ::checkDurationInvalid @dataProvider checkDurationInvalidDataProvider
DurationServiceTest::testconvertDateArrayToDateInterval public function @covers ::convertDateArrayToDateInterval @dataProvider convertDateArrayToDateIntervalDataProvider
DurationServiceTest::testconvertDateArrayToDurationString public function @covers ::convertDateArrayToDurationString @dataProvider convertDateArrayToDurationStringDataProvider
DurationServiceTest::testcreateEmptyDateInterval public function @covers ::createEmptyDateInterval
DurationServiceTest::testgetDurationStringFromDateInterval public function @covers ::getDurationStringFromDateInterval @dataProvider getDurationStringFromDateIntervalDataProvider
DurationServiceTest::testgetHumanReadableStringFromDateInterval public function @covers ::getHumanReadableStringFromDateInterval @dataProvider getHumanReadableStringFromDateIntervalDataProvider
DurationServiceTest::testgetSecondsFromDateInterval public function @covers ::getSecondsFromDateInterval @dataProvider getSecondsFromDateIntervalDataProvider
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::setUp protected function 340