class DurationServiceTest in Duration Field 8
Same name and namespace in other branches
- 8.2 tests/src/Unit/Service/DurationServiceTest.php \Drupal\Tests\duration_field\Unit\Service\DurationServiceTest
- 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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\duration_field\Unit\Service\DurationServiceTest
Expanded class hierarchy of DurationServiceTest
File
- tests/
src/ Unit/ Service/ DurationServiceTest.php, line 12
Namespace
Drupal\Tests\duration_field\Unit\ServiceView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DurationServiceTest:: |
public | function | Data provider for testCheckDurationInvalid. | |
DurationServiceTest:: |
public | function | Data provider for testConvertValue. | |
DurationServiceTest:: |
public | function | @covers ::checkDurationInvalid @dataProvider checkDurationInvalidDataProvider | |
DurationServiceTest:: |
public | function | @covers ::convertValue @dataProvider convertValueDataProvider | |
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. | |
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 |