@@ -17,7 +17,7 @@ final class ShellBuilderTest extends TestCase
1717{
1818 public function testBuilderConcept (): void
1919 {
20- $ result = 'a && b | c || d |& f && (g && h) || {i || j;} ' ;
20+ $ result = 'a && b | c || d |& f && (g && h) || { i || j;} ' ;
2121 $ builder = new ShellBuilder ();
2222 $ a = $ builder ->createCommand ('a ' );
2323 $ b = $ builder ->createCommand ('b ' );
@@ -46,7 +46,7 @@ public function testBuilderConcept(): void
4646
4747 public function testBuilderConceptWithShortcut (): void
4848 {
49- $ result = 'a && b | c || d |& f && (g && h) || {i || j;} ' ;
49+ $ result = 'a && b | c || d |& f && (g && h) || { i || j;} ' ;
5050 $ builder = new ShellBuilder ();
5151 $ builder
5252 ->add ('a ' )
@@ -146,7 +146,7 @@ public function testShellBuilderGroupSameShell(): void
146146 ->add (
147147 $ builder ->createCommand ('echo ' )->addArgument ('hello ' )
148148 )->and ('cat ' ));
149- $ this ->assertEquals ("{echo 'hello' && cat;} " , (string )$ builder );
149+ $ this ->assertEquals ("{ echo 'hello' && cat;} " , (string )$ builder );
150150 }
151151
152152 public function testSimpleSshCommand (): void
@@ -669,7 +669,7 @@ public function testCoprocessWithShellGroupAndRedirections(): void
669669 ->addToBuilder ()
670670 ->redirectDescriptor ('' , true , null , 3 );
671671 $ this ->assertEquals (
672- '{coproc tee {tee logfile;} >&3 ;} 3>&1 ' ,
672+ '{ coproc tee { tee logfile;} >&3 ;} 3>&1 ' ,
673673 (string )ShellBuilder::new ()->add ($ builder )->redirectDescriptor ('' , true , 3 , 1 )
674674 );
675675 }
@@ -686,11 +686,60 @@ public function testNamedCoprocessWithShellGroupAndRedirections(): void
686686 ->addToBuilder ())
687687 ->redirectDescriptor ('' , true , null , 3 );
688688 $ this ->assertEquals (
689- '{coproc mycoproc {awk \'{print "foo" $0;fflush()} \';} >&3 ;} 3>&1 ' ,
689+ '{ coproc mycoproc { awk \'{print "foo" $0;fflush()} \';} >&3 ;} 3>&1 ' ,
690690 (string )ShellBuilder::new ()->add ($ builder )->redirectDescriptor ('' , true , 3 , 1 )
691691 );
692692 }
693693
694+ public function testCondiditionalArguments (): void
695+ {
696+ // if false
697+ $ builder = ShellBuilder::new ()
698+ ->if (false , static function (ShellBuilder $ builder ) {
699+ return $ builder ->add ('echo ' );
700+ })
701+ ->ifThis (static function (ShellBuilder $ builder ) {
702+ return $ builder ->hasCommands () === false ;
703+ }, static function (ShellBuilder $ builder ) {
704+ return $ builder ->add ('print ' );
705+ });
706+ static ::assertEquals ('print ' , (string )$ builder );
707+
708+ // if true
709+ $ builder = ShellBuilder::new ()
710+ ->if (true , static function (ShellBuilder $ builder ) {
711+ return $ builder ->add ('echo ' );
712+ })
713+ ->ifThis (static function (ShellBuilder $ builder ) {
714+ return $ builder ->hasCommands () === false ;
715+ }, static function (ShellBuilder $ builder ) {
716+ return $ builder ->add ('print ' );
717+ });
718+ static ::assertEquals ('echo ' , (string )$ builder );
719+ }
720+
721+ public function testComplexCondiditionalArguments (): void
722+ {
723+ $ builder = ShellBuilder::new ()
724+ ->if (
725+ false ,
726+ static function (ShellBuilder $ builder ) {
727+ return $ builder ->add ('echo ' );
728+ },
729+ static function (ShellBuilder $ builder ) {
730+ return $ builder ->add ('awk ' );
731+ }
732+ )
733+ ->ifThis (static function (ShellBuilder $ builder ) {
734+ return $ builder ->hasCommands () === false ;
735+ }, static function (ShellBuilder $ builder ) {
736+ return $ builder ->add ('print ' );
737+ }, static function (ShellBuilder $ builder ) {
738+ return $ builder ->and ('print ' );
739+ });
740+ static ::assertEquals ('awk && print ' , (string )$ builder );
741+ }
742+
694743 public function testSimpleAsyncShellBuilder (): void
695744 {
696745 $ this ->assertEquals (
@@ -775,18 +824,18 @@ public function testCommandVariableWithConditionalAndCommand(): void
775824 public function testShellBuilderIsEmpty (): void
776825 {
777826 $ builder = ShellBuilder::new ();
778- $ this ->assertTrue ($ builder ->hasCommands ());
827+ $ this ->assertFalse ($ builder ->hasCommands ());
779828 }
780829
781830 public function testShellBuilderIsNotEmpty (): void
782831 {
783832 $ builder = ShellBuilder::new ();
784833 $ builder ->addVariable ('a ' , 'b ' );
785- $ this ->assertFalse ($ builder ->hasCommands ());
786- $ builder ->removeVariable ('a ' );
787834 $ this ->assertTrue ($ builder ->hasCommands ());
788- $ builder ->add ( ' echo ' );
835+ $ builder ->removeVariable ( ' a ' );
789836 $ this ->assertFalse ($ builder ->hasCommands ());
837+ $ builder ->add ('echo ' );
838+ $ this ->assertTrue ($ builder ->hasCommands ());
790839 }
791840
792841 public function testAddVariableWithoutSemicolon (): void
0 commit comments