ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ByTryingTransformTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
26 require_once("./components/ILIAS/Refinery/tests/TestCase.php");
27 
29 {
30  private const ERROR = 'error_expected';
31 
33 
34  protected function setUp(): void
35  {
36  $df = new DataFactory();
37  $lang = $this->getLanguage();
38  $this->refinery = new Refinery($df, $lang);
39  }
40 
41  public static function NullOrNumericDataProvider(): array
42  {
43  return [
44  'empty string' => ['', null],
45  'empty string - one space' => [' ', null],
46  'empty string - more spaces' => [' ', null],
47  'null' => [null, null],
48  'string' => ['str', self::ERROR],
49  'int' => [1, 1],
50  'negative int' => [-1, -1],
51  'zero' => [0, 0],
52  'array' => [[], self::ERROR],
53  'bool (false)' => [false, self::ERROR],
54  'bool (true)' => [true, self::ERROR]
55  ];
56  }
57 
63  public function testNullOrNumeric($value, $expected): void
64  {
65  $transformation = $this->refinery->byTrying([
66  $this->refinery->numeric()->isNumeric(),
67  $this->refinery->kindlyTo()->null()
68  ]);
69 
70  if ($expected === self::ERROR) {
71  $this->expectException(ConstraintViolationException::class);
72  }
73  $transformed = $transformation->transform($value);
74  $this->assertEquals($expected, $transformed);
75  }
76 
77 
78  public static function NullOrNumericOrStringDataProvider(): array
79  {
80  return [
81  'string' => ['str', 'str'],
82  'null' => [null, null],
83  'empty string' => ['', null],
84  'int' => [1, 1],
85  'bool (true)' => [true, self::ERROR],
86  'array' => [[], self::ERROR]
87  ];
88  }
89 
95  public function testNullOrNumericOrString($value, $expected): void
96  {
97  $transformation = $this->refinery->byTrying([
98  $this->refinery->kindlyTo()->null(),
99  $this->refinery->numeric()->isNumeric(),
100  $this->refinery->to()->string()
101  ]);
102 
103  if ($expected === self::ERROR) {
104  $this->expectException(ConstraintViolationException::class);
105  }
106  $transformed = $transformation->transform($value);
107  $this->assertEquals($expected, $transformed);
108  }
109 
110  public static function StringOrNullDataProvider(): array
111  {
112  return [
113  'string' => ['str', 'str'],
114  'null' => [null, null],
115  'empty string' => ['', ''],
116  'int' => [1, self::ERROR],
117  'array' => [[], self::ERROR]
118  ];
119  }
120 
126  public function testStringOrNull($value, $expected): void
127  {
128  $transformation = $this->refinery->byTrying([
129  $this->refinery->to()->string(),
130  $this->refinery->kindlyTo()->null()
131  ]);
132 
133  if ($expected === self::ERROR) {
134  $this->expectException(ConstraintViolationException::class);
135  }
136  $transformed = $transformation->transform($value);
137  $this->assertEquals($expected, $transformed);
138  }
139 }
testNullOrNumericOrString($value, $expected)
NullOrNumericOrStringDataProvider
testStringOrNull($value, $expected)
StringOrNullDataProvider
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...
testNullOrNumeric($value, $expected)
NullOrNumericDataProvider
$lang
Definition: xapiexit.php:25