bbatsche / Verify

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

Subject Inspectors #87

Open bbatsche opened 1 year ago

bbatsche commented 1 year ago

Verifier to add assertions about the contents of an array or JSON string. Something like:

verify([1, 2, 3])->arrayContent()->is()->int();

verify('{"name":"George","gpa":3.8}')->jsonContent()
    ->name->is()->identicalTo('George')
    ->gpa->within(0.01)->is()->equalTo(3.8);
bbatsche commented 1 year ago

Use __get() to allow more detailed assertions about individual array elements (if they're assoc)

bbatsche commented 1 year ago

Also add fileContent() for strings

bbatsche commented 1 year ago

Saving this off for 4.0.0 since complex inspectors like this are really going to benefit from multiple levels of chaining

bbatsche commented 1 year ago

Challenge:

With 4.0.0 we're trying to support deeper inspection of subject through property chaining, but that could interfere with inspecting JSON/array content. Something wild like subject has a user prop, toJson() method, and we want to inspect its structure

Solution

JSON & Array content inspectors accept an optional callback that allows multiple assertions on content. Something like:

verify($subject)->user->toJson()
    ->jsonContent(static function (Verifier $jsonConent): void {
        $jsonContent->first_name->is()->identicalTo('John')
            ->and()->last_name->is()->identicalTo('Smith');
    });