Skip to content

Commit

Permalink
Fix failing test with MySQL 8.0.32 (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 20, 2023
1 parent aacc247 commit 5b4005d
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 32 deletions.
35 changes: 15 additions & 20 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,58 +218,53 @@ Agile Data makes it very easy to use models with a simpler persistencies.
For example, consider you want to output a "table" to the user using HTML by
using Agile UI::

$htmltable = new \Atk4\Ui\Table();
$htmltable->invokeInit();
$table = \Atk4\Ui\Table::addTo($app);

$htmltable->setModel(new User($db));
$table->setModel(new User($db));

echo $htmltable->render();
echo $table->render();

Class `\\Atk4\\Ui\\Table` here is designed to work with persistencies and models -
it will populate columns of correct type, fetch data, calculate totals if needed.
But what if you have your data inside an array?
You can use :php:class:`Persistence\Static_` for that::

$htmltable = new \Atk4\Ui\Table();
$htmltable->invokeInit();
$table = \Atk4\Ui\Table::addTo($app);

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

echo $htmltable->render();
echo $table->render();

Even if you don't have a model, you can use Static persistence with Generic
model class to display VAT breakdown table::

$htmltable = new \Atk4\Ui\Table();
$htmltable->invokeInit();
$table = \Atk4\Ui\Table::addTo($app);

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

echo $htmltable->render();
echo $table->render();

It can be made even simpler::

$htmltable = new \Atk4\Ui\Table();
$htmltable->invokeInit();
$table = \Atk4\Ui\Table::addTo($app);

$htmltable->setModel(new Model(new Persistence\Static_([
$table->setModel(new Model(new Persistence\Static_([
'John',
'Peter',
])));

echo $htmltable->render();
echo $table->render();

Agile UI even offers a wrapper for static persistence::

$htmltable = new \Atk4\Ui\Table();
$htmltable->invokeInit();
$table = \Atk4\Ui\Table::addTo($app);

$htmltable->setSource([ 'John', 'Peter' ]);
$table->setSource([ 'John', 'Peter' ]);

echo $htmltable->render();
echo $table->render();
2 changes: 1 addition & 1 deletion docs/static.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Usage
This is most useful when working with "sample" code, where you want to see your
results quick::

$htmltable->setModel(new Model(new Persistence\Static_([
$table->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'],
])));
Expand Down
2 changes: 1 addition & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ public function setPersistence(Persistence $persistence)
$this->assertIsModel();

if ($this->issetPersistence()) {
throw new Exception('Persistence already set');
throw new Exception('Persistence is already set');
}

if ($this->persistenceData === []) {
Expand Down
5 changes: 3 additions & 2 deletions src/Persistence/Sql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ public static function normalizeDsn($dsn, $user = null, $password = null)
}
if (isset($dsn['dsn'])) {
if (str_contains($dsn['dsn'], '://')) {
/** @var array<string, string> https://github.com/phpstan/phpstan/issues/8638 */
$parsed = array_filter(parse_url($dsn['dsn']));
$dsn['dsn'] = str_replace('-', '_', $parsed['scheme']) . ':';
unset($parsed['scheme']);
foreach ($parsed as $k => $v) {
if ($k === 'pass') { // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/8638
if ($k === 'pass') {
unset($parsed[$k]);
$k = 'password';
} elseif ($k === 'path') { // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/8638
} elseif ($k === 'path') {
unset($parsed[$k]);
$k = 'dbname';
$v = preg_replace('~^/~', '', $v);
Expand Down
2 changes: 1 addition & 1 deletion src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static function (Model $model) use ($name): self {
protected function onHookToTheirModel(Model $model, string $spot, \Closure $fx, array $args = [], int $priority = 5): int
{
if ($model->ownerReference !== null && $model->ownerReference !== $this) {
throw new Exception('Model owner reference unexpectedly already set');
throw new Exception('Model owner reference is unexpectedly already set');
}
$model->ownerReference = $this;
$getThisFx = static function (Model $model) {
Expand Down
2 changes: 1 addition & 1 deletion src/Type/LocalObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class LocalObjectType extends DbalTypes\Type
/** @var array<int, \WeakReference<LocalObjectHandle>> */
private array $handlesIndex;

protected function __clone()
private function __clone()
{
// prevent clonning
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Field/EmailFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public function testEmailValidateDns(): void
{
$m = new Model();
$m->addField('email', [EmailField::class, 'dnsCheck' => true]);
$m->addField('email_idn', [EmailField::class, 'dnsCheck' => true]);
$entity = $m->createEntity();

$entity->set('email', ' foo@gmail.com');
static::assertSame('foo@gmail.com', $entity->get('email'));

$entity->set('email_idn', 'test@háčkyčárky.cz'); // official IDN test domain
static::assertSame('test@háčkyčárky.cz', $entity->get('email_idn'));
$entity->set('email', 'test@háčkyčárky.cz'); // official IDN test domain
static::assertSame('test@háčkyčárky.cz', $entity->get('email'));

$this->expectException(ValidationException::class);
$this->expectExceptionMessage('domain does not exist');
$entity->set('email', ' foo@lrcanoetuhasnotdusantotehusontehuasntddaontehudnouhtd.com');
$entity->set('email', 'foo@lrcanoetuhasnotdusantotehusontehuasntddaontehudnouhtd.com');
}

public function testEmailWithName(): void
Expand Down
8 changes: 8 additions & 0 deletions tests/Persistence/Sql/WithDb/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ public function testUtf8mb4Support(): void
$tableAlias = '名';
}

// remove once https://bugs.mysql.com/bug.php?id=109699 is fixed
if ($this->getDatabasePlatform() instanceof MySQLPlatform) {
$serverVersion = $this->getConnection()->getConnection()->getWrappedConnection()->getServerVersion(); // @phpstan-ignore-line
if ($serverVersion === '8.0.32') {
static::markTestIncomplete('MySQL Server 8.0.32 optimizer is broken');
}
}

static::assertSame(
[$columnAlias => 'žlutý_😀'],
$this->q(
Expand Down
2 changes: 1 addition & 1 deletion tests/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function testSetPersistence(): void
static::assertTrue($pAddCalled);

$this->expectException(Exception::class);
$this->expectExceptionMessage('Persistence already set');
$this->expectExceptionMessage('Persistence is already set');
$m->setPersistence($p);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ReferenceSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testBasic2(): void
if ($this->getDatabasePlatform() instanceof MySQLPlatform) {
$serverVersion = $this->getConnection()->getConnection()->getWrappedConnection()->getServerVersion(); // @phpstan-ignore-line
if (preg_match('~^5\.6~', $serverVersion)) {
static::markTestIncomplete('TODO MySQL: Unique key exceed max key (767 bytes) length');
static::markTestIncomplete('TODO MySQL 5.6: Unique key exceed max key (767 bytes) length');
}
}
$this->markTestIncompleteWhenCreateUniqueIndexIsNotSupportedByPlatform();
Expand Down

0 comments on commit 5b4005d

Please sign in to comment.