Skip to content

MockDataManipulation

Aleksandar Jovanovic edited this page Apr 5, 2015 · 2 revisions

DbMockLibrary\MockDataManipulation

MockDataManipulation extends DataContainer class and is therefore singleton as well. It's the main database mocking class. It provides a number of operations to be performed on mock data, that simulate actual database operations. Usage examples

For additional help, look at the tests, additional input scenarios are tested. deleteRow($collection, array $ids)

Deletes one or more rows, indicated by $ids array, from the collection. If any of the rows is missing, exception is thrown.

// dummy data before ['collection' => ['id1' => [1],'id2' => [2]]]
MockDataManipulation::deleteRow('collection', ['id1', 'id2']);
// dummy data after ['collection' => []]

dropCollections(array $collections = [])

Drops one or moe collections. If any if the collections is missing, exception is thrown.

['collection1' => ['id1' => [1]], 'collection2' => ['id3' => [1]]]
MockDataManipulation::dropCollections(['collection1', 'collection2']);
// dummy data after []

getAllIds(array $collections = [], $byCollection = false)

Retrieves the list of all rows' ids.

// dummy data: ['collection1' => ['id1' => [1], 'id2' => [2]], 'collection2' => ['id3' => [1]]]
MockDataManipulation::getAllIds(['collection1', 'collection2']); // ['id1', 'id2', 'id3']
MockDataManipulation::getAllIds(['collection1'], true); // ['collection1' => ['id1', 'id2']]

getCollectionElements($collection, $id = false)

Fetches dummy data by collection and optionally by row inside the collection. If collection or row id do not exist, exception is thrown.

// dummy data: ['collection' => ['id1' => [1], 'id2' => [2]]]
MockDataManipulation::getCollectionElements('collection1') // ['id' => [1], 'id2' => [2]]
MockDataManipulation::getCollectionElements('collection1', 'id1') // [1]

revertCollections(array $collections = [])

Reverts one or more collections to the initial state (state they were in upon initialization of the class). If the collection was not present at the initialization time, but was added later, it is dropped. If one of the collection does not exist and didn't exist in initial data, exception is thrown.

// initial dummy data: ['collection1' => ['id1' => [1]]]
// dummy data: ['collection1' => ['id1' => [2], 'id2' => [2]], 'collection2' => ['id1' => [1]]]
MockDataManipulation::revertCollections(['collection1', 'collection2'])
// dummy data: ['collection1' => ['id1' => [1]]]

saveCollection($value, $collection)

Adds new, or overwrites existing collection to the dummy data. Value must be two-dimensional array, or the exception is thrown.

// dummy data: ['collection' => ['id' => ['field' => 'value']]]
MockDataManipulation::saveCollection(['fooBar' => ['fooBar' => 'fooBar']], 'collection');
// dummy data: ['collection' => ['fooBar' => ['fooBar' => 'fooBar']]]

// dummy data: ['collection' => ['id' => ['field' => 'value']]]
MockDataManipulation::saveCollection(['fooBar' => ['fooBar' => 'fooBar']], 'fooBar');
// dummy data: ['collection' => ['id' => ['field' => 'value']], 'fooBar' => ['fooBar' => ['fooBar' => 'fooBar']]]

saveData($value, $collection = '', $id = '', $field = '')

Changes content of the dummy data, by adding or editing collection, row or field. Value has to be in appropriate format, depending the value of input parameters.

// dummy data: ['collection' => ['id' => ['field' => 'value']]]
MockDataManipulation::saveData(['fooBar' => ['fooBar' => ['fooBar' => 'fooBar']]]);
// dummy data: ['fooBar' => ['fooBar' => ['fooBar' => 'fooBar']]]

// dummy data: ['collection' => ['id' => ['field' => 'value']]]
MockDataManipulation::saveData('fooBar', 'collection', 'id', 'fooBar');
// dummy data: ['collection' => ['id' => ['field' => 'value', 'fooBar' => 'fooBar']]]

saveField($value, $collection, $id, $field)

Adds or alters field in the dummy data. If collection, or row do not exist, exception is thrown.

// dummy data: ['collection' => ['id' => ['field' => 'value']]]
MockDataManipulation::saveField('fooBar', 'collection', 'id' 'field');
// dummy data: ['collection' => ['id' => ['field' => 'fooBar']]]

saveRow($value, $collection, $id)

Adds or alters row of dummy data. If collection does not exist, exception is thrown.

// dummy data: ['collection' => ['id' => ['field' => 'value']]]
MockDataManipulation::saveRow(['fooBar' => 'fooBar'], 'collection', 'id');
// dummy data: ['collection' => ['id' => ['fooBar' => 'fooBar']]]

truncateCollections

Empties one or more cllections. If collection doesn't exist, exception is thrown.

// dummy data: ['collection' => ['id' => ['field' => 'value']]]
MockDataManipulation::truncateCollections(['collections');
// dummy data: ['collection' => []]