ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ByTryingTransformTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Refinery\Factory as Refinery;
24use ILIAS\Data\Factory as DataFactory;
25use PHPUnit\Framework\Attributes\DataProvider;
26
27require_once("./components/ILIAS/Refinery/tests/TestCase.php");
28
30{
31 private const ERROR = 'error_expected';
32
33 private Refinery $refinery;
34
35 protected function setUp(): void
36 {
37 $df = new DataFactory();
38 $lang = $this->getLanguage();
39 $this->refinery = new Refinery($df, $lang);
40 }
41
42 public static function NullOrNumericDataProvider(): array
43 {
44 return [
45 'empty string' => ['', null],
46 'empty string - one space' => [' ', null],
47 'empty string - more spaces' => [' ', null],
48 'null' => [null, null],
49 'string' => ['str', self::ERROR],
50 'int' => [1, 1],
51 'negative int' => [-1, -1],
52 'zero' => [0, 0],
53 'array' => [[], self::ERROR],
54 'bool (false)' => [false, self::ERROR],
55 'bool (true)' => [true, self::ERROR]
56 ];
57 }
58
59 #[DataProvider('NullOrNumericDataProvider')]
60 public function testNullOrNumeric(mixed $value, mixed $expected): void
61 {
62 $transformation = $this->refinery->byTrying([
63 $this->refinery->numeric()->isNumeric(),
64 $this->refinery->kindlyTo()->null()
65 ]);
66
67 if ($expected === self::ERROR) {
68 $this->expectException(ConstraintViolationException::class);
69 }
70 $transformed = $transformation->transform($value);
71 $this->assertEquals($expected, $transformed);
72 }
73
74
75 public static function NullOrNumericOrStringDataProvider(): array
76 {
77 return [
78 'string' => ['str', 'str'],
79 'null' => [null, null],
80 'empty string' => ['', null],
81 'int' => [1, 1],
82 'bool (true)' => [true, self::ERROR],
83 'array' => [[], self::ERROR]
84 ];
85 }
86
87 #[DataProvider('NullOrNumericOrStringDataProvider')]
88 public function testNullOrNumericOrString(mixed $value, mixed $expected): void
89 {
90 $transformation = $this->refinery->byTrying([
91 $this->refinery->kindlyTo()->null(),
92 $this->refinery->numeric()->isNumeric(),
93 $this->refinery->to()->string()
94 ]);
95
96 if ($expected === self::ERROR) {
97 $this->expectException(ConstraintViolationException::class);
98 }
99 $transformed = $transformation->transform($value);
100 $this->assertEquals($expected, $transformed);
101 }
102
103 public static function StringOrNullDataProvider(): array
104 {
105 return [
106 'string' => ['str', 'str'],
107 'null' => [null, null],
108 'empty string' => ['', ''],
109 'int' => [1, self::ERROR],
110 'array' => [[], self::ERROR]
111 ];
112 }
113
114 #[DataProvider('StringOrNullDataProvider')]
115 public function testStringOrNull(mixed $value, mixed $expected): void
116 {
117 $transformation = $this->refinery->byTrying([
118 $this->refinery->to()->string(),
119 $this->refinery->kindlyTo()->null()
120 ]);
121
122 if ($expected === self::ERROR) {
123 $this->expectException(ConstraintViolationException::class);
124 }
125 $transformed = $transformation->transform($value);
126 $this->assertEquals($expected, $transformed);
127 }
128}
testNullOrNumericOrString(mixed $value, mixed $expected)
testNullOrNumeric(mixed $value, mixed $expected)
testStringOrNull(mixed $value, mixed $expected)
Builds data types.
Definition: Factory.php:36
$lang
Definition: xapiexit.php:25