markhuot / craft-pest

https://craft-pest.com
Other
38 stars 11 forks source link

adds scenarios to store #78

Closed markhuot closed 1 year ago

markhuot commented 1 year ago

This allows you to set the scenario while saving an element, exposing the validation routines to tests. You can now expect save exceptions or mute them and assert that an entry is either valid or invalid.

// You can expect a model store exception when the scenario is set to live
$this->expectException(ModelStoreException::class);

$entry = Entry::factory()
  // otherwise you can "mute" validation exceptions when the scenario is set to live
  // this will cause the `->create()` method to return the element with a filled `->errors` key
  ->muteValidationErrors()
  ->scenario(\craft\elements\Entry::SCENARIO_LIVE)
  ->create();

// regardless of how you handle exceptions, you can assert that a validation exception
// was caught on a specific field
$entry->assertInvalid(['fieldHandle', 'authorId']);

// or, if everything should go well you can assert that too
$entry->assertValid();