comparison core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
85 * Absolute flag for DateTimePlus::diff method. 85 * Absolute flag for DateTimePlus::diff method.
86 * 86 *
87 * @dataProvider providerTestInvalidDateDiff 87 * @dataProvider providerTestInvalidDateDiff
88 */ 88 */
89 public function testInvalidDateDiff($input1, $input2, $absolute) { 89 public function testInvalidDateDiff($input1, $input2, $absolute) {
90 $this->setExpectedException(\BadMethodCallException::class, 'Method Drupal\Component\Datetime\DateTimePlus::diff expects parameter 1 to be a \DateTime or \Drupal\Component\Datetime\DateTimePlus object'); 90 if (method_exists($this, 'expectException')) {
91 $this->expectException(\BadMethodCallException::class);
92 $this->expectExceptionMessage('Method Drupal\Component\Datetime\DateTimePlus::diff expects parameter 1 to be a \DateTime or \Drupal\Component\Datetime\DateTimePlus object');
93 }
94 else {
95 $this->setExpectedException(\BadMethodCallException::class, 'Method Drupal\Component\Datetime\DateTimePlus::diff expects parameter 1 to be a \DateTime or \Drupal\Component\Datetime\DateTimePlus object');
96 }
91 $interval = $input1->diff($input2, $absolute); 97 $interval = $input1->diff($input2, $absolute);
92 } 98 }
93 99
94 /** 100 /**
95 * Test creating dates from invalid array input. 101 * Test creating dates from invalid array input.
102 * The Exception subclass to expect to be thrown. 108 * The Exception subclass to expect to be thrown.
103 * 109 *
104 * @dataProvider providerTestInvalidDateArrays 110 * @dataProvider providerTestInvalidDateArrays
105 */ 111 */
106 public function testInvalidDateArrays($input, $timezone, $class) { 112 public function testInvalidDateArrays($input, $timezone, $class) {
107 $this->setExpectedException($class); 113 if (method_exists($this, 'expectException')) {
114 $this->expectException($class);
115 }
116 else {
117 $this->setExpectedException($class);
118 }
108 $this->assertInstanceOf( 119 $this->assertInstanceOf(
109 '\Drupal\Component\DateTimePlus', 120 '\Drupal\Component\DateTimePlus',
110 DateTimePlus::createFromArray($input, $timezone) 121 DateTimePlus::createFromArray($input, $timezone)
111 ); 122 );
112 } 123 }
240 * The Exception subclass to expect to be thrown. 251 * The Exception subclass to expect to be thrown.
241 * 252 *
242 * @dataProvider providerTestInvalidDates 253 * @dataProvider providerTestInvalidDates
243 */ 254 */
244 public function testInvalidDates($input, $timezone, $format, $message, $class) { 255 public function testInvalidDates($input, $timezone, $format, $message, $class) {
245 $this->setExpectedException($class); 256 if (method_exists($this, 'expectException')) {
257 $this->expectException($class);
258 }
259 else {
260 $this->setExpectedException($class);
261 }
246 DateTimePlus::createFromFormat($format, $input, $timezone); 262 DateTimePlus::createFromFormat($format, $input, $timezone);
247 } 263 }
248 264
249 /** 265 /**
250 * Tests that DrupalDateTime can detect the right timezone to use. 266 * Tests that DrupalDateTime can detect the right timezone to use.
798 $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => FALSE]); 814 $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => FALSE]);
799 $this->assertEquals('0011-03-31 17:44:00', $date->format('Y-m-d H:i:s')); 815 $this->assertEquals('0011-03-31 17:44:00', $date->format('Y-m-d H:i:s'));
800 816
801 // Parse the same date with ['validate_format' => TRUE] and make sure we 817 // Parse the same date with ['validate_format' => TRUE] and make sure we
802 // get the expected exception. 818 // get the expected exception.
803 $this->setExpectedException(\UnexpectedValueException::class); 819 if (method_exists($this, 'expectException')) {
820 $this->expectException(\UnexpectedValueException::class);
821 }
822 else {
823 $this->setExpectedException(\UnexpectedValueException::class);
824 }
804 $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => TRUE]); 825 $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => TRUE]);
826 }
827
828 /**
829 * Tests setting the default time for date-only objects.
830 */
831 public function testDefaultDateTime() {
832 $utc = new \DateTimeZone('UTC');
833
834 $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '2017-05-23 22:58:00', $utc);
835 $this->assertEquals('22:58:00', $date->format('H:i:s'));
836 $date->setDefaultDateTime();
837 $this->assertEquals('12:00:00', $date->format('H:i:s'));
805 } 838 }
806 839
807 /** 840 /**
808 * Tests that object methods are chainable. 841 * Tests that object methods are chainable.
809 * 842 *
845 * Tests that chained calls to non-existent functions throw an exception. 878 * Tests that chained calls to non-existent functions throw an exception.
846 * 879 *
847 * @covers ::__call 880 * @covers ::__call
848 */ 881 */
849 public function testChainableNonCallable() { 882 public function testChainableNonCallable() {
850 $this->setExpectedException(\BadMethodCallException::class, 'Call to undefined method Drupal\Component\Datetime\DateTimePlus::nonexistent()'); 883 if (method_exists($this, 'expectException')) {
884 $this->expectException(\BadMethodCallException::class);
885 $this->expectExceptionMessage('Call to undefined method Drupal\Component\Datetime\DateTimePlus::nonexistent()');
886 }
887 else {
888 $this->setExpectedException(\BadMethodCallException::class, 'Call to undefined method Drupal\Component\Datetime\DateTimePlus::nonexistent()');
889 }
851 $date = new DateTimePlus('now', 'Australia/Sydney'); 890 $date = new DateTimePlus('now', 'Australia/Sydney');
852 $date->setTimezone(new \DateTimeZone('America/New_York'))->nonexistent(); 891 $date->setTimezone(new \DateTimeZone('America/New_York'))->nonexistent();
853 } 892 }
854 893
855 } 894 }