33namespace Smoren \ArrayView \Tests \Unit \ArrayView ;
44
55use Smoren \ArrayView \Exceptions \IndexError ;
6+ use Smoren \ArrayView \Exceptions \KeyError ;
67use Smoren \ArrayView \Exceptions \ValueError ;
78use Smoren \ArrayView \Views \ArrayView ;
89
@@ -17,7 +18,8 @@ public function testReadIndexError(array $source, array $indexes)
1718 foreach ($ indexes as $ index ) {
1819 try {
1920 $ _ = $ view [$ index ];
20- $ this ->fail ();
21+ $ strIndex = strval ($ index );
22+ $ this ->fail ("IndexError not thrown for key: \"{$ strIndex }\"" );
2123 } catch (IndexError $ e ) {
2224 $ this ->assertSame ("Index {$ index } is out of range. " , $ e ->getMessage ());
2325 }
@@ -33,13 +35,48 @@ public function testWriteIndexError(array $source, array $indexes)
3335 foreach ($ indexes as $ index ) {
3436 try {
3537 $ view [$ index ] = 1 ;
36- $ this ->fail ();
38+ $ strIndex = strval ($ index );
39+ $ this ->fail ("IndexError not thrown for key: \"{$ strIndex }\"" );
3740 } catch (IndexError $ e ) {
3841 $ this ->assertSame ("Index {$ index } is out of range. " , $ e ->getMessage ());
3942 }
4043 }
4144 }
4245
46+ /**
47+ * @dataProvider dataProviderForBadKeys
48+ */
49+ public function testReadKeyError (array $ source , array $ keys )
50+ {
51+ $ view = ArrayView::toView ($ source );
52+ foreach ($ keys as $ key ) {
53+ $ strKey = is_scalar ($ key ) ? strval ($ key ) : gettype ($ key );
54+ try {
55+ $ _ = $ view [$ key ];
56+ $ this ->fail ("KeyError not thrown for key: \"{$ strKey }\"" );
57+ } catch (KeyError $ e ) {
58+ $ this ->assertSame ("Invalid key: \"{$ strKey }\". " , $ e ->getMessage ());
59+ }
60+ }
61+ }
62+
63+ /**
64+ * @dataProvider dataProviderForBadKeys
65+ */
66+ public function testWriteKeyError (array $ source , array $ keys )
67+ {
68+ $ view = ArrayView::toView ($ source );
69+ foreach ($ keys as $ key ) {
70+ $ strKey = is_scalar ($ key ) ? strval ($ key ) : gettype ($ key );
71+ try {
72+ $ view [$ key ] = 1 ;
73+ $ this ->fail ("KeyError not thrown for key: \"{$ strKey }\"" );
74+ } catch (KeyError $ e ) {
75+ $ this ->assertSame ("Invalid key: \"{$ strKey }\". " , $ e ->getMessage ());
76+ }
77+ }
78+ }
79+
4380 /**
4481 * @dataProvider dataProviderForNonSequentialError
4582 */
@@ -53,9 +90,25 @@ public function testNonSequentialError(callable $arrayGetter)
5390 public function dataProviderForOutOfRangeIndexes (): array
5491 {
5592 return [
56- [[], [-2 , -1 , 0 , 1 ]],
57- [[1 ], [-3 , -2 , 1 , 2 ]],
58- [[1 , 2 , 3 ], [-100 , -5 , 4 , 100 ]],
93+ [[], [-2 , -1 , 0 , 1 , NAN , INF , -INF ]],
94+ [[1 ], [-3 , -2 , 1 , 2 , NAN , INF , -INF ]],
95+ [[1 , 2 , 3 ], [-100 , -5 , 4 , 100 , NAN , INF , -INF ]],
96+ ];
97+ }
98+
99+ public function dataProviderForBadKeys (): array
100+ {
101+ return [
102+ [[], ['a ' , 'b ' , 'c ' ]],
103+ [[], ['1a ' , 'test ' , '! ' ]],
104+ [[], [[], [1 , 2 , 3 ], ['a ' => 'test ' ]], new \stdClass ([])],
105+ [[], [null , true , false , [], [1 , 2 , 3 ], ['a ' => 'test ' ]], new \stdClass ([])],
106+ [[1 ], ['a ' , 'b ' , 'c ' ]],
107+ [[1 ], ['1a ' , 'test ' , '! ' ]],
108+ [[1 ], [null , true , false , [], [1 , 2 , 3 ], ['a ' => 'test ' ]], new \stdClass ([])],
109+ [[1 , 2 , 3 ], ['a ' , 'b ' , 'c ' ]],
110+ [[1 , 2 , 3 ], ['1a ' , 'test ' , '! ' ]],
111+ [[2 ], [null , true , false , [], [1 , 2 , 3 ], ['a ' => 'test ' ]], new \stdClass ([])],
59112 ];
60113 }
61114
0 commit comments