bbatsche / Verify

BDD Assertions for PHPUnit and Codeception
MIT License
4 stars 0 forks source link

Verify Method #80

Closed bbatsche closed 2 years ago

bbatsche commented 2 years ago

Add method() accessor to Value class. Will allow you to write assertions about the return value & exception thrown by a method of an object or (static method of a) class.

verify($user)->method('getName')->will()->returnValue()->equalTo('John');

Method is invoked at returnValue() and value is stored locally in $resolvedValue so additional assertions can be done against it without reinvoking the method.

with(...$args) allows passing arguments to method, and can be used to create a sort of data provider to the method.

verify($user)->method('setBirthday')->with('Tomorrow')->will()->throwException()->instanceOf(BadBirthday::class)

throwException() will invoke method and capture the exception locally. Can drill down into properties of the exception with withMessage() and withCode() methods.

If exception is not thrown in throwException() then test is failed.

bbatsche commented 2 years ago

We could theoretically overload __call() and add this functionality there as well. Would that make the fluent API too funky? Is it adding too much to that method?

verify($user)->getName()->will()->returnValue()->equalTo('John')
    ->setBirthday('Tomorrow')->throwException()->instanceOf(BadBirthday::class)
    ->setBirthday('1984-10-30')->returnValue()->null();
bbatsche commented 2 years ago

Is there anyway we could support verify($foo)->method('bar')->willNot()->throwException();?

bbatsche commented 2 years ago

Should split out verifier classes (use withVerifier() internally to swap them out).

Value
 +- Attribute
 +- Method
     +- Exception

getActual() in Value becomes simple stud method, other verifiers override with their own logic.

Override getActual() in File as well. Create a getContents($filename) method that will do assert readable, is file, open, read contents, if false fail, return contents. getActual() uses that to read subject contents and cache locally.