Skip to content

Commit baf4147

Browse files
Add tests to dispatchShell
1 parent bc35e7b commit baf4147

File tree

2 files changed

+141
-21
lines changed

2 files changed

+141
-21
lines changed

tests/TestCase/Console/ShellTest.php

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,78 @@ public function testDispatchShellArgsParser()
689689
$this->assertEquals($expected, $result);
690690
}
691691

692+
/**
693+
* test calling a shell that dispatch another one
694+
*
695+
* @return void
696+
*/
697+
public function testDispatchShell()
698+
{
699+
$Shell = new TestingDispatchShell();
700+
ob_start();
701+
$Shell->runCommand(['test_task'], true);
702+
$result = ob_get_clean();
703+
704+
$expected = <<<TEXT
705+
<info>Welcome to CakePHP Console</info>
706+
I am a test task, I dispatch another Shell
707+
I am a dispatched Shell
708+
709+
TEXT;
710+
$this->assertEquals($expected, $result);
711+
712+
ob_start();
713+
$Shell->runCommand(['test_task_dispatch_array'], true);
714+
$result = ob_get_clean();
715+
$this->assertEquals($expected, $result);
716+
717+
ob_start();
718+
$Shell->runCommand(['test_task_dispatch_command_string'], true);
719+
$result = ob_get_clean();
720+
$this->assertEquals($expected, $result);
721+
722+
ob_start();
723+
$Shell->runCommand(['test_task_dispatch_command_array'], true);
724+
$result = ob_get_clean();
725+
$this->assertEquals($expected, $result);
726+
727+
$expected = <<<TEXT
728+
<info>Welcome to CakePHP Console</info>
729+
I am a test task, I dispatch another Shell
730+
I am a dispatched Shell. My param `foo` has the value `bar`
731+
732+
TEXT;
733+
734+
ob_start();
735+
$Shell->runCommand(['test_task_dispatch_with_param'], true);
736+
$result = ob_get_clean();
737+
$this->assertEquals($expected, $result);
738+
739+
$expected = <<<TEXT
740+
<info>Welcome to CakePHP Console</info>
741+
I am a test task, I dispatch another Shell
742+
I am a dispatched Shell. My param `foo` has the value `bar`
743+
My param `fooz` has the value `baz`
744+
745+
TEXT;
746+
ob_start();
747+
$Shell->runCommand(['test_task_dispatch_with_multiple_params'], true);
748+
$result = ob_get_clean();
749+
$this->assertEquals($expected, $result);
750+
751+
$expected = <<<TEXT
752+
<info>Welcome to CakePHP Console</info>
753+
I am a test task, I dispatch another Shell
754+
<info>Welcome to CakePHP Console</info>
755+
I am a dispatched Shell
756+
757+
TEXT;
758+
ob_start();
759+
$Shell->runCommand(['test_task_dispatch_with_requested_off'], true);
760+
$result = ob_get_clean();
761+
$this->assertEquals($expected, $result);
762+
}
763+
692764
/**
693765
* Test that runCommand() doesn't call public methods when the second arg is false.
694766
*
@@ -897,27 +969,6 @@ public function testRunCommandHittingTaskInSubcommand()
897969
$shell->runCommand(['slice', 'one']);
898970
}
899971

900-
/**
901-
* test calling a shell that dispatch another one
902-
*
903-
* @return void
904-
*/
905-
public function testDispatchShell()
906-
{
907-
$Shell = new TestingDispatchShell();
908-
ob_start();
909-
$Shell->runCommand(['test_task'], true);
910-
$result = ob_get_clean();
911-
912-
$expected = <<<TEXT
913-
<info>Welcome to CakePHP Console</info>
914-
I am a test task, I dispatch another Shell
915-
I am a dispatched Shell
916-
917-
TEXT;
918-
$this->assertEquals($expected, $result);
919-
}
920-
921972
/**
922973
* test wrapBlock wrapping text.
923974
*

tests/test_app/TestApp/Shell/TestingDispatchShell.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,83 @@ public function out($message = null, $newlines = 1, $level = Shell::NORMAL)
3636
}
3737

3838
public function testTask()
39+
{
40+
$this->out('I am a test task, I dispatch another Shell');
41+
Configure::write('App.namespace', 'TestApp');
42+
$this->dispatchShell('testing_dispatch dispatch_test_task');
43+
}
44+
45+
public function testTaskDispatchArray()
3946
{
4047
$this->out('I am a test task, I dispatch another Shell');
4148
Configure::write('App.namespace', 'TestApp');
4249
$this->dispatchShell('testing_dispatch', 'dispatch_test_task');
4350
}
4451

52+
public function testTaskDispatchCommandString()
53+
{
54+
$this->out('I am a test task, I dispatch another Shell');
55+
Configure::write('App.namespace', 'TestApp');
56+
$this->dispatchShell(['command' => 'testing_dispatch dispatch_test_task']);
57+
}
58+
59+
public function testTaskDispatchCommandArray()
60+
{
61+
$this->out('I am a test task, I dispatch another Shell');
62+
Configure::write('App.namespace', 'TestApp');
63+
$this->dispatchShell(['command' => ['testing_dispatch', 'dispatch_test_task']]);
64+
}
65+
66+
public function testTaskDispatchWithParam()
67+
{
68+
$this->out('I am a test task, I dispatch another Shell');
69+
Configure::write('App.namespace', 'TestApp');
70+
$this->dispatchShell([
71+
'command' => ['testing_dispatch', 'dispatch_test_task_param'],
72+
'extra' => [
73+
'foo' => 'bar'
74+
]
75+
]);
76+
}
77+
78+
public function testTaskDispatchWithMultipleParams()
79+
{
80+
$this->out('I am a test task, I dispatch another Shell');
81+
Configure::write('App.namespace', 'TestApp');
82+
$this->dispatchShell([
83+
'command' => ['testing_dispatch dispatch_test_task_params'],
84+
'extra' => [
85+
'foo' => 'bar',
86+
'fooz' => 'baz'
87+
]
88+
]);
89+
}
90+
91+
public function testTaskDispatchWithRequestedOff()
92+
{
93+
$this->out('I am a test task, I dispatch another Shell');
94+
Configure::write('App.namespace', 'TestApp');
95+
$this->dispatchShell([
96+
'command' => ['testing_dispatch', 'dispatch_test_task'],
97+
'extra' => [
98+
'requested' => false
99+
]
100+
]);
101+
}
102+
45103
public function dispatchTestTask()
46104
{
47105
$this->out('I am a dispatched Shell');
48106
}
107+
108+
public function dispatchTestTaskParam()
109+
{
110+
$this->out('I am a dispatched Shell. My param `foo` has the value `' . $this->param('foo') . '`');
111+
}
112+
113+
public function dispatchTestTaskParams()
114+
{
115+
$this->out('I am a dispatched Shell. My param `foo` has the value `' . $this->param('foo') . '`');
116+
$this->out('My param `fooz` has the value `' . $this->param('fooz') . '`');
117+
}
49118
}

0 commit comments

Comments
 (0)