You are here

class DurationServiceTest in Duration Field 8

Same name and namespace in other branches
  1. 8.2 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 12

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 ::convertValue
   * @dataProvider convertValueDataProvider
   */
  public function testConvertValue($input, $expectedResponse, $message) {
    $response = DurationService::convertValue($input);
    $this
      ->assertSame($response, $expectedResponse, $message);
  }

  /**
   * Data provider for testConvertValue.
   */
  public function convertValueDataProvider() {
    return [
      [
        [
          'year' => 1,
          'month' => 2,
          'day' => 3,
          'hour' => 4,
          'minute' => 5,
          'second' => 6,
        ],
        'P1Y2M3DT4H5M6S',
        'P1Y2M3DT4H5M6S was correctly validated',
      ],
      [
        [
          'year' => 1,
          'month' => 2,
          'day' => 3,
        ],
        'P1Y2M3D',
        '
        P1Y2M3D was correctly validated',
      ],
      [
        [
          'hour' => 4,
          'minute' => 5,
          'second' => 6,
        ],
        'PT4H5M6S',
        'PT4H5M6S was correctly validated',
      ],
      [
        [
          'year' => 1,
          'hour' => 4,
        ],
        'P1YT4H',
        'P1YT4H was correctly validated',
      ],
      [
        [],
        '',
        'empty string was correctly validated',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DurationServiceTest::checkDurationInvalidDataProvider public function Data provider for testCheckDurationInvalid.
DurationServiceTest::convertValueDataProvider public function Data provider for testConvertValue.
DurationServiceTest::testCheckDurationInvalid public function @covers ::checkDurationInvalid @dataProvider checkDurationInvalidDataProvider
DurationServiceTest::testConvertValue public function @covers ::convertValue @dataProvider convertValueDataProvider
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