8000 call to member function get on a non object · Issue #12798 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

call to member function get on a non object #12798

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

Closed
ghost opened this issue Dec 2, 2014 · 7 comments
Closed

call to member function get on a non object #12798

ghost opened this issue Dec 2, 2014 · 7 comments

Comments

@ghost
Copy link
ghost commented Dec 2, 2014

Following the example set at the bottom of this dialog helper page
http://symfony.com/doc/current/components/console/helpers/dialoghelper.html

When I attempt to run the following test

    public function test_interactive_dialogue()
    {
        // ...
        $command = new SomeCommand();
        $commandTester = new CommandTester($command);

        $dialog = $command->getHelper('dialog');
        $dialog->setInputStream($this->getInputStream("Test\n"));

        $commandTester->execute(array('command' => $command->getName()));

    }

    protected function getInputStream($input)
    {
        $stream = fopen('php://memory', 'r+', false);
        fputs($stream, $input);
        rewind($stream);

        return $stream;
    }

I get the following exception stack

PHP Fatal error:  Call to a member function get() on a non-object in
PHP   2. IDE_Base_PHPUnit_TextUI_Command::main()
PHP   3. PHPUnit_TextUI_Command->run()
PHP   4. PHPUnit_TextUI_TestRunner->doRun()
PHP   5. PHPUnit_Framework_TestSuite->run() 
PHP   6. PHPUnit_Framework_TestCase->run()
PHP   7. PHPUnit_Framework_TestResult->run()
PHP   8. PHPUnit_Framework_TestCase->runBare()
PHP   9. PHPUnit_Framework_TestCase->runTest()
PHP  10. ReflectionMethod->invokeArgs()
PHP  11. SQLUp\Tests\WrapperCommands\GlobalConfigCommandTest->test_interactive_dialogue()
PHP  12. Symfony\Component\Console\Command\Command->getHelper()

on this specific line

        $dialog->setInputStream($this->getInputStream("Test\n"));

It's calling the getHelper method on a object that doesn't have it, the command method instead of the dialogue method that's being utilized in the execute method implemented.

This code works, I've tested it from command line, the test doesn't reflect that though, the exception is correct, but the example needs to be updated to address this issue on the symfony 2 docs.

@xabbuh
Copy link
Member
xabbuh commented Dec 2, 2014

You will either have to register the command at an application or initialise the command's helper set manually.

@peterrehm
Copy link
Contributor

@xabbuh But it looks like his code sample is almost exactly what the docs state?

@xabbuh
Copy link
Member
xabbuh commented Dec 3, 2014

@peterrehm Well, there is a placeholder at the top of the method indicating that there would be some more code. This is maybe not ideal in this chapter which doesn't talk about testing commands in general.

@BardiaAfshin This looks like a documentation issue to me. Could you open an issue over there to improve the documentation here?

@ghost
Copy link
Author
ghost commented Dec 3, 2014

@xabbuh sure thing, where is the documentation issue official home?

@xabbuh
Copy link
Member
xabbuh commented Dec 3, 2014

@ghost
Copy link
Author
ghost commented Dec 3, 2014

Created issue

symfony/symfony-docs#4581

@peterrehm
Copy link
Contributor

I have just submitted the docs PR.

If you want to initialize in an application you need to do the following:

        $application = new Application();
        $application->add(new MyCommand());
        $command = $application->find('my:command:name');
        $commandTester = new CommandTester($command);

However to manually set the HelperSet you can do the following:

$command->setHelperSet(
    new HelperSet(array(
            new FormatterHelper(),
            new DialogHelper(),
            new ProgressHelper(),
            new TableHelper(),
            new DebugFormatterHelper(),
            new ProcessHelper(),
            new QuestionHelper(),
        ))
);

I hope this makes it a bit more clear and the docs will be improved shortly.

wouterj added a commit to symfony/symfony-docs that referenced this issue Dec 28, 2014
…with Helpers (peterrehm)

This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #4582).

Discussion
----------

Completed the needed context to successfully test commands with Helpers

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | all
| Fixed tickets | #4581

As mentioned in symfony/symfony#12798 the explanation about the initialization is not clear.
Therefore I have added the needed context and a reference to the related cookbook article.

Commits
-------

a94bd71 Completed the needed context to successfully test commands
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0