Skip to content

WIP: DF_Model result classes — shortcut for triggering exception-throwing when saving

Robert Sinton requested to merge patch/df_model_result__uec_shortcut into master

TO DO

Currently conflicted with master — only due to use of v 1.8.2 for a different change in DF_Model_result.php. Merged and resolve when ready to review.

What I did

  • Added an error_class option to DF_Model_result's save() method.

Implications

This change makes it much easier to produce mock objects with their database-saving behaviour mocked out, when we want exception-throwing behaviour from DF_Model.

Currently, we need to write code like this in order to trigger exception throwing if something goes wrong while saving (unless the model itself is set to always throw exceptions, but that is very rare — much more common to be adding calls like this with legacy models):

$object->use_error_class()->save();

When writing unit tests that use mock objects to test code that includes calls like the above, we need to manufacture mock objects where not only save() is mocked out, but also where use_error_class() is mocked to return an object that behaves properly in this chain, i.e. returns a mock object that can have save() called on it.

With this change, we can save objects like with this simple call:

$object->save(true);

Where the parameter indicates that we want exception-throwing behaviour. true will throw the default exception class for that object or DF_Model, or we can pass the name of an exception class that we want thrown.

Then when writing unit tests, the mock objects just needs to have save() mocked to return null.

Setup

None

How to test

  • Code review
  • Having trouble finding a way to test this in practice; will leave as WIP for now.
Edited by Robert Sinton

Merge request reports