Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix strict types in App::getTag() #1541

Merged
merged 13 commits into from
Dec 13, 2020
6 changes: 5 additions & 1 deletion src/Persistence/Ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Ui extends \Atk4\Data\Persistence

/**
* This method contains the logic of casting generic values into user-friendly format.
*
* @return string|string[]
*/
public function _typecastSaveField(\Atk4\Data\Field $f, $value)
{
Expand Down Expand Up @@ -103,7 +105,9 @@ public function _typecastSaveField(\Atk4\Data\Field $f, $value)
break;
}

return $value;
return is_array($value)
? array_map(function ($v) { return (string) $v; }, $value)
: (string) $value;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ public function setModel(Model $model)
*/
public function setSource(array $data, $fields = null)
{
// ID with zero value is not supported, fix the data
if (isset($data[0])) {
Copy link
Member

@mvorisek mvorisek Dec 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@georgehristov because of atk4/data#806 when validation is triggered.

I am not sure with this. We can switch to mandatory or allow zero IDs for non SQL persistences only.

Do you know, if zero as ID is actually a problem with some DB engine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to reproduce http://sqlfiddle.com/#!9/d28469/1 - at least with MySQL zero is understood as empty ID and AI is applied.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@mvorisek mvorisek Dec 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with last fix I think we can keep "required" (and thus reject zero IDs)

/cc @DarkSide666 @georgehristov

$oldData = $data;
$data = [];
foreach ($oldData as $k => $row) {
$data['_setSource_' . $k] = $row;
}
}

$this->setModel(new Model(new Static_($data)), $fields);
$this->model->getField($this->model->id_field)->type = null; // TODO probably unwanted

Expand Down
32 changes: 22 additions & 10 deletions tests/DemosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,29 +237,41 @@ public function demoFilesProvider(): array
$excludeDirs = ['_demo-data', '_includes', '_unit-test', 'special'];
$excludeFiles = ['layout/layouts_error.php'];

// these tests require SessionTrait, more precisely session_start() which we do not support in non-HTTP testing
if (static::class === self::class) {
$excludeFiles[] = 'collection/tablefilter.php';
$excludeFiles[] = 'interactive/popup.php';
}

$files = [];
$files[] = ['index.php'];
$files[] = 'index.php';
foreach (array_diff(scandir(static::DEMOS_DIR), ['.', '..'], $excludeDirs) as $dir) {
if (!is_dir(static::DEMOS_DIR . '/' . $dir)) {
continue;
}

foreach (scandir(static::DEMOS_DIR . '/' . $dir) as $f) {
if (substr($f, -4) !== '.php' || in_array($dir . '/' . $f, $excludeFiles, true)) {
$path = $dir . '/' . $f;
if (substr($path, -4) !== '.php' || in_array($path, $excludeFiles, true)) {
continue;
}

$files[] = [$dir . '/' . $f];
$files[] = $path;
}
}

return $files;
// these tests require SessionTrait, more precisely session_start() which we do not support in non-HTTP testing
// always move these tests to the end, so data provider # stays same as much as possible across tests for fast skip
$httpOnlyFiles = ['collection/tablefilter.php', 'interactive/popup.php'];
foreach ($files as $k => $path) {
if (in_array($path, $httpOnlyFiles, true)) {
unset($files[$k]);
$files[] = $path;
}
}
if (static::class === self::class) {
foreach ($files as $k => $path) {
if (in_array($path, $httpOnlyFiles, true)) {
unset($files[$k]);
}
}
}

return array_map(function (string $v) { return [$v]; }, $files);
}

/**
Expand Down