Skip to content

Commit

Permalink
Adjust tests to the latest phpunit (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Sep 26, 2022
1 parent fa3353a commit bd90320
Show file tree
Hide file tree
Showing 29 changed files with 62 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ $m->loadBy('name', 'Pear Company');
$m->ref('Order')
->save(['ref' => 'TBL1', 'delivery' => new DateTime('+1 month')])
->ref('Lines')->import([
['Table', 'category' => 'furniture', 'qty' => 2, 'price' => 10.50],
['Table', 'category' => 'furniture', 'qty' => 2, 'price' => 10.5],
['Chair', 'category' => 'furniture', 'qty' => 10, 'price' => 3.25],
]);
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"phpstan/phpstan": "^1.6",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-strict-rules": "^1.3",
"phpunit/phpunit": "^9.5.5"
"phpunit/phpunit": "^9.5.25"
},
"conflict": {
"jdorn/sql-formatter": "<1.2.9"
Expand Down
4 changes: 2 additions & 2 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ which I want to define like this::

$this->getOwner()->addField('updated_dts', ['type' => 'datetime']);

$this->getOwner()->onHook(Model::HOOK_BEFORE_UPDATE, function ($m, $data) {
$this->getOwner()->onHook(Model::HOOK_BEFORE_UPDATE, function (Model $m, array $data) {
if (isset($this->getApp()->user) && $this->getApp()->user->isLoaded()) {
$data['updated_by'] = $this->getApp()->user->getId();
}
Expand Down Expand Up @@ -696,7 +696,7 @@ section. Add this into your Invoice Model::
Next both payment and lines need to be added after invoice is actually created,
so::

$this->onHookShort(Model::HOOK_AFTER_SAVE, function ($isUpdate) {
$this->onHookShort(Model::HOOK_AFTER_SAVE, function (bool $isUpdate) {
if ($this->_isset('payment')) {
$this->ref('Payment')->insert($this->get('payment'));
}
Expand Down
2 changes: 1 addition & 1 deletion docs/aggregates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ in various ways to fine-tune aggregation. Below is one sample use::
);

// $aggregate will have following rows:
// ['country' => 'UK', 'count' => 20, 'total_amount' => 123.20];
// ['country' => 'UK', 'count' => 20, 'total_amount' => 123.2];
// ..

Below is how opening balance can be built::
Expand Down
4 changes: 2 additions & 2 deletions docs/design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ Code to declare fields::

Code to access field values::

$order->set('amount', 1200.20);
$order->set('amount', 1200.2);

Domain Model Relationship
-------------------------
Expand Down Expand Up @@ -289,7 +289,7 @@ Each object is stored with some unique identifier, so you can load and store
object if you know it's ID::

$order = $order->load(20);
$order->set('amount', 1200.20);
$order->set('amount', 1200.2);
$order->save();


Expand Down
2 changes: 1 addition & 1 deletion docs/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Expression Callback

You can use a callback method when defining expression::

$m->addExpression('total_gross', ['expr' => function ($m, $q) {
$m->addExpression('total_gross', ['expr' => function (Model $m, Expression $q) {
return '[total_net] + [total_vat]';
}, 'type' => 'float']);

Expand Down
2 changes: 1 addition & 1 deletion docs/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ You can't use NULL or any value that is considered empty/false by PHP.
Some examples that are not allowed are:

- empty string ''
- 0 numerical value or 0.00
- 0 numerical value or 0.0
- boolean false

Example::
Expand Down
31 changes: 6 additions & 25 deletions docs/hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Example with beforeSave
The next code snippet demonstrates a basic usage of a `beforeSave` hook.
This one will update field values just before record is saved::

$m->onHook(Model::HOOK_BEFORE_SAVE, function ($m) {
$m->onHook(Model::HOOK_BEFORE_SAVE, function (Model $m) {
$m->set('name', strtoupper($m->get('name')));
$m->set('surname', strtoupper($m->get('surname')));
});
Expand Down Expand Up @@ -85,7 +85,7 @@ model will assume the operation was successful.

You can also break beforeLoad hook which can be used to skip rows::

$model->onHook(Model::HOOK_AFTER_LOAD, function ($m) {
$model->onHook(Model::HOOK_AFTER_LOAD, function (Model $m) {
if ($m->get('date') < $m->date_from) {
$m->breakHook(false); // will not yield such data row
}
Expand Down Expand Up @@ -136,7 +136,7 @@ of save.

You may actually drop validation exception inside save, insert or update hooks::

$m->onHook(Model::HOOK_BEFORE_SAVE, function ($m) {
$m->onHook(Model::HOOK_BEFORE_SAVE, function (Model $m) {
if ($m->get('name') === 'Yagi') {
throw new \Atk4\Data\ValidationException(['name' => "We don't serve like you"]);
}
Expand Down Expand Up @@ -184,33 +184,14 @@ Hook execution sequence

- afterSave (bool $isUpdate) [after insert or update, model is reloaded]

How to verify Updates
---------------------

The model is only being saved if any fields have been changed (dirty).
Sometimes it's possible that the record in the database is no longer available
and your update() may not actually update anything. This does not normally
generate an error, however if you want to actually make sure that update() was
effective, you can implement this through a hook::

$m->onHook(Persistence\Sql::HOOK_AFTER_UPDATE_QUERY, function ($m, $update, $c) {
if ($c === 0) {
throw (new \Atk4\Core\Exception('Update didn\'t affect any records'))
->addMoreInfo('query', $update->getDebugQuery())
->addMoreInfo('statement', $c)
->addMoreInfo('model', $m);
}
});


How to prevent actions
----------------------

In some cases you want to prevent default actions from executing.
Suppose you want to check 'memcache' before actually loading the record from
the database. Here is how you can implement this functionality::

$m->onHook(Model::HOOK_BEFORE_LOAD, function ($m, $id) {
$m->onHook(Model::HOOK_BEFORE_LOAD, function (Model $m, $id) {
$data = $m->getApp()->cacheFetch($m->table, $id);
if ($data) {
$dataRef = &$m->getDataRef();
Expand Down Expand Up @@ -238,15 +219,15 @@ This can be used in various situations.

Save information into auditLog about failure:

$m->onHook(Model::HOOK_ROLLBACK, function ($m) {
$m->onHook(Model::HOOK_ROLLBACK, function (Model $m) {
$m->auditLog->registerFailure();
});

Upgrade schema:

use Atk4\Data\Persistence\Sql\Exception as SqlException;

$m->onHook(Model::HOOK_ROLLBACK, function ($m, $exception) {
$m->onHook(Model::HOOK_ROLLBACK, function (Model $m, \Throwable $exception) {
if ($exception instanceof SqlException) {
$m->schema->upgrade();
$m->breakHook(false); // exception will not be thrown
Expand Down
4 changes: 2 additions & 2 deletions docs/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ To invoke code from `init()` methods of ALL models (for example soft-delete logi
you use Persistence's "afterAdd" hook. This will not affect ALL models but just models
which are associated with said persistence::

$db->onHook(Persistence::HOOK_AFTER_ADD, function ($p, $m) use ($acl) {
$db->onHook(Persistence::HOOK_AFTER_ADD, function (Persistence $p, Model $m) use ($acl) {
$fields = $m->getFields();

$acl->disableRestrictedFields($fields);
Expand Down Expand Up @@ -266,7 +266,7 @@ You must make sure that expression is valid for current `$this->getPersistence()
// new "total" field now contains complex logic, which is executed in SQL

$product->addCondition('total', '<', 10);
// filter products that cost less than 10.00 (including discount)
// filter products that cost less than 10.0 (including discount)


For the times when you are not working with SQL persistence, you can calculate field in PHP.
Expand Down
12 changes: 6 additions & 6 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ If your persistence does not support expressions (e.g. you are using Redis or
MongoDB), you would need to define the field differently::

$model->addField('gross');
$model->onHook(Model::HOOK_BEFORE_SAVE, function ($m) {
$model->onHook(Model::HOOK_BEFORE_SAVE, function (Model $m) {
$m->set('gross', $m->get('net') + $m->get('vat'));
});

Expand All @@ -183,7 +183,7 @@ you want it to work with NoSQL, then your solution might be::
} else {
// persistence does not support expressions
$model->addField('gross');
$model->onHook(Model::HOOK_BEFORE_SAVE, function ($m) {
$model->onHook(Model::HOOK_BEFORE_SAVE, function (Model $m) {
$m->set('gross', $m->get('net') + $m->get('vat'));
});
}
Expand Down Expand Up @@ -234,8 +234,8 @@ You can use :php:class:`Persistence\Static_` for that::
$htmltable->invokeInit();

$htmltable->setModel(new User(new Persistence\Static_([
['name' => 'John', 'is_admin' => false, 'salary' => 34400.00],
['name' => 'Peter', 'is_admin' => false, 'salary' => 42720.00],
['name' => 'John', 'is_admin' => false, 'salary' => 34_400.0],
['name' => 'Peter', 'is_admin' => false, 'salary' => 42_720.0],
])));

echo $htmltable->render();
Expand All @@ -247,8 +247,8 @@ model class to display VAT breakdown table::
$htmltable->invokeInit();

$htmltable->setModel(new Model(new Persistence\Static_([
['VAT_rate' => '12.0%', 'VAT' => '36.00', 'Net' => '300.00'],
['VAT_rate' => '10.0%', 'VAT' => '52.00', 'Net' => '520.00'],
['VAT_rate' => '12.0%', 'VAT' => 36.0, 'Net' => 300.0],
['VAT_rate' => '10.0%', 'VAT' => 52.0, 'Net' => 520.0],
])));

echo $htmltable->render();
Expand Down
16 changes: 8 additions & 8 deletions docs/persistence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ It's not only the 'type' property, but 'enum' can also imply restrictions::
There are also non-trivial types in Agile Data::

$m->addField('salary', ['type' => 'atk4_money']);
$m->set('salary', '20'); // converts to 20.00
$m->set('salary', 20); // converts to '20.00 EUR'

$m->addField('date', ['type' => 'date']);
$m->set('date', time()); // converts to DateTime class
Expand All @@ -164,7 +164,7 @@ Finally, you may create your own custom field types that follow a more
complex logic::

$m->add(new Field_Currency(), 'balance');
$m->set('balance', '12,200.00 EUR');
$m->set('balance', 12_200.0);

// May transparently work with 2 columns: 'balance_amount' and
// 'balance_currency_id' for example.
Expand Down Expand Up @@ -237,7 +237,7 @@ Multi-column fields
Lets talk more about this currency field::

$m->add(new Field_Currency(), 'balance');
$m->set('balance', '12,200.00 EUR');
$m->set('balance', 12_200.0);

It may be designed to split up the value by using two fields in the database:
`balance_amount` and `balance_currency_id`.
Expand Down Expand Up @@ -524,11 +524,11 @@ application::
$m = $m->withPersistence($this->mdb)->save();
}

$m->onHook(Model::HOOK_BEFORE_SAVE, function ($m) {
$m->onHook(Model::HOOK_BEFORE_SAVE, function (Model $m) {
$m->withPersistence($this->sql)->save();
});

$m->onHook(Model::HOOK_BEFORE_DELETE, function ($m) {
$m->onHook(Model::HOOK_BEFORE_DELETE, function (Model $m) {
$m->withPersistence($this->sql)->delete();
});

Expand Down Expand Up @@ -568,11 +568,11 @@ records.
The last two hooks are in order to replicate any changes into the SQL database
also::

$m->onHook(Model::HOOK_BEFORE_SAVE, function ($m) {
$m->onHook(Model::HOOK_BEFORE_SAVE, function (Model $m) {
$m->withPersistence($this->sql)->save();
});

$m->onHook(Model::HOOK_BEFORE_DELETE, function ($m) {
$m->onHook(Model::HOOK_BEFORE_DELETE, function (Model $m) {
$m->withPersistence($this->sql)->delete();
});

Expand Down Expand Up @@ -624,7 +624,7 @@ Archive Copies into different persistence
If you wish that every time you save your model the copy is also stored inside
some other database (for archive purposes) you can implement it like this::

$m->onHook(Model::HOOK_BEFORE_SAVE, function ($m) {
$m->onHook(Model::HOOK_BEFORE_SAVE, function (Model $m) {
$arc = $this->withPersistence($m->getApp()->archive_db);

// add some audit fields
Expand Down
2 changes: 1 addition & 1 deletion docs/references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ User-defined Reference
Sometimes you would want to have a different type of relation between models,
so with `addReference` you can define whatever reference you want::

$m->addReference('Archive', ['model' => function ($m) {
$m->addReference('Archive', ['model' => function (Model $m) {
return $m->newInstance(null, ['table' => $m->table . '_archive']);
}]);

Expand Down
2 changes: 1 addition & 1 deletion docs/typecasting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ to normalize value::
$m->addField('age', ['type' => 'integer']);
$m->addField('name', ['type' => 'string']);

$m->set('age', '49.80');
$m->set('age', '49.8');
$m->set('name', ' John');

echo $m->get('age'); // 49 - normalization cast value to integer
Expand Down
4 changes: 2 additions & 2 deletions docs/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ All measurements are implemented with :php:class:`Units` and can be further exte
$model->set('speed', '30km/s');

echo $model->get('speed'); // 30000
echo $model->getField('speed')->format(); // 30km/s
echo $model->getField('speed')->format('m'); // 30000m/s
echo $model->getField('speed')->format(); // 30 km/s
echo $model->getField('speed')->format('m'); // 30000 m/s

4 changes: 2 additions & 2 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ public function getTitles(): array

$field = $this->titleField && $this->hasField($this->titleField) ? $this->titleField : $this->idField;

return array_map(function ($row) use ($field) {
return array_map(function (array $row) use ($field) {
return $row[$field];
}, $this->export([$field], $this->idField));
}
Expand Down Expand Up @@ -1768,7 +1768,7 @@ public function getIterator(): \Traversable
}

// you can return false in afterLoad hook to prevent to yield this data row, example:
// $model->onHook(self::HOOK_AFTER_LOAD, static function ($m) {
// $model->onHook(self::HOOK_AFTER_LOAD, static function (Model $m) {
// if ($m->get('date') < $m->date_from) {
// $m->breakHook(false);
// }
Expand Down
2 changes: 1 addition & 1 deletion src/Model/UserActionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getUserActions(string $appliesTo = null): array
{
$this->assertIsModel();

return array_filter($this->userActions, function ($action) use ($appliesTo) {
return array_filter($this->userActions, function (UserAction $action) use ($appliesTo) {
return !$action->system && ($appliesTo === null || $action->appliesTo === $appliesTo);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public function export(Model $model, array $fields = null, bool $typecast = true
$data = $model->action('select', [$fields])->getRows();

if ($typecast) {
$data = array_map(function ($row) use ($model) {
$data = array_map(function (array $row) use ($model) {
return $this->typecastLoadRow($model, $row);
}, $data);
}
Expand Down Expand Up @@ -401,7 +401,7 @@ public function initAction(Model $model, array $fields = null): Action
}

if ($fields !== null) {
$rows = array_map(function ($row) use ($fields) {
$rows = array_map(function (array $row) use ($fields) {
return array_intersect_key($row, array_flip($fields));
}, $rows);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function saveHeader(Model $model): void
public function initializeHeader(array $header): void
{
// removes forbidden symbols from header (field names)
$this->header = array_map(function ($name) {
$this->header = array_map(function (string $name): string {
return preg_replace('~[^a-z0-9_-]+~i', '_', $name);
}, $header);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public function export(Model $model, array $fields = null, bool $typecast = true
$data = $model->action('select', [$fields])->getRows();

if ($typecast) {
$data = array_map(function ($row) use ($model) {
$data = array_map(function (array $row) use ($model) {
return $this->typecastLoadRow($model, $row);
}, $data);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Sql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,8 @@ protected function _renderGroup(): ?string
return '';
}

$g = array_map(function ($a) {
return $this->consume($a, self::ESCAPE_IDENTIFIER_SOFT);
$g = array_map(function ($v) {
return $this->consume($v, self::ESCAPE_IDENTIFIER_SOFT);
}, $this->args['group']);

return ' group by ' . implode(', ', $g);
Expand Down

0 comments on commit bd90320

Please sign in to comment.