Comments (19)
After our discussion in Slack, Nuno found the issue:
The command is instantiated while bootstrapping everything (eg. building the application, etc.), so adding a mock into the container will not register with the command. To temporarily fix this, I've added the following method to my TestCase
class to resolve the command out of the container again (with the right dependencies), and add it back to the application:
/**
* @param string $command
* @param array $arguments
*/
protected function call($command, array $arguments = [])
{
$command = $this->app->getContainer()->make($command);
$this->app->add($command);
$this->app->call($command->getName(), $arguments);
}
This way, we can do the following in the tests:
/** @test */
function it_tests_a_mocked_dependency()
{
$mock = $this->createMock(TestService::class);
$this->app->getContainer()->instance(TestService::class, $mock);
$mock->expects($this->once())->method('someMethod')->willReturn('from the tests');
$this->call(HelloCommand::class);
}
Thanks, @nunomaduro for the help!
from laravel-zero.
@sandervanhooft You want to make sure a command is called right?
from laravel-zero.
I let you think about this, just make sure you prototype something quick and let's think about the real implementation later 👍
from laravel-zero.
After a bit of debugging by @rickbolton and myself, we found this line, which we thought was the perpetrator. We thought changing that to Container::getInstance()
instead of manually newing up the container (like it is now) would fix the issue, but it didn't.
Will do some more digging!
from laravel-zero.
Thanks for this. I just spent a day fighting with this issue.
from laravel-zero.
Is there a way to only bind and not call the mocked command? I am trying to test that executing command A (non-mocked) after completion calls command B (mocked).
from laravel-zero.
Hi @nunomaduro , yes that's right.
from laravel-zero.
Meanwhile, I'm testing the solution offered by @svenluijten:
- I receive an error that the
call()
method should be madepublic
instead ofprotected
. So I did. - Then I receive another error:
Call to undefined method LaravelZero\Framework\Application::getContainer()
This is triggered for this line from Sven's example:
$this->app->getContainer()->instance(PaymentListCommand::class, $mock);
from laravel-zero.
The only non-basic thing is that I have Console Dusk installed.
from laravel-zero.
@sandervanhooft You want to make sure a command is called right?
Yes, but the command I want to make sure is called is called indirectly from another command.
from laravel-zero.
@sandervanhooft A solution may be have a class service
that does the real job from the secondary command. And then, mock that service
class using the container.
from laravel-zero.
I'm going to dive a bit deeper into this in the next few days (hopefully), will let you know how it goes!
So far absolutely 💗💗 Laravel Zero by the way!
And however briefly - nice to meet you at Laracon.eu!
from laravel-zero.
@sandervanhooft A solution may be have a class
service
that does the real job from the secondary command. And then, mock thatservice
class using the container.
May be an interesting approach, but I think the assertCommandCalled
will be a recurring theme in my app and probably in other devs' as well.
from laravel-zero.
Hummmm. Could you think about an implementation for that? Would be good to add it to Laravel Zero or even to Laravel itself.
from laravel-zero.
Absolutely no idea yet. May need to implement a spy on the Kernel?
from laravel-zero.
Did a quick scan, I can think of three directions currently:
-
Inspired by
Event::fake()
, wrapIlluminate\Command
in a spy when usingCommand:spy
helper. Then use aassertCommandCalled($command, ?$callback)
test helper for asserting. It seems impossible to me to go a level deeper because then we get into Symphony territory. -
Add lifecycle Events to the Command calls (like Eloquent model has) and track these using
Event::fake()
andEvent::assertDispatched(...)
-
Do both. But that's a bit much atm.
What do you think?
from laravel-zero.
(1. "feels" possible, but will be a stretch for me so may take some time)
from laravel-zero.
@sandervanhooft Can you create a new issue with the content of that proposal? Sorry, but I would like to really follow this.
from laravel-zero.
@nunomaduro Done. See #178.
from laravel-zero.
Related Issues (20)
- Undefined constant "SIGINT" HOT 2
- Failed to open stream: phar error: Cannot open phar archive
- Consider using Laravel Prompts HOT 3
- New Project doesn't work "Undefined variable $command" HOT 2
- [BUG] self-update from github releases not working HOT 1
- Cannot find sqlite database when upgrading HOT 3
- app:build ErrorException rename(app.phar, builds/app): the system cannot find the file specified (code 2) HOT 3
- Failing builds: "database/factories" and "database/seeders" was expected to exist HOT 1
- app:build gives the error: The system cannot find the file specified (code: 2) HOT 4
- Why is package auto-discovery is off ? HOT 4
- Site https://laravel-zero.com is down HOT 1
- website down/expired. Is this project still being worked on? HOT 1
- Website is down - domain expired? HOT 3
- laravel-zero.com is down HOT 1
- Website is not working anymore http://laravel-zero.com/ HOT 3
- Global Composer installation without commiting binary to version control?
- Using DB pagination methods inside a command throws an exception HOT 1
- On Windows, when running app:build "impossible find specified file" HOT 5
- QUEUES Call to undefined method Psr\Log\NullLogger::driver()
- Composer Warnings when installing pest-plugin-laravel HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-zero.