ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ByTryingTransformTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
27 require_once("./components/ILIAS/Refinery/tests/TestCase.php");
28 
30 {
31  private const ERROR = 'error_expected';
32 
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 }
testStringOrNull(mixed $value, mixed $expected)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$lang
Definition: xapiexit.php:25
testNullOrNumericOrString(mixed $value, mixed $expected)
testNullOrNumeric(mixed $value, mixed $expected)