xref: /PHP-8.4/ext/date/tests/bug-gh11416.phpt (revision e157da11)
1--TEST--
2Bug GH-11416: Crash with DatePeriod when uninitialised objects are passed in
3--INI--
4date.timezone=UTC
5--FILE--
6<?php
7$now = new DateTimeImmutable();
8$simpleInterval = new DateInterval("P2D");
9
10$date = (new ReflectionClass(DateTime::class))->newInstanceWithoutConstructor();
11try {
12	new DatePeriod($date, new DateInterval('P1D'), 2);
13} catch (Error $e) {
14	echo get_class($e), ': ', $e->getMessage(), "\n";
15}
16
17$date = (new ReflectionClass(DateTime::class))->newInstanceWithoutConstructor();
18try {
19	new DatePeriod($now, new DateInterval('P1D'), $date);
20} catch (Error $e) {
21	echo get_class($e), ': ', $e->getMessage(), "\n";
22}
23
24$date = (new ReflectionClass(DateTime::class))->newInstanceWithoutConstructor();
25$dateperiod = (new ReflectionClass(DatePeriod::class))->newInstanceWithoutConstructor();
26$dateinterval = (new ReflectionClass(DateInterval::class))->newInstanceWithoutConstructor();
27try {
28	$dateperiod->__unserialize(['start' => $date]);
29} catch (Error $e) {
30	echo get_class($e), ': ', $e->getMessage(), "\n";
31}
32
33try {
34	$dateperiod->__unserialize(['start' => $now, 'end' => $date]);
35} catch (Error $e) {
36	echo get_class($e), ': ', $e->getMessage(), "\n";
37}
38
39try {
40	$dateperiod->__unserialize(['start' => $now, 'end' => $now, 'current' => $date]);
41} catch (Error $e) {
42	echo get_class($e), ': ', $e->getMessage(), "\n";
43}
44
45try {
46	$dateperiod->__unserialize(['start' => $now, 'end' => $now, 'current' => $now, 'interval' => $dateinterval]);
47} catch (Error $e) {
48	echo get_class($e), ': ', $e->getMessage(), "\n";
49}
50
51try {
52	$dateperiod->__unserialize([
53		'start' => $now, 'end' => $now, 'current' => $now, 'interval' => $simpleInterval,
54		'recurrences' => 2, 'include_start_date' => true, 'include_end_date' => true,
55	]);
56	echo "DatePeriod::__unserialize: SUCCESS\n";
57} catch (Error $e) {
58	echo get_class($e), ': ', $e->getMessage(), "\n";
59}
60echo "OK\n";
61?>
62--EXPECT--
63DateObjectError: Object of type DateTimeInterface has not been correctly initialized by calling parent::__construct() in its constructor
64DateObjectError: Object of type DateTimeInterface has not been correctly initialized by calling parent::__construct() in its constructor
65Error: Invalid serialization data for DatePeriod object
66Error: Invalid serialization data for DatePeriod object
67Error: Invalid serialization data for DatePeriod object
68Error: Invalid serialization data for DatePeriod object
69DatePeriod::__unserialize: SUCCESS
70OK
71