@@ -84,33 +84,32 @@ public function testWithRealpathRoot(): void {
8484 ->nsdir (__DIR__ . '/../src ' , __NAMESPACE__ )
8585 ->withRealpathRoot (),
8686 );
87+ $ this ->callAndAssertException (
88+ \RuntimeException::class,
89+ fn () => $ this
90+ ->nsdir (__DIR__ . '/non/existing/path ' , 'Acme\Foo ' )
91+ ->withRealpathRoot (),
92+ );
8793 }
8894
8995 public function testFindNamespace (): void {
90- // It does not work from a child namespace towards a top namespace.
91- $ this ->assertNull (
92- $ this
93- ->nsdirFromClass (PlantInterface::class)
94- ->findNamespace (__NAMESPACE__ ),
95- );
96-
97- // It does work from a parent namespace to a child namespace.
96+ $ nsdir = $ this ->nsdir ('path/to/package/src/Animal/Mammal ' , 'Acme\Zoo\Animal\Mammal ' );
97+ $ this ->assertSame ($ nsdir , $ nsdir ->findNamespace ('Acme\Zoo\Animal\Mammal ' ));
98+ // Find child namespace.
9899 $ this ->assertNamespaceDir (
99- __DIR__ . '/Fixtures/Acme/Plant ' ,
100- __NAMESPACE__ . '\\Fixtures \\Acme \\Plant ' ,
101- $ this
102- ->nsdirFromClass (self ::class)
103- ->findNamespace (__NAMESPACE__ . '\\Fixtures \\Acme \\Plant ' ) ?? $ this ->fail (),
100+ 'path/to/package/src/Animal/Mammal/Whale ' ,
101+ $ namespace = 'Acme\Zoo\Animal\Mammal\Whale ' ,
102+ $ nsdir ->findNamespace ($ namespace ) ?? $ this ->fail (),
104103 );
105-
106- // It also works for non-existing directory.
107104 $ this ->assertNamespaceDir (
108- __DIR__ . '/NonExisting ' ,
109- __NAMESPACE__ . '\\NonExisting ' ,
110- $ this
111- ->nsdirFromClass (self ::class)
112- ->findNamespace (__NAMESPACE__ . '\\NonExisting ' ) ?? $ this ->fail (),
105+ 'path/to/package/src/Animal/Mammal/Whale/Dolphin ' ,
106+ $ namespace = 'Acme\Zoo\Animal\Mammal\Whale\Dolphin ' ,
107+ $ nsdir ->findNamespace ($ namespace ) ?? $ this ->fail (),
113108 );
109+ // Cannot find parent namespace.
110+ $ this ->assertNull ($ nsdir ->findNamespace ('Acme\Zoo\Animal ' ));
111+ // Cannot find sibling namespace.
112+ $ this ->assertNull ($ nsdir ->findNamespace ('Acme\Zoo\Animal\Insect ' ));
114113 }
115114
116115 public function testBasedir (): void {
@@ -212,28 +211,63 @@ public function testRequireParent(): void {
212211 }
213212
214213 public function testParentN (): void {
215- $ nsdir = $ this ->nsdirFromClass (PlantInterface::class);
214+ $ nsdir = $ this ->nsdir (
215+ 'path/to/package/src/Animal/Mammal ' ,
216+ 'Acme\Zoo\Animal\Mammal ' ,
217+ );
218+ $ this ->assertSame ($ nsdir , $ nsdir ->parentN (0 ));
216219 $ this ->assertNamespaceDir (
217- $ nsdir -> getDirectory () ,
218- $ nsdir -> getNamespace () ,
219- $ nsdir ->parentN (0 ) ?? $ this ->fail (' ->parentN(0) is NULL. ' ),
220+ ' path/to/package/src/Animal ' ,
221+ ' Acme\Zoo\Animal ' ,
222+ $ nsdir ->parentN (1 ) ?? $ this ->fail (),
220223 );
221224 $ this ->assertNamespaceDir (
222- __DIR__ ,
223- __NAMESPACE__ ,
224- $ nsdir ->parentN (3 ) ?? $ this ->fail (' ->parentN(3) is NULL. ' ),
225+ ' path/to/package/src ' ,
226+ ' Acme\Zoo ' ,
227+ $ nsdir ->parentN (2 ) ?? $ this ->fail (),
225228 );
229+ $ this ->assertNull ($ nsdir ->parentN (3 ));
226230 $ this ->assertNull ($ nsdir ->parentN (4 ));
231+ $ this ->assertNull ($ nsdir ->parentN (-1 ));
232+ $ this ->assertNamespaceDir (
233+ 'path/to/package/src ' ,
234+ 'Acme\Zoo ' ,
235+ $ nsdir ->parentN (-2 ) ?? $ this ->fail (),
236+ );
237+ $ this ->assertNamespaceDir (
238+ 'path/to/package/src/Animal ' ,
239+ 'Acme\Zoo\Animal ' ,
240+ $ nsdir ->parentN (-3 ) ?? $ this ->fail (),
241+ );
242+ $ this ->assertSame ($ nsdir , $ nsdir ->parentN (-4 ));
243+ $ this ->assertNull ($ nsdir ->parentN (-5 ));
244+ $ this ->assertNull ($ nsdir ->parentN (-6 ));
227245 }
228246
229247 public function testParent (): void {
230- $ nsdir = $ this ->nsdirFromClass (PlantInterface::class );
248+ $ f = fn ( string $ dir , string $ namespace ) => $ this ->nsdir ( $ dir , $ namespace )-> parent ( );
231249 $ this ->assertNamespaceDir (
232- __DIR__ . '/Fixtures/Acme ' ,
233- __NAMESPACE__ . '\\Fixtures \\Acme ' ,
234- $ nsdir ->parent () ?? $ this ->fail ('->parent() is NULL. ' ),
250+ 'path/to/Animal ' ,
251+ 'Acme\Zoo\Animal ' ,
252+ $ f (
253+ 'path/to/Animal/Mammal ' ,
254+ 'Acme\Zoo\Animal\Mammal ' ,
255+ ) ?? $ this ->fail (),
256+ );
257+ $ this ->assertNull ($ f ('path/to/package/src ' , 'Acme\Zoo ' ));
258+ $ this ->assertNull ($ f ('path/to/package ' , '' ));
259+ $ this ->assertNull ($ f ('' , 'Acme ' ));
260+ $ this ->assertNull ($ f ('path-without-slash ' , 'Acme\Zoo ' ));
261+ $ this ->assertNamespaceDir (
262+ '' ,
263+ 'Acme ' ,
264+ $ f ('Zoo ' , 'Acme\Zoo ' ) ?? $ this ->fail (),
265+ );
266+ $ this ->assertNamespaceDir (
267+ 'path/to/package ' ,
268+ '' ,
269+ $ f ('path/to/package/Acme ' , 'Acme ' ) ?? $ this ->fail (),
235270 );
236- $ this ->assertNull ($ this ->nsdirFromClass (self ::class)->parent ());
237271 }
238272
239273 public function testSubdir (): void {
@@ -257,34 +291,24 @@ public function testGetNamespace(): void {
257291 }
258292
259293 public function testGetShortname (): void {
260- $ this ->assertSame (
261- 'Tests ' ,
262- $ this ->nsdirFromClass (self ::class)->getShortname (),
263- );
264- $ this ->assertNull (
265- $ this ->nsdir ('/path/to/root/nsp ' , '' )->getTerminatedShortname (),
266- );
294+ $ f = fn (string $ namespace ) => $ this ->nsdir ('path/to ' , $ namespace )->getShortname ();
295+ $ this ->assertSame ('Animal ' , $ f ('Acme\Zoo\Animal ' ));
296+ $ this ->assertSame ('Acme ' ,$ f ('Acme ' ));
297+ $ this ->assertNull ($ f ('' ));
267298 }
268299
269300 public function testGetTerminatedShortname (): void {
270- $ this ->assertSame (
271- 'Tests \\' ,
272- $ this ->nsdirFromClass (self ::class)->getTerminatedShortname (),
273- );
274- $ this ->assertNull (
275- $ this ->nsdir ('/path/to/root/nsp ' , '' )->getTerminatedShortname (),
276- );
301+ $ f = fn (string $ namespace ) => $ this ->nsdir ('path/to ' , $ namespace )->getTerminatedShortname ();
302+ $ this ->assertSame ('Animal \\' , $ f ('Acme\Zoo\Animal ' ));
303+ $ this ->assertSame ('Acme \\' , $ f ('Acme ' ));
304+ $ this ->assertNull ($ f ('' ));
277305 }
278306
279307 public function testGetShortFqn (): void {
280- $ this ->assertSame (
281- '\\Tests ' ,
282- $ this ->nsdirFromClass (self ::class)->getShortFqn (),
283- );
284- $ this ->assertSame (
285- '' ,
286- $ this ->nsdir ('/path/to/root/nsp ' , '' )->getShortFqn (),
287- );
308+ $ f = fn (string $ namespace ) => $ this ->nsdir ('path/to ' , $ namespace )->getShortFqn ();
309+ $ this ->assertSame ('\\Animal ' , $ f ('Acme\Zoo\Animal ' ));
310+ $ this ->assertSame ('\\Acme ' , $ f ('Acme ' ));
311+ $ this ->assertSame ('' , $ f ('' ));
288312 }
289313
290314 public function testGetTerminatedNamespace (): void {
@@ -334,24 +358,40 @@ public function testGetPackageDirectory(): void {
334358 }
335359
336360 public function testGetRelativeTerminatedNamespace (): void {
337- $ nsdir = $ this -> nsdirFromClass (PlantInterface::class);
338- $ this ->assertSame ( ' Fixtures \\ Acme \\ Plant \\ ' , $ nsdir ->getRelativeTerminatedNamespace (3 ) );
339- $ this -> assertSame ( ' Tests \\ Fixtures \\ Acme \\ Plant \\ ' , $ nsdir -> getRelativeTerminatedNamespace () );
340- $ this ->assertSame ('Acme \\ Plant \\ ' , $ nsdir -> getRelativeTerminatedNamespace ( 4 ));
341- // It does not work with level: 2.
342- $ this ->expectException (\RuntimeException::class );
343- $ nsdir -> getPackageDirectory ( );
344- $ this ->assertSame (
345- __NAMESPACE__ . '\\' ,
346- $ this ->nsdirFromClass ( self ::class)-> getTerminatedNamespace (),
347- );
348- $ this ->assertSame (
349- '' ,
350- $ this ->nsdir ( ' /path/to/root/nsp ' , '' )-> getTerminatedNamespace (),
351- );
361+ // @phpstan-ignore argument.type
362+ $ f = fn ( string $ namespace , int ... $ args ) => $ this ->nsdir ( ' path/to ' , $ namespace ) ->getRelativeTerminatedNamespace (... $ args );
363+ $ fn = fn ( int ... $ args ) => $ f ( ' Acme\Zoo\Animal\Mammal ' , ... $ args );
364+ $ this ->assertSame ('Animal\Mammal \\ ' , $ fn ( ));
365+ $ this -> assertSame ( ' Acme\Zoo\Animal\Mammal \\' , $ fn ( 0 ));
366+ $ this ->assertSame ( ' Zoo\Animal\Mammal \\' , $ fn ( 1 ) );
367+ $ this -> assertSame ( ' Animal\Mammal \\' , $ fn ( 2 ) );
368+ $ this ->assertSame (' Mammal \\' , $ fn ( 3 ));
369+ $ this -> assertSame ( '' , $ fn ( 4 ));
370+ $ this ->callAndAssertException (\RuntimeException ::class, fn () => $ fn ( 5 ));
371+ $ this -> assertSame ( '' , $ f ( '' , 0 ) );
372+ $ this ->callAndAssertException (\RuntimeException::class, fn () => $ f ( '' , 1 ));
373+ $ this -> assertSame ( ' Acme \\' , $ f ( ' Acme ' , 0 ));
374+ $ this ->assertSame ( ' ' , $ f ( ' Acme ' , 1 ));
375+ $ this -> callAndAssertException (\RuntimeException::class, fn () => $ f ( ' Acme ' , 2 ) );
352376 }
353377
354378 public function testGetRelativeFqn (): void {
379+ $ f = fn (string $ namespace , int ...$ args ) => $ this ->nsdir ('path/to ' , $ namespace )->getRelativeFqn (...$ args );
380+ $ fn = fn (int ...$ args ) => $ f ('Acme\Zoo\Animal\Mammal ' , ...$ args );
381+ $ this ->assertSame ('\\Animal\Mammal ' , $ fn ());
382+ $ this ->assertSame ('\\Acme\Zoo\Animal\Mammal ' , $ fn (0 ));
383+ $ this ->assertSame ('\\Zoo\Animal\Mammal ' , $ fn (1 ));
384+ $ this ->assertSame ('\\Animal\Mammal ' , $ fn (2 ));
385+ $ this ->assertSame ('\\Mammal ' , $ fn (3 ));
386+ $ this ->assertSame ('' , $ fn (4 ));
387+ $ this ->callAndAssertException (\RuntimeException::class, fn () => $ fn (5 ));
388+ $ this ->assertSame ('' , $ f ('' , 0 ));
389+ $ this ->callAndAssertException (\RuntimeException::class, fn () => $ f ('' , 1 ));
390+ $ this ->assertSame ('\\Acme ' , $ f ('Acme ' , 0 ));
391+ $ this ->assertSame ('' , $ f ('Acme ' , 1 ));
392+ $ this ->callAndAssertException (\RuntimeException::class, fn () => $ f ('Acme ' , 2 ));
393+
394+
355395 $ nsdir = $ this ->nsdirFromClass (PlantInterface::class);
356396 $ this ->assertSame ('\\Fixtures \\Acme \\Plant ' , $ nsdir ->getRelativeFqn (3 ));
357397 $ this ->assertSame ('\\Tests \\Fixtures \\Acme \\Plant ' , $ nsdir ->getRelativeFqn ());
@@ -388,6 +428,8 @@ public function testGetIterator(): void {
388428 ],
389429 iterator_to_array ($ nsdir ->getIterator ()),
390430 );
431+ $ bad_nsdir = $ this ->nsdir (__DIR__ . '/non/existing/subdir ' , 'Acme\Missing ' );
432+ $ this ->callAndAssertException (\RuntimeException::class, fn () => $ bad_nsdir ->getIterator ()->valid ());
391433 }
392434
393435 public function testGetElements (): void {
@@ -457,7 +499,10 @@ protected function callAndAssertException(string $exception_class, callable $cal
457499 $ this ->fail ("Expected exception was not thrown. " );
458500 }
459501 catch (\Throwable $ e ) {
460- $ this ->assertSame ($ exception_class , get_class ($ e ));
502+ if (get_class ($ e ) !== $ exception_class ) {
503+ throw $ e ;
504+ }
505+ $ this ->addToAssertionCount (1 );
461506 }
462507 }
463508
0 commit comments