More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Datetime / DrupalDateTimeTest.php
index eff9f77294210c2647b136b32b3c67e03a9a3464..6c4777953545e7c74b6d223ecbbec90294413b61 100644 (file)
@@ -156,4 +156,60 @@ class DrupalDateTimeTest extends UnitTestCase {
     ];
   }
 
+  /**
+   * Tests setting the default time for date-only objects.
+   */
+  public function testDefaultDateTime() {
+    $utc = new \DateTimeZone('UTC');
+
+    $date = DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2017-05-23 22:58:00', $utc, ['langcode' => 'en']);
+    $this->assertEquals('22:58:00', $date->format('H:i:s'));
+    $date->setDefaultDateTime();
+    $this->assertEquals('12:00:00', $date->format('H:i:s'));
+  }
+
+  /**
+   * Tests that object methods are chainable.
+   *
+   * @covers ::__call
+   */
+  public function testChainable() {
+    $tz = new \DateTimeZone(date_default_timezone_get());
+    $date = new DrupalDateTime('now', $tz, ['langcode' => 'en']);
+
+    $date->setTimestamp(12345678);
+    $rendered = $date->render();
+    $this->assertEquals('1970-05-24 07:21:18 Australia/Sydney', $rendered);
+
+    $date->setTimestamp(23456789);
+    $rendered = $date->setTimezone(new \DateTimeZone('America/New_York'))->render();
+    $this->assertEquals('1970-09-29 07:46:29 America/New_York', $rendered);
+  }
+
+  /**
+   * Tests that non-chainable methods work.
+   *
+   * @covers ::__call
+   */
+  public function testChainableNonChainable() {
+    $tz = new \DateTimeZone(date_default_timezone_get());
+    $datetime1 = new DrupalDateTime('2009-10-11 12:00:00', $tz, ['langcode' => 'en']);
+    $datetime2 = new DrupalDateTime('2009-10-13 12:00:00', $tz, ['langcode' => 'en']);
+    $interval = $datetime1->diff($datetime2);
+    $this->assertInstanceOf(\DateInterval::class, $interval);
+    $this->assertEquals('+2 days', $interval->format('%R%a days'));
+  }
+
+  /**
+   * Tests that chained calls to non-existent functions throw an exception.
+   *
+   * @covers ::__call
+   */
+  public function testChainableNonCallable() {
+    $this->setExpectedException(\BadMethodCallException::class, 'Call to undefined method Drupal\Core\Datetime\DrupalDateTime::nonexistent()');
+    $tz = new \DateTimeZone(date_default_timezone_get());
+    $date = new DrupalDateTime('now', $tz, ['langcode' => 'en']);
+    $date->setTimezone(new \DateTimeZone('America/New_York'))->nonexistent();
+  }
+
 }