55namespace Asm \Ansible \Command ;
66
77use Asm \Ansible \Process \ProcessBuilder ;
8+ use Asm \Ansible \Process \ProcessBuilderInterface ;
89use Asm \Ansible \Testing \AnsibleTestCase ;
910use Asm \Ansible \Utils \Env ;
1011use DateTime ;
@@ -801,21 +802,14 @@ public function testExtraVars(): void
801802 //$playbookFile = $this->getSamplesPathFor(AnsiblePlaybook::class) . '/playbook1.yml';
802803
803804 $ tests = [
804- // Test empty strings (with & without spaces).
805805 [
806806 'input ' => '' ,
807807 'expect ' => false ,
808808 ],
809- [
810- 'input ' => ' ' ,
811- 'expect ' => false ,
812- ],
813- // Test empty array.
814809 [
815810 'input ' => [],
816811 'expect ' => false ,
817812 ],
818- // Test Arrays
819813 [
820814 'input ' => ['key ' => 'value ' ],
821815 'expect ' => '--extra-vars=key=value ' ,
@@ -824,12 +818,6 @@ public function testExtraVars(): void
824818 'input ' => ['key1 ' => 'value1 ' , 'key2 ' => 'value2 ' ],
825819 'expect ' => '--extra-vars=key1=value1 key2=value2 ' ,
826820 ],
827- // Test valid JSON.
828- [
829- 'input ' => '{ "key1": "value1", "key2": "value2" } ' ,
830- 'expect ' => '--extra-vars={ "key1": "value1", "key2": "value2" } ' ,
831- ],
832- // Test key value string.
833821 [
834822 'input ' => 'key=value ' ,
835823 'expect ' => '--extra-vars=key=value ' ,
@@ -1037,4 +1025,96 @@ public function testSshPipelining(): void
10371025 $ this ->assertEquals ($ expect , $ env ['ANSIBLE_SSH_PIPELINING ' ]);
10381026 }
10391027 }
1028+
1029+ public function testReturnsErrorOutputIfProcessWasNotSuccessful (): void
1030+ {
1031+ $ builder = $ this ->createMock (ProcessBuilderInterface::class);
1032+ $ builder
1033+ ->expects (self ::once ())
1034+ ->method ('setArguments ' )
1035+ ->willReturnSelf ();
1036+ $ builder
1037+ ->expects (self ::once ())
1038+ ->method ('getProcess ' )
1039+ ->willReturn ($ process = $ this ->createMock (Process::class));
1040+ $ process
1041+ ->expects (self ::once ())
1042+ ->method ('run ' );
1043+ $ process
1044+ ->expects (self ::once ())
1045+ ->method ('isSuccessful ' )
1046+ ->willReturn (false );
1047+ $ process
1048+ ->expects (self ::once ())
1049+ ->method ('getErrorOutput ' )
1050+ ->willReturn ('error output ' );
1051+ $ process
1052+ ->expects (self ::never ())
1053+ ->method ('getOutput ' );
1054+
1055+ $ playbook = new AnsiblePlaybook ($ builder );
1056+
1057+ self ::assertEquals ('error output ' , $ playbook ->execute ());
1058+ }
1059+
1060+ public function testReturnsNormalOutputIfProcessWasSuccessful (): void
1061+ {
1062+ $ builder = $ this ->createMock (ProcessBuilderInterface::class);
1063+ $ builder
1064+ ->expects (self ::once ())
1065+ ->method ('setArguments ' )
1066+ ->willReturnSelf ();
1067+ $ builder
1068+ ->expects (self ::once ())
1069+ ->method ('getProcess ' )
1070+ ->willReturn ($ process = $ this ->createMock (Process::class));
1071+ $ process
1072+ ->expects (self ::once ())
1073+ ->method ('run ' );
1074+ $ process
1075+ ->expects (self ::once ())
1076+ ->method ('isSuccessful ' )
1077+ ->willReturn (true );
1078+ $ process
1079+ ->expects (self ::once ())
1080+ ->method ('getOutput ' )
1081+ ->willReturn ('success ' );
1082+ $ process
1083+ ->expects (self ::never ())
1084+ ->method ('getErrorOutput ' );
1085+
1086+ $ playbook = new AnsiblePlaybook ($ builder );
1087+
1088+ self ::assertEquals ('success ' , $ playbook ->execute ());
1089+ }
1090+
1091+ public function testReturnsExitCodeIfCallbackwasPassed (): void
1092+ {
1093+ $ builder = $ this ->createMock (ProcessBuilderInterface::class);
1094+ $ builder
1095+ ->expects (self ::once ())
1096+ ->method ('setArguments ' )
1097+ ->willReturnSelf ();
1098+ $ builder
1099+ ->expects (self ::once ())
1100+ ->method ('getProcess ' )
1101+ ->willReturn ($ process = $ this ->createMock (Process::class));
1102+ $ process
1103+ ->expects (self ::once ())
1104+ ->method ('run ' )
1105+ ->willReturn (0 );
1106+ $ process
1107+ ->expects (self ::never ())
1108+ ->method ('isSuccessful ' );
1109+ $ process
1110+ ->expects (self ::never ())
1111+ ->method ('getOutput ' );
1112+ $ process
1113+ ->expects (self ::never ())
1114+ ->method ('getErrorOutput ' );
1115+
1116+ $ playbook = new AnsiblePlaybook ($ builder );
1117+
1118+ self ::assertEquals (0 , $ playbook ->execute (fn () => null ));
1119+ }
10401120}
0 commit comments