Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Testing commands with mocked dependencies fails since 3.0.3 #18558

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
ruudvanderweijde opened this issue Apr 15, 2016 · 8 comments
Closed

Comments

@ruudvanderweijde
Copy link

In symfony 3.0.2 and before you could test a console command with mocks like:

$application = new Application($kernel);
$application->add(new MyConsoleCommand($myMockedRepository));

// we check it's properly registered :)
$command = $application->find('namespace:my-command');

$commandTester = new CommandTester($command);
// execute command

But since 3.0.3 the mocked object is not set in the console command when it is executed.
A way to work around it is:

$myCommand = new MyConsoleCommand($myMockedRepository);
$application = new Application($kernel);
$application->add($myCommand);

$commandTester = new CommandTester($command);
// execute command

But then I cannot use the $application->find() method.

Was this a desired change? I cannot seem to find it in the change log.

@xabbuh
Copy link
Member

xabbuh commented Apr 15, 2016

@ruudvanderweijde Could you fork the Symfony Standard Edition and make the changes that are necessary to reproduce your issue?

@xabbuh xabbuh added the Console label Apr 15, 2016
@ruudvanderweijde
Copy link
Author

ruudvanderweijde commented Apr 16, 2016

I did (see ruudvanderweijde/symfony-standard@6ceaf60) and found something weird.

The test was all fine, until I require mockery/mockery. Then when I run the test (even without no using mockery) the test fails.
Then when I remove mockery again, the test still fails 😕

@ruudvanderweijde
Copy link
Author

It seems not to be related to mockery, it is related to composer install on v3.0.3

Steps to reproduce:

git checkout v3.0.3
composer install
git checkout 6ceaf605c3410d2e41154a9f08dc1a476fdc85e1
phpunit

Result:

PHPUnit 4.7.7 by Sebastian Bergmann and contributors.

E.

Time: 374 ms, Memory: 20.00Mb

There was 1 error:

1) Tests\AppBundle\Command\MyCommandTest::testCommand
Exception: this method should be mocked

/Users/ruud/git/ruudvanderweijde/symfony-standard/src/AppBundle/Service/MyService.php:9
/Users/ruud/git/ruudvanderweijde/symfony-standard/src/AppBundle/Command/MyCommand.php:33
/Users/ruud/git/ruudvanderweijde/symfony-standard/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:256
/Users/ruud/git/ruudvanderweijde/symfony-standard/vendor/symfony/symfony/src/Symfony/Component/Console/Tester/CommandTester.php:80
/Users/ruud/git/ruudvanderweijde/symfony-standard/tests/AppBundle/Command/MyCommandTest.php:29

@xabbuh
Copy link
Member

xabbuh commented Apr 19, 2016

The behaviour you observe is related to the changes done in #17569.

@xabbuh
Copy link
Member

xabbuh commented Dec 16, 2016

@ruudvanderweijde Does #20442 fix your issue?

@ruudvanderweijde
Copy link
Author

@xabbuh No, still the same error.

I noticed that I removed my local repository. Want me to clone symfony-standard again?

@xabbuh
Copy link
Member

xabbuh commented Dec 16, 2016

@ruudvanderweijde That would be nice.

@ruudvanderweijde
Copy link
Author

ruudvanderweijde commented Dec 16, 2016

@xabbuh ruudvanderweijde/symfony-standard@d897fd8

I noticed that it doesn't matter wether or not I'm adding the command the the application: ruudvanderweijde/symfony-standard@d897fd8#diff-c1f483ff17980e2a7d449c0d09efcca9R25
$application->add($consoleCommand);
Maybe I'm doing something else wrong?

aaa2000 added a commit to aaa2000/symfony that referenced this issue May 24, 2017
fabpot added a commit that referenced this issue May 28, 2017
…egistered by the kernel (aaa2000)

This PR was merged into the 2.7 branch.

Discussion
----------

[FrameworkBundle][Console] Fix the override of a command registered by the kernel

| Q             | A
| ------------- | ---
| Branch?       |2.7
| Bug fix?      | yes
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #18558 <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        |  <!--highly recommended for new features-->

<!--
- Bug fixes must be submitted against the lowest branch where they apply
  (lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the 3.4,
  legacy code removals go to the master branch.
- Please fill in this template according to the PR you're about to submit.
- Replace this comment by a description of what your PR is solving.
-->

Fix the override of a command registered by the kernel

Commits
-------

3e6643b [FrameworkBundle][Console] Fix the override of a command registered by the kernel
@fabpot fabpot closed this as completed May 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants