44use Gt \FileCache \Cache ;
55use Gt \FileCache \FileAccess ;
66use PHPUnit \Framework \TestCase ;
7+ use SplFileInfo ;
8+ use SplFixedArray ;
79use stdClass ;
10+ use TypeError ;
811
912class CacheTest extends TestCase {
1013 public function tearDown ():void {
@@ -97,6 +100,18 @@ public function testGetInstance():void {
97100 self ::assertSame ($ value ->name , $ class ->name );
98101 }
99102
103+ public function testGetInstance_error ():void {
104+ $ value = new StdClass ();
105+ $ value ->name = uniqid ();
106+
107+ $ sut = $ this ->getSut ([
108+ "test " => $ value ,
109+ ]);
110+ self ::expectException (TypeError::class);
111+ self ::expectExceptionMessage ("Value is not an instance of SplFileInfo " );
112+ $ sut ->getInstance ("test " , SplFileInfo::class, fn () => false );
113+ }
114+
100115 public function testGetArray ():void {
101116 $ value = [1 , 2 , 3 ];
102117 $ sut = $ this ->getSut ([
@@ -110,11 +125,104 @@ public function testGetArray_notArray():void {
110125 $ sut = $ this ->getSut ([
111126 "numbers " => $ value ,
112127 ]);
113- self ::expectException (\ TypeError::class);
114- self ::expectExceptionMessage ("Value with key 'numbers' is not an array " );
128+ self ::expectException (TypeError::class);
129+ self ::expectExceptionMessage ("Data 'numbers' is not an array " );
115130 $ sut ->getArray ("numbers " , fn () => []);
116131 }
117132
133+ public function testGetTypedArray_notArray ():void {
134+ $ value = (object )[1 , 2 , 3 ];
135+ $ sut = $ this ->getSut ([
136+ "numbers " => $ value ,
137+ ]);
138+ self ::expectException (TypeError::class);
139+ self ::expectExceptionMessage ("Data 'numbers' is not an array " );
140+ $ sut ->getTypedArray ("numbers " , "int " , fn () => []);
141+ }
142+
143+ public function testGetTypedArray_int ():void {
144+ $ value = [1 , "2 " , 3.000 ];
145+ $ sut = $ this ->getSut ([
146+ "numbers " => $ value ,
147+ ]);
148+ $ typedArray = $ sut ->getTypedArray ("numbers " , "int " , fn () => []);
149+ foreach ($ typedArray as $ value ) {
150+ self ::assertIsInt ($ value );
151+ }
152+ }
153+
154+ public function testGetTypedArray_intFailure ():void {
155+ $ value = [1 , "2 " , 3.000 , "four " ];
156+ $ sut = $ this ->getSut ([
157+ "numbers " => $ value ,
158+ ]);
159+ self ::expectException (TypeError::class);
160+ $ sut ->getTypedArray ("numbers " , "int " , fn () => []);
161+ }
162+
163+ public function testGetTypedArray_float ():void {
164+ $ value = [1 , "2 " , 3.000 ];
165+ $ sut = $ this ->getSut ([
166+ "numbers " => $ value ,
167+ ]);
168+ $ typedArray = $ sut ->getTypedArray ("numbers " , "float " , fn () => []);
169+ foreach ($ typedArray as $ value ) {
170+ self ::assertIsFloat ($ value );
171+ }
172+ }
173+
174+ public function testGetTypedArray_floatFailure ():void {
175+ $ value = [1 , "2 " , 3.000 , "four " ];
176+ $ sut = $ this ->getSut ([
177+ "numbers " => $ value ,
178+ ]);
179+ self ::expectException (TypeError::class);
180+ $ sut ->getTypedArray ("numbers " , "float " , fn () => []);
181+ }
182+
183+ public function testGetTypedArray_string ():void {
184+ $ value = [1 , "2 " , 3.000 , "four " ];
185+ $ sut = $ this ->getSut ([
186+ "numbers " => $ value ,
187+ ]);
188+ $ typedArray = $ sut ->getTypedArray ("numbers " , "string " , fn () => []);
189+ foreach ($ typedArray as $ value ) {
190+ self ::assertIsString ($ value );
191+ }
192+ }
193+
194+ public function testGetTypedArray_bool ():void {
195+ $ value = [0 , "1 " , false , true , [], new StdClass ()];
196+ $ sut = $ this ->getSut ([
197+ "booleans " => $ value ,
198+ ]);
199+ $ typedArray = $ sut ->getTypedArray ("booleans " , "bool " , fn () => []);
200+ foreach ($ typedArray as $ i => $ value ) {
201+ self ::assertSame ((bool )($ i % 2 ), $ value , $ i );
202+ }
203+ }
204+
205+ public function testGetTypedArray_class ():void {
206+ $ value = [new SplFileInfo (__FILE__ ), new SplFileInfo (__DIR__ )];
207+ $ sut = $ this ->getSut ([
208+ "files " => $ value ,
209+ ]);
210+ $ typedArray = $ sut ->getTypedArray ("files " , SplFileInfo::class, fn () => []);
211+ foreach ($ typedArray as $ value ) {
212+ self ::assertInstanceOf (SplFileInfo::class, $ value );
213+ }
214+ }
215+
216+ public function testGetTypedArray_classError ():void {
217+ $ value = [new SplFileInfo (__FILE__ ), new SplFixedArray (), new SplFileInfo (__DIR__ )];
218+ $ sut = $ this ->getSut ([
219+ "files " => $ value ,
220+ ]);
221+ self ::expectExceptionMessage ("Array value at key '1' is not an instance of SplFileInfo " );
222+ self ::expectException (TypeError::class);
223+ $ sut ->getTypedArray ("files " , SplFileInfo::class, fn () => []);
224+ }
225+
118226 private function getSut (array $ mockFiles = []):Cache {
119227 $ mockFileAccess = null ;
120228 if (!empty ($ mockFiles )) {
0 commit comments