Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.1 - Shell "requested" param #6292

Merged
merged 2 commits into from Apr 21, 2015

Conversation

HavokInspiration
Copy link
Member

Dispatching a Shell within another shell (with Shell::dispatchShell()) makes the welcome message called twice :

Welcome to CakePHP v3.0.0 Console
---------------------------------------------------------------
App : src
Path: /home/yves/PhpStormProjects/cakephp-app/src/
---------------------------------------------------------------

First Shell output
Need to dispatch another shell inside this one

Welcome to CakePHP v3.0.0 Console
---------------------------------------------------------------
App : src
Path: /home/yves/PhpStormProjects/cakephp-app/src/
---------------------------------------------------------------

Second shell output

This PR adds a new parameter called "requested" that will prevent the welcome message to be displayed.
By extension, this parameter could be used in userland to detect if the Shell was called with the dispatchMethod.
Additionally, any call to dispatchShell() will append this parameter to the list of parameters.

More details on #6272.

@HavokInspiration
Copy link
Member Author

I'll fix the failing tests

@markstory markstory added this to the 3.1.0 milestone Apr 8, 2015
@@ -211,7 +211,10 @@ public function initialize()
*/
public function startup()
{
$this->_welcome();
$requested = $this->param('requested');
if (empty($requested)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you just test the truthiness of the param? Given that its always locked to a boolean it will be a yes/no condition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

@HavokInspiration HavokInspiration force-pushed the 3.1-shell-requested branch 4 times, most recently from a5cc596 to 2e88862 Compare April 8, 2015 21:15
@HavokInspiration
Copy link
Member Author

@markstory I've refactored it all and went with your suggestion of having a new extra arguments that can be used when dispatching a Shell. I went with passing the parameter directly to runCommand() and dispatch(). Felt simpler and avoid having the extra parameters stored somewhere. And since they are "runtime" parameter in a way, that makes more sense IMO.

It might be good to test the parsing of the dispatchShell() method. But I'll have to admit that I need some hint on how to do this, since all it returns is a bool.

@@ -211,7 +211,9 @@ public function initialize()
*/
public function startup()
{
$this->_welcome();
if (!(bool)$this->param('requested')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

! does implicit boolean casting for you.

@markstory
Copy link
Member

Testing dispatchShell() is pretty painful which is why there aren't any existing tests sadly.

@HavokInspiration
Copy link
Member Author

Ok, that's what I was afraid of.
What about having a method to do this part of the logic and add tests for it ? That would at least prevent regressions in arguments parsing if dispatchShell() needs modifications in the future.

@markstory
Copy link
Member

@HavokInspiration Another method sounds like a good option to me.

@HavokInspiration HavokInspiration force-pushed the 3.1-shell-requested branch 2 times, most recently from a4ff279 to 71a9243 Compare April 9, 2015 20:40
@HavokInspiration
Copy link
Member Author

Added a method to parse arguments given to dispatchShell() and corresponding tests.
I also did a "real" test dispatching a shell from another shell and it works okay.

It does feel flying without wings to not have tests validating that dispatchShell() works okay. I certainly would not want to be the guy who broke a part of the framework 😄

Is there no way to test this? With I don't know... A custom test shell that will overwrite out() to make it log the output, call a command that dispatch the same shell that calls out() and compares the final values of the outputed message and what we expect ?
Something like this :

class TestingShell extends Shell
{

    public $out = [];

    public function out($message = null, $newlines = 1, $level = Shell::NORMAL)
    {
        $this->out[] = $message;
    }

    public function testTask()
    {
        $this->out('I am a test task, I dispatch another Shell');
        $this->dispatchShell('testing dispatch_test_task');
    }

    public function dispatchTestTask()
    {
        $this->out('I am a dispatched Shell');
    }
}

//In test files
$TestingShell = new TestingShell();
$TestingShell->runCommand('test_task');

//Compare $TestingShell->out with our own expected array

It's a draft done quick and dirty, just to have something more concrete than my so poorly written explanation. But that might give some sort of coverage to dispatchShell(). Or maybe I'm missing something and that would not work at all.

@markstory
Copy link
Member

Looks good to me.

This "extra" arguments allow additional parameters to be passed to the Shell without them needing to be a CLI flag or a command argument.
 This commit also introduce a "requested" extra parameter that is automatically passed when a Shell is dispatched with a Shell::dispatchShell() call. This parameter, when set and not null or false, prevents the default cake shell welcome message from being displayed
…Shell::dispatchShell() call.

Add tests for that method.
markstory added a commit that referenced this pull request Apr 21, 2015
@markstory markstory merged commit a1c7b51 into cakephp:3.1 Apr 21, 2015
markstory added a commit to cakephp/docs that referenced this pull request Apr 21, 2015
@HavokInspiration
Copy link
Member Author

@markstory I think this was merged too early. Since master was merged to 3.1, the dispatchShell test added on #6325 will probably fail (the welcome message will not be outputed, hence the expected value will not be correct anymore.
I planned on adding the tests this week (I should have posted a message, my bad)

I'll try to submit a quick fix for the test for the 3.1 branch with just github online editor (will check on my fork first).
I'll add more in depth tests to dispatchShell linked to this PR (hopefully, before the end of the week)

@markstory
Copy link
Member

Whoops. Failing tests can be fixed easily enough 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants