ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ByTryingTransformTest.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
3 /* Copyright (c) 2020 Nils Haagen <nils.haagen@concepts-and-training.de>, Extended GPL, see docs/LICENSE */
4 
5 require_once('./libs/composer/vendor/autoload.php');
6 
10 use ILIAS\Data;
11 
16 {
17  const ERROR = 'error_expected';
18 
19  public function setUp() : void
20  {
21  $df = new Data\Factory();
22  $lang = $this->getLanguage();
23  $this->refine = new \ILIAS\Refinery\Factory($df, $lang);
24  }
25 
26  public function NullOrNumericDataProvider()
27  {
28  return [
29  'empty string' => ['', null],
30  'empty string - one space' => [' ', null],
31  'empty string - more spaces' => [' ', null],
32  'null' => [null, null],
33  'string' => ['str', self::ERROR],
34  'int' => [1, 1],
35  'negative int' => [-1, -1],
36  'zero' => [0, 0],
37  'array' => [[], self::ERROR],
38  'bool (false)' => [false, self::ERROR],
39  'bool (true)' => [true, self::ERROR]
40  ];
41  }
42 
46  public function testNullOrNumeric($value, $expected)
47  {
48  $transformation = $this->refine->byTrying([
49  $this->refine->numeric()->isNumeric(),
50  $this->refine->kindlyTo()->null()
51  ]);
52 
53  if ($expected === self::ERROR) {
54  $this->expectException(ConstraintViolationException::class);
55  }
56  $transformed = $transformation->transform($value);
57  $this->assertEquals($expected, $transformed);
58  }
59 
60 
62  {
63  return [
64  'string' => ['str', 'str'],
65  'null' => [null, null],
66  'empty string' => ['', null],
67  'int' => [1, 1],
68  'bool (true)' => [true, self::ERROR],
69  'array' => [[], self::ERROR]
70  ];
71  }
72 
76  public function testNullOrNumericOrString($value, $expected)
77  {
78  $transformation = $this->refine->byTrying([
79  $this->refine->kindlyTo()->null(),
80  $this->refine->numeric()->isNumeric(),
81  $this->refine->to()->string()
82  ]);
83 
84  if ($expected === self::ERROR) {
85  $this->expectException(ConstraintViolationException::class);
86  }
87  $transformed = $transformation->transform($value);
88  $this->assertEquals($expected, $transformed);
89  }
90 
91  public function StringOrNullDataProvider()
92  {
93  return [
94  'string' => ['str', 'str'],
95  'null' => [null, null],
96  'empty string' => ['', ''],
97  'int' => [1, self::ERROR],
98  'array' => [[], self::ERROR]
99  ];
100  }
101 
105  public function testStringOrNull($value, $expected)
106  {
107  $transformation = $this->refine->byTrying([
108  $this->refine->to()->string(),
109  $this->refine->kindlyTo()->null()
110  ]);
111 
112  if ($expected === self::ERROR) {
113  $this->expectException(ConstraintViolationException::class);
114  }
115  $transformed = $transformation->transform($value);
116  $this->assertEquals($expected, $transformed);
117  }
118 }
Test ByTrying transformation.
testStringOrNull($value, $expected)
StringOrNullDataProvider
testNullOrNumeric($value, $expected)
NullOrNumericDataProvider
$lang
Definition: xapiexit.php:8
testNullOrNumericOrString($value, $expected)
NullOrNumericOrStringDataProvider