@@ -862,63 +862,76 @@ public function testErrorHandlerChaining(): void
862862 public function testOptionalParamsWithNullUseDefault (): void
863863 {
864864 // Test when optional param receives null, it should use default value
865- // But empty string should be preserved as-is
866- $ route = new Route ('GET ' , '/path ' );
865+ // Empty strings are treated as valid values and preserved as-is
866+ // Note: Using Text validator with min=0 to allow empty strings
867+ $ route = new Route ('GET ' , '/test-null ' );
867868 $ route
868- ->param ('x ' , 'x-default ' , new Text (200 ), 'x param ' , true )
869- ->param ('y ' , 'y-default ' , new Text (200 ), 'y param ' , true )
870- ->action (function ($ x , $ y ) {
871- echo $ x . '- ' . $ y ;
869+ ->param ('param1 ' , 'default1 ' , new Text (200 , min: 0 ), 'param1 desc ' , true )
870+ ->param ('param2 ' , 'default2 ' , new Text (200 , min: 0 ), 'param2 desc ' , true )
871+ ->action (function ($ param1 , $ param2 ) {
872+ echo $ param1 . '| ' . $ param2 ;
872873 });
873874
874875 // Test with null value - should use default
875876 \ob_start ();
876877 $ request = new UtopiaRequestTest ();
877- $ request ::_setParams (['x ' => null , 'y ' => 'y-value ' ]);
878+ $ request ::_setParams (['param1 ' => null , 'param2 ' => 'value2 ' ]);
879+ $ this ->app ->execute ($ route , $ request , new Response ());
880+ $ result = \ob_get_contents ();
881+ \ob_end_clean ();
882+
883+ $ this ->assertEquals ('default1|value2 ' , $ result );
884+
885+ // Test with both params having valid values
886+ \ob_start ();
887+ $ request = new UtopiaRequestTest ();
888+ $ request ::_setParams (['param1 ' => 'value1 ' , 'param2 ' => 'value2 ' ]);
878889 $ this ->app ->execute ($ route , $ request , new Response ());
879890 $ result = \ob_get_contents ();
880891 \ob_end_clean ();
881892
882- $ this ->assertEquals ('x-default-y-value ' , $ result );
893+ $ this ->assertEquals ('value1|value2 ' , $ result );
883894
884- // Test with empty string value - should keep empty string
895+ // Test with both params as null - both should use defaults
885896 \ob_start ();
886897 $ request = new UtopiaRequestTest ();
887- $ request ::_setParams (['x ' => '' , 'y ' => ' y-value ' ]);
898+ $ request ::_setParams (['param1 ' => null , 'param2 ' => null ]);
888899 $ this ->app ->execute ($ route , $ request , new Response ());
889900 $ result = \ob_get_contents ();
890901 \ob_end_clean ();
891902
892- $ this ->assertEquals ('-y-value ' , $ result );
903+ $ this ->assertEquals ('default1|default2 ' , $ result );
893904
894- // Test with null in one param and empty string in another
905+ // Test with empty string - should be preserved as valid value
895906 \ob_start ();
896907 $ request = new UtopiaRequestTest ();
897- $ request ::_setParams (['x ' => null , 'y ' => '' ]);
908+ $ request ::_setParams (['param1 ' => '' , 'param2 ' => 'value2 ' ]);
898909 $ this ->app ->execute ($ route , $ request , new Response ());
899910 $ result = \ob_get_contents ();
900911 \ob_end_clean ();
901912
902- $ this ->assertEquals ('x-default- ' , $ result );
913+ // Empty string is a valid value, not replaced with default
914+ $ this ->assertEquals ('|value2 ' , $ result );
903915
904- // Test that valid values are still used
916+ // Test with both empty string and null
905917 \ob_start ();
906918 $ request = new UtopiaRequestTest ();
907- $ request ::_setParams (['x ' => ' x-value ' , 'y ' => 'y-value ' ]);
919+ $ request ::_setParams (['param1 ' => null , 'param2 ' => '' ]);
908920 $ this ->app ->execute ($ route , $ request , new Response ());
909921 $ result = \ob_get_contents ();
910922 \ob_end_clean ();
911923
912- $ this ->assertEquals ('x-value-y-value ' , $ result );
924+ // null uses default, empty string is preserved
925+ $ this ->assertEquals ('default1| ' , $ result );
913926
914- // Test that numeric zero is still treated as a valid value (not null)
927+ // Test with numeric zero - should be treated as valid value (not null)
915928 \ob_start ();
916929 $ request = new UtopiaRequestTest ();
917- $ request ::_setParams (['x ' => '0 ' , 'y ' => 'y-value ' ]);
930+ $ request ::_setParams (['param1 ' => '0 ' , 'param2 ' => 'value2 ' ]);
918931 $ this ->app ->execute ($ route , $ request , new Response ());
919932 $ result = \ob_get_contents ();
920933 \ob_end_clean ();
921934
922- $ this ->assertEquals ('0-y-value ' , $ result );
935+ $ this ->assertEquals ('0|value2 ' , $ result );
923936 }
924937}
0 commit comments