-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathDateIntervalFieldTest.php
More file actions
75 lines (59 loc) · 2.81 KB
/
Copy pathDateIntervalFieldTest.php
File metadata and controls
75 lines (59 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Unit\Field;
use EasyCorp\Bundle\EasyAdminBundle\Field\Configurator\DateIntervalConfigurator;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateIntervalField;
class DateIntervalFieldTest extends AbstractFieldTest
{
protected function setUp(): void
{
parent::setUp();
$this->configurator = new DateIntervalConfigurator();
}
public function testFieldWithoutValue(): void
{
$field = DateIntervalField::new('foo');
$field->setFieldFqcn(DateIntervalField::class);
$fieldDto = $this->configure($field);
$this->assertNull($fieldDto->getFormattedValue());
}
public function testFieldWithoutCustomFormatLeavesValueForTemplateRendering(): void
{
$field = DateIntervalField::new('foo')->setValue(new \DateInterval('P2Y4DT6H8M'));
$field->setFieldFqcn(DateIntervalField::class);
$fieldDto = $this->configure($field);
// sentinel empty string keeps CommonPostConfigurator from swapping the template
$this->assertSame('', $fieldDto->getFormattedValue());
$this->assertInstanceOf(\DateInterval::class, $fieldDto->getValue());
}
public function testFieldWithPerFieldFormat(): void
{
$field = DateIntervalField::new('foo')
->setValue(new \DateInterval('P1Y2M3D'))
->setFormat('%y years, %m months, %d days');
$field->setFieldFqcn(DateIntervalField::class);
$fieldDto = $this->configure($field);
$this->assertSame('1 years, 2 months, 3 days', $fieldDto->getFormattedValue());
}
public function testTemplateRendersLocalizedPluralizedString(): void
{
$field = DateIntervalField::new('foo')->setValue(new \DateInterval('P1Y2M3DT4H5M6S'));
$field->setFieldFqcn(DateIntervalField::class);
$fieldDto = $this->configure($field);
$html = $this->renderFieldTemplate($fieldDto, $this->entityDto, $this->adminContext);
$this->assertStringContainsString('1 year', $html);
$this->assertStringContainsString('2 months', $html);
$this->assertStringContainsString('3 days', $html);
$this->assertStringContainsString('4 hours', $html);
$this->assertStringContainsString('5 minutes', $html);
$this->assertStringContainsString('6 seconds', $html);
$this->assertStringContainsString('datetime="P1Y2M3DT4H5M6S"', $html);
}
public function testTemplateRendersZeroDurationAsEmptyLabel(): void
{
$field = DateIntervalField::new('foo')->setValue(new \DateInterval('PT0S'));
$field->setFieldFqcn(DateIntervalField::class);
$fieldDto = $this->configure($field);
$html = $this->renderFieldTemplate($fieldDto, $this->entityDto, $this->adminContext);
$this->assertStringContainsString('0 seconds', $html);
}
}