3

I am using phpspec to test our application. Now we need to develop some commands and therefore I want to spec these as well.

I am a bit stuck here, because the docs (http://symfony.com/doc/master/cookbook/console/console_command.html#testing-commands) only tell me how to test the commands using phpunit.

Even if I would use a similar approach (creating the kernel somehow, and instantiating the command in question) in the specs I think that this would not follow the idea of describing behaviour. I would only spec, whether the output is correct or not, but not if the command is calling the right methods and so on.

Has anyone used phpspec to successfully describe Symfony2 commands? What would be a feasible approach of doing this?

Thanks

Coruja
  • 51
  • 3

2 Answers2

3

You can use PhpSpec to test Command in a unit testable way - it's not too bad but you end up with a lot of mocking of input/output.

It's better to keep your Commands nice and small, with them delegating to other services that are unit tested - then you can cover with Behat and get your confidence that they're working.

It's a very similar problem to testing Controllers, just that Commands have more dependencies to mock if you try the unit testing approach.

Ciaran McNulty
  • 18,698
  • 6
  • 32
  • 40
  • 2
    Yeah, what Ciaran said. However, trying to spec commands is a good exercise. It will make realize what kind of stuff you should delegate to services, and will teach you how to keep your commands simple. Take a look at what I've tried here: https://gist.github.com/jakzal/8455718 – Jakub Zalas Feb 17 '15 at 12:58
0

You should use Behat for this, not phpspec

user2156980
  • 365
  • 1
  • 4
  • 15