File tree Expand file tree Collapse file tree 8 files changed +120
-6
lines changed Expand file tree Collapse file tree 8 files changed +120
-6
lines changed Original file line number Diff line number Diff line change @@ -17,4 +17,5 @@ install:
17
17
- composer install
18
18
19
19
script :
20
- - vendor/bin/phpspec run --no-interaction
20
+ - vendor/bin/phpspec run --no-interaction
21
+ - vendor/bin/phpunit tests
Original file line number Diff line number Diff line change @@ -71,6 +71,7 @@ Currently the following types are supported:
71
71
* List
72
72
* Datetime
73
73
* Regex
74
+ * Optional
74
75
75
76
There are some open issues with ideas for more types. Feel free to send pull requests.
76
77
Original file line number Diff line number Diff line change 13
13
}
14
14
],
15
15
"require-dev" : {
16
- "phpspec/phpspec" : " ^3.2"
16
+ "phpspec/phpspec" : " ^3.2" ,
17
+ "phpunit/phpunit" : " ^5.6"
17
18
},
18
19
"autoload" : {
19
20
"psr-4" : {
20
21
"StructureCheck\\ " : [
21
22
" src"
23
+ ],
24
+ "StructureCheck\\ Test\\ " : [
25
+ " tests"
22
26
]
23
27
}
24
28
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace spec \StructureCheck \Type ;
4
+
5
+ use StructureCheck \Result ;
6
+ use StructureCheck \Type \OptionalType ;
7
+ use PhpSpec \ObjectBehavior ;
8
+ use StructureCheck \Type \TypeInterface ;
9
+
10
+ class OptionalTypeSpec extends ObjectBehavior
11
+ {
12
+ function it_is_initializable (TypeInterface $ childType )
13
+ {
14
+ $ this ->beConstructedWith ($ childType );
15
+ $ this ->shouldHaveType (OptionalType::class);
16
+ }
17
+
18
+ function it_should_return_the_value_from_the_child (TypeInterface $ childType ) {
19
+ $ this ->beConstructedWith ($ childType );
20
+ $ childType ->check (false )->willReturn (new Result (false , []));
21
+ $ this ->check (false )->isValid ()->shouldBe (false );
22
+ }
23
+ }
Original file line number Diff line number Diff line change @@ -55,14 +55,14 @@ public function check($value)
55
55
return $ result ;
56
56
}
57
57
58
- if ($ value instanceof Countable) {
58
+ if (! $ value instanceof Countable) {
59
59
return new Result (
60
60
false ,
61
61
[sprintf (self ::$ countableErrorMessage , json_encode ($ value ))]
62
62
);
63
63
}
64
64
65
- if (count ($ value ) = == $ this ->count ) {
65
+ if (count ($ value ) ! == $ this ->count ) {
66
66
return new Result (
67
67
false ,
68
68
[sprintf (self ::$ countErrorMessage , json_encode ($ value ), $ this ->count )]
Original file line number Diff line number Diff line change @@ -33,8 +33,10 @@ public function check($value)
33
33
34
34
foreach ($ this ->children as $ key => $ child ) {
35
35
if (!array_key_exists ($ key , $ value )) {
36
- $ valid = false ;
37
- $ errors [] = sprintf (self ::$ missingKeyErrorMessage , $ key );
36
+ if (!$ child instanceof OptionalType) {
37
+ $ valid = false ;
38
+ $ errors [] = sprintf (self ::$ missingKeyErrorMessage , $ key );
39
+ }
38
40
39
41
continue ;
40
42
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace StructureCheck \Type ;
4
+
5
+ use StructureCheck \ResultInterface ;
6
+
7
+ /**
8
+ * Class OptionalType
9
+ * @package StructureCheck\Type
10
+ * @author Christian Blank <christian@cubicl.de>
11
+ */
12
+ class OptionalType implements TypeInterface
13
+ {
14
+ /**
15
+ * @var TypeInterface
16
+ */
17
+ private $ child ;
18
+
19
+ /**
20
+ * OptionalType constructor.
21
+ * @param TypeInterface $child
22
+ */
23
+ public function __construct (TypeInterface $ child )
24
+ {
25
+ $ this ->child = $ child ;
26
+ }
27
+
28
+ /**
29
+ * @param mixed $value
30
+ *
31
+ * @return ResultInterface
32
+ */
33
+ public function check ($ value )
34
+ {
35
+ return $ this ->child ->check ($ value );
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace StructureCheck \Test \Integration \Type ;
4
+
5
+ use PHPUnit \Framework \TestCase ;
6
+ use StructureCheck \Checker ;
7
+ use StructureCheck \CheckerInterface ;
8
+ use StructureCheck \Type \AnyType ;
9
+ use StructureCheck \Type \ObjectType ;
10
+ use StructureCheck \Type \OptionalType ;
11
+
12
+ /**
13
+ * Class ObjectTypeTest
14
+ * @package StructureCheck\Test\Integration\Type
15
+ * @author Christian Blank <christian@cubicl.de>
16
+ */
17
+ class ObjectTypeTest extends TestCase
18
+ {
19
+ /**
20
+ * @var CheckerInterface
21
+ */
22
+ private $ checker ;
23
+
24
+ /**
25
+ *
26
+ */
27
+ protected function setUp ()
28
+ {
29
+ parent ::setUp ();
30
+ $ this ->checker = new Checker ();
31
+ }
32
+
33
+ /**
34
+ * @test
35
+ */
36
+ public function itShouldHandleAbsenceOfOptionalDeclaredType ()
37
+ {
38
+ $ structure = new ObjectType ([
39
+ 'opt ' => new OptionalType (new AnyType ())
40
+ ]);
41
+
42
+ $ actual = $ structure ->check ([]);
43
+
44
+ $ this ->assertSame (true , $ actual ->isValid ());
45
+ }
46
+ }
You can’t perform that action at this time.
0 commit comments