DefaultModelTest extends TestCase
Tests the DefaultModel class.
Due to how tightly coupled the IPropertySet instance is to the internal workings of IModel, this is more of an integration test than a unit test. This uses buffalokiwi\magicgraph\QuickPropertySet as the property set instance for testing.
Table of Contents
$instance | Test model instance | IModel |
---|---|---|
$props | Properties config used for the model | array |
createIModelInstance() | Creates a DefaultModel instance used for testing. | IModel |
setUp() | void | |
testMagicIsset() | Tests the __isset() magic method. | void |
testMagicGetSet() | Tests the __get() and __set() magic methods. | void |
testGetValue() | Tests the IModel::getValue() method. | void |
testGetValueThrowsInvalidArgumentException() | Test that retrieving a property value for an invalid property name throws an exception | mixed |
testInvalidSetValueSilentlyFails() | Test that setting a value of some non-existing property throws an exception | void |
testSetValueValidationException() | Test that setting an invalid value for the "validate" test property If validation fails from a property closure, an error is triggered and false is returned by the validate function. | void |
testSetterBehaviorCallback() | Test to ensure the setter behavior callback can modify the value prior to validation being called. | void |
testSetValueAsArrayException() | Test to ensure that setValue throws an InvalidArgumentException if the passed value is an array | void |
testSetValueAsObjectException() | Tests passing an object to setValue for a property that does not accept object values. | void |
getSetValue() | Tests the IModel::setValue() method. | void |
testGetModifiedProperties() | Tests IModel::getModifiedProperties() Expected to return a cloned instance of the supplied IPropertySet instance with enabled bits matching the enabled bits of the internal edited bitset. | void |
testGetPropertySet() | Tests IModel::getPropertySet() Expects the IPropertySet instance supplied to the constructor to be returned | void |
testToJSON() | Tests IModel::toObject() Using the active bits in the IPropertySet instance supplied to the constructor, this expects a valid JSON object to be returned. | void |
testToArray() | Tests IModel::toArray() Using the active bits in the IPropertySet instance supplied to the constructor, this expects an array to be returned containing key value pairs. | void |
testEquals() | Tests IModel::equals() Let A and B being instances of IModel, and test the following: | void |
testValidate() | Tests IModel::validate() Expects IProperty::validate() to be called for each property listed within the IPropertySet instance supplied to the DefaultModel constructor | void |
testHash() | Test IModel::hash() Expects hash() to return an md5 hash of: | void |
testGetInsertProperties() | Tests IModel::getInsertProperties() Expects a clone of the supplied IPropertySet to be returned with the bits enabled that match any properties that DO NOT have the INSERT flag enabled. | void |
Properties
$instance
Test model instance
private
IModel
$instance
$props
Properties config used for the model
private
array
$props
Methods
createIModelInstance()
Creates a DefaultModel instance used for testing.
protected
createIModelInstance(
)
: IModel
Properties are contained in $this->props.
Return values
IModelsetUp()
public
setUp(
)
: void
testMagicIsset()
Tests the __isset() magic method.
public
testMagicIsset(
)
: void
This simply calls IPropertySet::isMember()
testMagicGetSet()
Tests the __get() and __set() magic methods.
public
testMagicGetSet(
)
: void
This calls IModel::getValue()
testGetValue()
Tests the IModel::getValue() method.
public
testGetValue(
)
: void
Expects an InvalidArgumentException if the supplied property name is not a member of the supplied IPropertySet If the IProperty::getGetterCallback() result is an instance of \Closure, the result of that Closure is expected to be returned. Otherwise the stored value for the property is expected.
testGetValueThrowsInvalidArgumentException()
Test that retrieving a property value for an invalid property name throws an exception
public
testGetValueThrowsInvalidArgumentException(
)
: mixed
Return values
mixedtestInvalidSetValueSilentlyFails()
Test that setting a value of some non-existing property throws an exception
public
testInvalidSetValueSilentlyFails(
)
: void
testSetValueValidationException()
Test that setting an invalid value for the "validate" test property If validation fails from a property closure, an error is triggered and false is returned by the validate function.
public
testSetValueValidationException(
)
: void
The validate property has a behavior callback that will purposely fail for any non-empty value.
testSetterBehaviorCallback()
Test to ensure the setter behavior callback can modify the value prior to validation being called.
public
testSetterBehaviorCallback(
)
: void
The setter behavior callback on the "testpreparevalidate" test property will convert any value to a \stdClass, which will cause a ValidationException to be thrown.
testSetValueAsArrayException()
Test to ensure that setValue throws an InvalidArgumentException if the passed value is an array
public
testSetValueAsArrayException(
)
: void
testSetValueAsObjectException()
Tests passing an object to setValue for a property that does not accept object values.
public
testSetValueAsObjectException(
)
: void
getSetValue()
Tests the IModel::setValue() method.
public
getSetValue(
)
: void
Expects an InvalidArgumentException if the supplied property name is not a member of the supplied IPropertySet if IProperty::getSetterCallback() returns a valid \Closure the result of that Closure is expected to be used as the value prior to validate being called IProperty::validate() is expected to be called If the value is an IEnum instance, it is expected to call IEnum::setValue() for the stored enum If the value is an ISet instance, it is expected to call the ISet::clear(), then ISet::add() methods for the stored set If the value is an object, it MUST match IProperty::getClass() If the value is an object or array and the setter callback and IProperty::getClass() are undefined, then an InvalidArgumentException is expected to be thrown The value is expected to be stored in the model
testGetModifiedProperties()
Tests IModel::getModifiedProperties() Expected to return a cloned instance of the supplied IPropertySet instance with enabled bits matching the enabled bits of the internal edited bitset.
public
testGetModifiedProperties(
)
: void
Edited bits are expected to be enabled by calling setValue()
testGetPropertySet()
Tests IModel::getPropertySet() Expects the IPropertySet instance supplied to the constructor to be returned
public
testGetPropertySet(
)
: void
testToJSON()
Tests IModel::toObject() Using the active bits in the IPropertySet instance supplied to the constructor, this expects a valid JSON object to be returned.
public
testToJSON(
)
: void
The JSON object must be an object with properties matching the property names of the IPropertySet instance, and the values matching the stored values in the IModel instance for those properties.
{"property":"value"}
testToArray()
Tests IModel::toArray() Using the active bits in the IPropertySet instance supplied to the constructor, this expects an array to be returned containing key value pairs.
public
testToArray(
)
: void
The array must have keys matching the property names of the IPropertySet instance, and the values matching the stored values in the IModel instance for those properties.
["property" => "value"]
testEquals()
Tests IModel::equals() Let A and B being instances of IModel, and test the following:
public
testEquals(
)
: void
get_class( A ) == get_class( B ) get_class( A::getPropertySet()) == get_class( B::getPropertySet()) A::hash() == B::hash()
Expects the class name of A to match the class name of B Expects the IPropertySet instance passed to A to be the same class as The IPropertySet passed to B Expects the result of the hash methods to match.
testValidate()
Tests IModel::validate() Expects IProperty::validate() to be called for each property listed within the IPropertySet instance supplied to the DefaultModel constructor
public
testValidate(
)
: void
testHash()
Test IModel::hash() Expects hash() to return an md5 hash of:
public
testHash(
)
: void
The Model's class name + The IPropertySet class name + a concatenated list of all included property names
testGetInsertProperties()
Tests IModel::getInsertProperties() Expects a clone of the supplied IPropertySet to be returned with the bits enabled that match any properties that DO NOT have the INSERT flag enabled.
public
testGetInsertProperties(
)
: void