Behavior driven development in Yii

Anyone that came from Ruby probably knows what is BDD, Cucumber and it’s advantages over the plain unit tests. For the rest, BDD is way to test models AND controllers with easy to read “scenarios”.

Great thing is that Cucumber (Ruby’s BDD framework) is ported to PHP and it’s called Behat . Example of working with behat can be checked here .

Anyone tried to integrate behat with Yii? I’m working with Yii for almost 2 years, and used it for several large projects so far. Currently, my team is preparing for another large project and we’re thinking to make this integration ourselves. Any help (code or ideas) will be appreciated and we will of course share our integration (although cannot promise future maintenance)

Of course, it would be best if Yii team decide to make it since it woule be included in future versions of framework. That’s why I put it in here :)

P.S. Symphony 2.0 will have Behat integrated…

Don’t we have already a set of TDD (Test Driven Development) classes with Yii (ie CWebTestCase)? what is the difference between Behat or PHPUnit?

PS: Just curious… I have read Behat docs and is interesting but I do not see major diffferences

Difference is that Unit tests are testing only models, not how they are implemented in controllers. With behat, you test both controllers and models, otherwise you have to use selenium or similar (unreliable) tools to test controllers. Other differences are: test are easy to understand even for common people and you can use it as some kind of documentation, use cases for testers and etc. And to be honest, creating unit tests is boring even we denied this because it helps us a lot. Creating scenarios (tests in BDD) can be fun and when they are finished,they give you great overview on what needs to be done and in which order.

My advice is that you try BDD on simple examples (without Yii) and soon you’ll see the difference

Very interesting… thank you very much for your explanation. If I have a deeper look I let you know, it doesn’t seem very hard to have a go.

Can’t agree more - but don’t you still need to write the unit tests below BDD? Or did i get this wrong? I can’t imagine that BDD auto generates unit tests for you :)

You can create unit tests with behat as a part of scenario… check out link I posted before At the bottom of the page you can see assets presented in ascii-like table and of course given scenario will use those and create unit test for models

While searching for BDD and PHP found also this - Support for BDD and Stories in PHPUnit 3.3

I’ve look at it. I have to say - nice stuff. Too bad it’s not powerful as behat. Still worth a look at it.

I also found PHPSpec which seems to be clone of RSpec (another BDD framework for ruby) but it is not stable yet.

Sounds like a good suggestion. If PHPUnit is implementing a similar solution, would it be more backwards-compatible for Yii to stick with PHPUnit for BDD implementation?

I am installing Behat and I want to implement bdd and tdd.

I might give it a go too - I really can’t get the hang on TDD with PHPUnit…

Behat seems like a winner.

I was really looking forward for PhpUnit Stories support. Unfortunately Mr. Bergmann decided not to support them any more. One thing that really scares me in Behat is the use of regex to match implementations:

/** @Given /^I am in a directory "([^"]*)"$/ */

Also, I was under the impression that, compared to php_unit, it requires a lots of additional steps to get going. A little bit like walking that way. :) Maybe that’s because I didn’t give it a lot time.

And what if I need to test human actions like clicks or something like that? I am talking about a tool like Mink for Symfony 2.

I’m really interested in this too. Just come to Yii from Rails, and my last project was using Cucumber (plus a bit of RSpec) for BDD. I found it so useful and made the whole process of development really flow, at the same time leaving a trail of meaningful tests which can be used at any time to test part or all of the system. Helps to make really solid apps without the worry of hidden bugs plus “user stories” that integrate with code.

Would love to see something like this for Yii… perhaps Behat can be integrated somehow?

Behat AND BDD for PHP sound more interesting then TDD development, and it would be good if Yii would support BDD in addition to current support for TDD development(unit tests).