From 7ce66d4de8159a5a19de2dc7b87000c400915e8e Mon Sep 17 00:00:00 2001 From: Gert de Pagter Date: Sun, 25 Sep 2022 13:58:10 +0200 Subject: [PATCH] Add failing test case for unions Seems unions for scalars with additional data (non-empty) lose their extra data --- tests/Type/PslTypeSpecifyingExtensionTest.php | 1 + tests/Type/data/bug-9.php | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/Type/data/bug-9.php diff --git a/tests/Type/PslTypeSpecifyingExtensionTest.php b/tests/Type/PslTypeSpecifyingExtensionTest.php index cfcb288..04dc61e 100644 --- a/tests/Type/PslTypeSpecifyingExtensionTest.php +++ b/tests/Type/PslTypeSpecifyingExtensionTest.php @@ -17,6 +17,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/coerce.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/assert.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/matches.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9.php'); if (InstalledVersions::satisfies(new VersionParser(), 'azjezz/psl', '<2.0.0')) { yield from $this->gatherAssertTypes(__DIR__ . '/data/complexTypev1.php'); } else { diff --git a/tests/Type/data/bug-9.php b/tests/Type/data/bug-9.php new file mode 100644 index 0000000..5d454e7 --- /dev/null +++ b/tests/Type/data/bug-9.php @@ -0,0 +1,42 @@ + Type\non_empty_string(), + 'bar' => Type\nullable(Type\non_empty_string()), + 'baz' => Type\optional(Type\non_empty_string()), + 'other' => Type\union(Type\non_empty_string(), Type\positive_int()) + ])->coerce($input); + + assertType('array{foo: non-empty-string, bar: non-empty-string|null, baz?: non-empty-string, other: int<1, max>|non-empty_string}', $output); +} + + +/** + * @param mixed $input + */ +function checkNoShape($input): void +{ + $output = Type\union(Type\non_empty_string(), Type\positive_int())->coerce($input); + + assertType('int<1, max>|non-empty-string', $output); +} + +/** + * @param mixed $input + */ +function checkNoUnion($input): void +{ + $output = Type\non_empty_string()->assert($input); + + assertType('non-empty-string', $output); +}