ILIAS  release_8 Revision v8.24
ByTryingTransformTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21use ILIAS\Refinery\Factory as Refinery;
24use ILIAS\Data\Factory as DataFactory;
25
27{
28 private const ERROR = 'error_expected';
29
30 private Refinery $refinery;
31
32 protected function setUp(): void
33 {
34 $df = new DataFactory();
35 $lang = $this->getLanguage();
36 $this->refinery = new Refinery($df, $lang);
37 }
38
39 public function NullOrNumericDataProvider(): array
40 {
41 return [
42 'empty string' => ['', null],
43 'empty string - one space' => [' ', null],
44 'empty string - more spaces' => [' ', null],
45 'null' => [null, null],
46 'string' => ['str', self::ERROR],
47 'int' => [1, 1],
48 'negative int' => [-1, -1],
49 'zero' => [0, 0],
50 'array' => [[], self::ERROR],
51 'bool (false)' => [false, self::ERROR],
52 'bool (true)' => [true, self::ERROR]
53 ];
54 }
55
61 public function testNullOrNumeric($value, $expected): void
62 {
63 $transformation = $this->refinery->byTrying([
64 $this->refinery->numeric()->isNumeric(),
65 $this->refinery->kindlyTo()->null()
66 ]);
67
68 if ($expected === self::ERROR) {
69 $this->expectException(ConstraintViolationException::class);
70 }
71 $transformed = $transformation->transform($value);
72 $this->assertEquals($expected, $transformed);
73 }
74
75
76 public function NullOrNumericOrStringDataProvider(): array
77 {
78 return [
79 'string' => ['str', 'str'],
80 'null' => [null, null],
81 'empty string' => ['', null],
82 'int' => [1, 1],
83 'bool (true)' => [true, self::ERROR],
84 'array' => [[], self::ERROR]
85 ];
86 }
87
93 public function testNullOrNumericOrString($value, $expected): void
94 {
95 $transformation = $this->refinery->byTrying([
96 $this->refinery->kindlyTo()->null(),
97 $this->refinery->numeric()->isNumeric(),
98 $this->refinery->to()->string()
99 ]);
100
101 if ($expected === self::ERROR) {
102 $this->expectException(ConstraintViolationException::class);
103 }
104 $transformed = $transformation->transform($value);
105 $this->assertEquals($expected, $transformed);
106 }
107
108 public function StringOrNullDataProvider(): array
109 {
110 return [
111 'string' => ['str', 'str'],
112 'null' => [null, null],
113 'empty string' => ['', ''],
114 'int' => [1, self::ERROR],
115 'array' => [[], self::ERROR]
116 ];
117 }
118
124 public function testStringOrNull($value, $expected): void
125 {
126 $transformation = $this->refinery->byTrying([
127 $this->refinery->to()->string(),
128 $this->refinery->kindlyTo()->null()
129 ]);
130
131 if ($expected === self::ERROR) {
132 $this->expectException(ConstraintViolationException::class);
133 }
134 $transformed = $transformation->transform($value);
135 $this->assertEquals($expected, $transformed);
136 }
137}
testNullOrNumeric($value, $expected)
@dataProvider NullOrNumericDataProvider
testNullOrNumericOrString($value, $expected)
@dataProvider NullOrNumericOrStringDataProvider
testStringOrNull($value, $expected)
@dataProvider StringOrNullDataProvider
Builds data types.
Definition: Factory.php:21
$lang
Definition: xapiexit.php:26