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