Skip to content

Commit

Permalink
fix tests NO_CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Eichhorn committed May 17, 2024
1 parent 2ea1d03 commit 8f96fe3
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 52 deletions.
22 changes: 22 additions & 0 deletions DataStorage/Database/BuilderAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,26 @@ abstract public function toSql() : string;
* @since 1.0.0
*/
abstract public function execute() : ?\PDOStatement;

/**
* Get bind parameter type.
*
* @param mixed $value Value to bind
*
* @return int
*
* @throws \Exception
*
* @since 1.0.0
*/
public static function getBindParamType(mixed $value) : int
{
if (\is_int($value)) {
return \PDO::PARAM_INT;
} elseif (\is_string($value) || \is_float($value)) {
return \PDO::PARAM_STR;
}

throw new \Exception();
}
}
22 changes: 0 additions & 22 deletions DataStorage/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1423,28 +1423,6 @@ public function prepare() : ?\PDOStatement
return $sth;
}

/**
* Get bind parameter type.
*
* @param mixed $value Value to bind
*
* @return int
*
* @throws \Exception
*
* @since 1.0.0
*/
public static function getBindParamType(mixed $value) : int
{
if (\is_int($value)) {
return \PDO::PARAM_INT;
} elseif (\is_string($value) || \is_float($value)) {
return \PDO::PARAM_STR;
}

throw new \Exception();
}

/**
* Get column name
*
Expand Down
56 changes: 53 additions & 3 deletions DataStorage/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public function addConstraint(string $key, string $foreignTable, string $foreign
/**
* {@inheritdoc}
*/
public function execute() : ?\PDOStatement
public function prepare() : ?\PDOStatement
{
$sth = null;
$sql = '';
Expand All @@ -389,7 +389,49 @@ public function execute() : ?\PDOStatement
return null;
}

$sth->execute();
foreach ($this->binds as $key => $bind) {
if (!isset($bind['type'])) {
$bind['type'] = self::getBindParamType($bind['value']);
}

$sth->bindParam(\is_int($key) ? $key + 1 : $key, $bind['value'], $bind['type']);
}
} catch (\Throwable $t) {
// @codeCoverageIgnoreStart
\phpOMS\Log\FileLogger::getInstance()->error(
\phpOMS\Log\FileLogger::MSG_FULL, [
'message' => $t->getMessage() . ':' . $sql,
'line' => __LINE__,
'file' => self::class,
]
);

\phpOMS\Log\FileLogger::getInstance()->error(
\phpOMS\Log\FileLogger::MSG_FULL, [
'message' => \json_encode($this->binds),
'line' => __LINE__,
'file' => self::class,
]
);

$sth = null;
// @codeCoverageIgnoreEnd
}

return $sth;
}

/**
* {@inheritdoc}
*/
public function execute() : ?\PDOStatement
{
$sth = null;
try {
$sth = $this->prepare();
if ($sth !== null) {
$sth->execute();
}

if ($this->hasPostQuery) {
$sqls = $this->grammar->compilePostQueries($this);
Expand All @@ -402,7 +444,15 @@ public function execute() : ?\PDOStatement
// @codeCoverageIgnoreStart
\phpOMS\Log\FileLogger::getInstance()->error(
\phpOMS\Log\FileLogger::MSG_FULL, [
'message' => $t->getMessage() . ':' . $sql,
'message' => $t->getMessage() . ':' . $this->toSql(),
'line' => __LINE__,
'file' => self::class,
]
);

\phpOMS\Log\FileLogger::getInstance()->error(
\phpOMS\Log\FileLogger::MSG_FULL, [
'message' => \json_encode($this->binds),
'line' => __LINE__,
'file' => self::class,
]
Expand Down
54 changes: 27 additions & 27 deletions tests/Utils/IO/Spreadsheet/SpreadsheetDatabaseMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ protected function tearDown() : void
#[\PHPUnit\Framework\Attributes\TestDox('Data can be inserted into a database from an ods files')]
public function testInsertOds() : void
{
$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods');
$mapper->insert();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->import(__DIR__ . '/insert.ods', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand Down Expand Up @@ -104,8 +104,8 @@ public function testInsertOds() : void
#[\PHPUnit\Framework\Attributes\TestDox('Data can be inserted into a database from a xls files')]
public function testInsertXls() : void
{
$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls');
$mapper->insert();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->import(__DIR__ . '/insert.xls', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand Down Expand Up @@ -136,8 +136,8 @@ public function testInsertXls() : void
#[\PHPUnit\Framework\Attributes\TestDox('Data can be inserted into a database from a xlsx files')]
public function testInsertXlsx() : void
{
$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx');
$mapper->insert();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->import(__DIR__ . '/insert.xlsx', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand Down Expand Up @@ -168,8 +168,8 @@ public function testInsertXlsx() : void
#[\PHPUnit\Framework\Attributes\TestDox('Data can be updated in a database from an ods files')]
public function testUpdateOds() : void
{
$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods');
$mapper->insert();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->import(__DIR__ . '/insert.ods', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand All @@ -195,8 +195,8 @@ public function testUpdateOds() : void
$data
);

$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/update.ods');
$mapper->update();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->update(__DIR__ . '/update.ods', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand Down Expand Up @@ -227,8 +227,8 @@ public function testUpdateOds() : void
#[\PHPUnit\Framework\Attributes\TestDox('Data can be updated in a database from a xls files')]
public function testUpdateXls() : void
{
$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls');
$mapper->insert();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->import(__DIR__ . '/insert.xls', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand All @@ -254,8 +254,8 @@ public function testUpdateXls() : void
$data
);

$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/update.xls');
$mapper->update();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->update(__DIR__ . '/update.xls', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand Down Expand Up @@ -286,8 +286,8 @@ public function testUpdateXls() : void
#[\PHPUnit\Framework\Attributes\TestDox('Data can be updated in a database from a xlsx files')]
public function testUpdateXlsx() : void
{
$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx');
$mapper->insert();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->import(__DIR__ . '/insert.xlsx', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand All @@ -313,8 +313,8 @@ public function testUpdateXlsx() : void
$data
);

$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/update.xlsx');
$mapper->update();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->update(__DIR__ . '/update.xlsx', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand Down Expand Up @@ -349,8 +349,8 @@ public function testSelectOds() : void
\unlink(__DIR__ . '/select.ods');
}

$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods');
$mapper->insert();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->import(__DIR__ . '/insert.ods', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand Down Expand Up @@ -381,7 +381,7 @@ public function testSelectOds() : void
$builder = new Builder($this->sqlite, true);
$data = $builder->select('int', 'decimal', 'bool', 'varchar', 'datetime')->from('insert_1');

$mapper->select([$builder]);
$mapper->export(__DIR__ . '/select.ods', [$builder]);

self::assertTrue($this->compareSelectInsertSheet(__DIR__ . '/select.ods', __DIR__ . '/insert.ods'));

Expand All @@ -398,8 +398,8 @@ public function testSelectXls() : void
\unlink(__DIR__ . '/select.xls');
}

$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls');
$mapper->insert();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->import(__DIR__ . '/insert.xls', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand Down Expand Up @@ -430,7 +430,7 @@ public function testSelectXls() : void
$builder = new Builder($this->sqlite, true);
$data = $builder->select('int', 'decimal', 'bool', 'varchar', 'datetime')->from('insert_1');

$mapper->select([$builder]);
$mapper->export(__DIR__ . '/select.xls', [$builder]);

self::assertTrue($this->compareSelectInsertSheet(__DIR__ . '/select.xls', __DIR__ . '/insert.xls'));

Expand All @@ -447,8 +447,8 @@ public function testSelectXlsx() : void
\unlink(__DIR__ . '/select.xlsx');
}

$mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx');
$mapper->insert();
$mapper = new SpreadsheetDatabaseMapper($this->sqlite);
$mapper->import(__DIR__ . '/insert.xlsx', 'insert_1');

$builder = new Builder($this->sqlite, true);
$data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? [];
Expand Down Expand Up @@ -479,7 +479,7 @@ public function testSelectXlsx() : void
$builder = new Builder($this->sqlite, true);
$data = $builder->select('int', 'decimal', 'bool', 'varchar', 'datetime')->from('insert_1');

$mapper->select([$builder]);
$mapper->export(__DIR__ . '/select.xlsx', [$builder]);

self::assertTrue($this->compareSelectInsertSheet(__DIR__ . '/select.xlsx', __DIR__ . '/insert.xlsx'));

Expand Down

0 comments on commit 8f96fe3

Please sign in to comment.