phpspec / prophecy

Highly opinionated mocking framework for PHP 5.3+
MIT License
8.53k stars 241 forks source link

[Idea] Implement contract tests based on the mocking #36

Closed stof closed 11 years ago

stof commented 11 years ago

I found the Ruby Bogus mocking library which provides a feature named contract testing. They are verifying that the behavior of mocked object is in sync with the behavior of the actual implementing by checking whether the spec of the mocked class actually describes a case corresponding to the interaction done with the mock.

I'm not sure if this can be implemented in Prophecy itself as Bogus relies on the possibility to replace the class definition in Ruby to wrap it. But it would still be great to investigate such feature. If Prophecy alone cannot achieve it, it may be possible to implement this in PhpSpec as the spec class could register the calls done on the subject. But it may still require some new code in prophecy to retrieve the calls done on mocks.

What do you think about it ?

everzet commented 11 years ago

I think it's very interesting idea and could become a great feature. Feature of PhpSpec. The reason why we can't have it in Prophecy is simply because Prophecy doesn't know how to differentiate user-owned classes from 3rd-party ones - PhpSpec can. And it's really important that if we bring such feature, it wouldn't bother people with recommendations to cover code that they don't own.

As of implementation, it should be quite straight-forward as you rightly said - PhpSpec proxies all SUS calls anyways - we just need to record those and compare with recorded prophecies calls. As of Prophecy involvement and

But it may still require some new code in prophecy to retrieve the calls done on mocks.

Prophecy already has it - its out-of-the box spies support. Every single call you're making on any double gets recorded in that double prophecy's CallCenter as additional Call object. This Call stores method name, arguments it was called with and even return value it returned to the outside world. So, basically, you have everything you need from the Prophecy side ;)

stof commented 11 years ago

Moved to tje phpspec repo