/
MapPropertyTypesToFieldsTest.php
91 lines (78 loc) · 2.98 KB
/
MapPropertyTypesToFieldsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace spec\watoki\qrator\form;
use watoki\qrator\form\fields\ArrayField;
use watoki\qrator\form\fields\CheckboxField;
use watoki\qrator\form\fields\DateTimeField;
use watoki\qrator\form\fields\InputField;
use watoki\qrator\representer\generic\GenericActionRepresenter;
use watoki\scrut\Specification;
/**
* @property \spec\watoki\reflect\fixtures\ClassFixture class <-
*/
class MapPropertyTypesToFieldsTest extends Specification {
function testUnknown() {
$this->class->givenTheClass_WithTheBody('mapUnknown\Action', '
public $unknown;
');
$this->whenIGetTheFieldsOf('mapUnknown\Action');
$this->then_ShouldBeA('unknown', InputField::class);
}
function testString() {
$this->class->givenTheClass_WithTheBody('mapString\Action', '
/** @var string */
public $string;
');
$this->whenIGetTheFieldsOf('mapString\Action');
$this->then_ShouldBeA('string', InputField::class);
}
function testArray() {
$this->class->givenTheClass_WithTheBody('mapArray\Action', '
/** @var array|string[] */
public $array;
');
$this->whenIGetTheFieldsOf('mapArray\Action');
$this->then_ShouldBeA('array', ArrayField::class);
$this->thenTheInnerFieldOf_ShouldBeA('array', InputField::class);
}
function testDateTime() {
$this->class->givenTheClass_WithTheBody('mapDateTime\Action', '
/** @var \DateTime */
public $date;
');
$this->whenIGetTheFieldsOf('mapDateTime\Action');
$this->then_ShouldBeA('date', DateTimeField::class);
}
function testBoolean() {
$this->class->givenTheClass_WithTheBody('mapBoolean\Action', '
/** @var bool */
public $boolean;
');
$this->whenIGetTheFieldsOf('mapBoolean\Action');
$this->then_ShouldBeA('boolean', CheckboxField::class);
}
##################################################################################################
/** @var \watoki\qrator\form\Field[] */
private $fields;
private function whenIGetTheFieldsOf($actionClass) {
$representer = new GenericActionRepresenter($actionClass, $this->factory);
$this->fields = $representer->getFields($representer->create());
}
private function find($property) {
foreach ($this->fields as $field) {
if ($field->getName() == $property) {
return $field;
}
}
throw new \Exception("[$property] not found");
}
private function then_ShouldBeA($property, $fieldClass) {
$this->assertInstanceOf($fieldClass, $this->find($property));
}
private function thenTheInnerFieldOf_ShouldBeA($property, $fieldClass) {
$field = $this->find($property);
if (!($field instanceof ArrayField)) {
$this->fail("No an ArrayField");
}
$this->assertInstanceOf($fieldClass, $field->getInnerField());
}
}