22
33namespace Selective \Artifact \Test ;
44
5+ use InvalidArgumentException ;
56use PHPUnit \Framework \TestCase ;
67use Selective \ArrayReader \ArrayReader ;
78
@@ -25,6 +26,7 @@ class ArrayReaderTest extends TestCase
2526 public function testGetString ($ data , string $ key , $ default , $ expected )
2627 {
2728 $ reader = new ArrayReader ($ data );
29+ static ::assertSame ($ expected , $ reader ->findString ($ key , $ default ));
2830 static ::assertSame ($ expected , $ reader ->getString ($ key , $ default ));
2931 }
3032
@@ -37,6 +39,77 @@ public function providerGetString(): array
3739 {
3840 return [
3941 [['key ' => 'value ' ], 'key ' , null , 'value ' ],
42+ [['key ' => null ], 'key ' , 'value ' , 'value ' ],
43+ [['key ' => 'value ' ], 'nope ' , 'default ' , 'default ' ],
44+ [['key ' => ['key2 ' => 'value ' ]], 'key.key2 ' , null , 'value ' ],
45+ [['key ' => ['key2 ' => 'value ' ]], 'key.nope ' , 'default ' , 'default ' ],
46+ ];
47+ }
48+
49+ /**
50+ * Test.
51+ *
52+ * @dataProvider providerGetStringError
53+ *
54+ * @param mixed $data The data
55+ * @param string $key The lookup key
56+ *
57+ * @return void
58+ */
59+ public function testGetStringError ($ data , string $ key )
60+ {
61+ $ this ->expectException (InvalidArgumentException::class);
62+
63+ $ reader = new ArrayReader ($ data );
64+ $ reader ->getString ($ key );
65+
66+ static ::assertTrue (true );
67+ }
68+
69+ /**
70+ * Provider.
71+ *
72+ * @return array[] The test data
73+ */
74+ public function providerGetStringError (): array
75+ {
76+ return [
77+ [['key ' => 'value ' ], 'nope ' ],
78+ [['key ' => null ], 'nope ' ],
79+ [['key ' => ['key2 ' => 'value ' ]], 'key.nope ' ],
80+ [['key ' => ['key2 ' => null ]], 'key.key2 ' ],
81+ ];
82+ }
83+
84+ /**
85+ * Test.
86+ *
87+ * @dataProvider providerFindString
88+ *
89+ * @param mixed $data The data
90+ * @param string $key The lookup key
91+ * @param mixed $default The default value
92+ * @param mixed $expected The expected value
93+ *
94+ * @return void
95+ */
96+ public function testFindString ($ data , string $ key , $ default , $ expected )
97+ {
98+ $ reader = new ArrayReader ($ data );
99+ static ::assertSame ($ expected , $ reader ->findString ($ key , $ default ));
100+ }
101+
102+ /**
103+ * Provider.
104+ *
105+ * @return array[] The test data
106+ */
107+ public function providerFindString (): array
108+ {
109+ return [
110+ [['key ' => 'value ' ], 'key ' , null , 'value ' ],
111+ [['key ' => null ], 'key ' , 'value ' , 'value ' ],
112+ [['key ' => null ], 'key ' , null , null ],
40113 [['key ' => 'value ' ], 'nope ' , 'default ' , 'default ' ],
41114 [['key ' => ['key2 ' => 'value ' ]], 'key.key2 ' , null , 'value ' ],
42115 [['key ' => ['key2 ' => 'value ' ]], 'key.nope ' , 'default ' , 'default ' ],
0 commit comments