ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ChangeTimezoneTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2019 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
8 
13 {
14  protected function setUp() : void
15  {
16  $df = new Factory();
17  $this->dt = new DateTime\Group($df);
18  }
19 
20 
21  public function testTransform()
22  {
23  $dat = '2019-05-26 13:15:01';
24  $origin_tz = 'Europe/Berlin';
25  $target_tz = 'Europe/London';
26  $origin = new \DateTimeImmutable($dat, new \DateTimeZone($origin_tz));
27  $expected = new \DateTimeImmutable($dat, new \DateTimeZone($target_tz));
28  $trans = $this->dt->changeTimezone($target_tz);
29 
30  $this->assertEquals(
31  $expected,
32  $trans->transform($origin)
33  );
34  }
35 
36  public function testTransformValues()
37  {
38  $dat = '2019-05-26 13:15:01';
39  $origin_tz = 'Europe/Berlin';
40  $target_tz = 'America/El_Salvador';
41  $origin = new \DateTimeImmutable($dat, new \DateTimeZone($origin_tz));
42  $trans = $this->dt->changeTimezone($target_tz);
43  $this->assertEquals(
44  $dat,
45  date_format($trans->transform($origin), 'Y-m-d H:i:s')
46  );
47  }
48 
49  public function testNullTransform()
50  {
51  $trans = $this->dt->changeTimezone('Europe/Berlin');
52  $this->expectException(\InvalidArgumentException::class);
53  $trans->transform(null);
54  }
55 
56  public function testInvalidTransform()
57  {
58  $this->expectException(\InvalidArgumentException::class);
59  $trans = $this->dt->changeTimezone('Europe/Berlin');
60  $trans->transform('erroneous');
61  }
62 
63  public function testInvoke()
64  {
65  $dat = '2019/05/26 16:05:22';
66  $origin_tz = 'Europe/Berlin';
67  $target_tz = 'Europe/London';
68  $origin = new \DateTimeImmutable($dat, new \DateTimeZone($origin_tz));
69  $expected = new \DateTimeImmutable($dat, new \DateTimeZone($target_tz));
70  $trans = $this->dt->changeTimezone($target_tz);
71  $this->assertEquals($expected, $trans($origin));
72  }
73 
74  public function testApplyToOK()
75  {
76  $trans = $this->dt->changeTimezone('Europe/London');
77  $value = '2019/05/26';
78  $origin = new \DateTimeImmutable($value);
79  $expected = new \DateTimeImmutable($value, new \DateTimeZone('Europe/London'));
80 
81  $df = new \ILIAS\Data\Factory();
82  $ok = $df->ok($origin);
83 
84  $result = $trans->applyTo($ok);
85  $this->assertEquals($expected, $result->value());
86  $this->assertFalse($result->isError());
87  }
88 
89  public function testApplyToFail()
90  {
91  $trans = $this->dt->changeTimezone('Europe/London');
92  $df = new \ILIAS\Data\Factory();
93  $ok = $df->ok('not_a_date');
94 
95  $result = $trans->applyTo($ok);
96  $this->assertTrue($result->isError());
97  }
98 }
$result
Builds data types.
Definition: Factory.php:19
Testcase for timezone transformation.