View source
<?php
namespace Drupal\Tests\duration_field\Unit\Service;
use Drupal\Tests\UnitTestCase;
use Drupal\duration_field\Service\DurationService;
class DurationServiceTest extends UnitTestCase {
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);
}
}
public function checkDurationInvalidDataProvider() {
return [
[
'PY1D',
TRUE,
'PY1D correctly tested as invalid',
],
[
'P1Y2M3DT4H',
FALSE,
'P1Y2M3DT4H correctly tested as valid',
],
];
}
public function testConvertValue($input, $expectedResponse, $message) {
$response = DurationService::convertValue($input);
$this
->assertSame($response, $expectedResponse, $message);
}
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',
],
];
}
}