Skip to content

Commit

Permalink
Tests namespaced
Browse files Browse the repository at this point in the history
  • Loading branch information
dergel committed Sep 9, 2024
1 parent e2e4479 commit 1d1a435
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</php>
<testsuites>
<testsuite name="yorm">
<directory suffix="_test.php">tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
75 changes: 36 additions & 39 deletions tests/rex_yform_yorm_test.php → tests/Manager/Table/ApiTest.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
<?php

namespace yform\tests;
namespace Yakamara\YForm\Manager\Table;

use Exception;
use PHPUnit\Framework\TestCase;
use rex_sql;
use rex_yform_manager_dataset;
use rex_yform_manager_field;
use rex_yform_manager_table;

use rex_yform_manager_table_api;
use Yakamara\YForm\Manager\Dataset;
use Yakamara\YForm\Manager\Field;

use function count;

/**
* @internal
*/
class rex_yform_yorm_test extends TestCase
class ApiTest extends TestCase
{
public static function setUpTable($tableName)
{
$table = \Yakamara\YForm\Manager\Table\Api::setTable(
$table = Api::setTable(
[
'table_name' => $tableName,
'name' => 'Name of Table - ' . $tableName,
Expand All @@ -39,7 +36,7 @@ public static function setUpTable($tableName)
return $table;
}

public static function setUpTableField(\Yakamara\YForm\Manager\Table\Table $table, $field)
public static function setUpTableField(Table $table, $field)
{
$field['type_id'] ??= 'value';
$field['type_name'] ??= 'text';
Expand All @@ -49,8 +46,8 @@ public static function setUpTableField(\Yakamara\YForm\Manager\Table\Table $tabl
$field['search'] = $field['list_hidden'] ?? 0;
$field['prio'] = $field['list_hidden'] ?? 99999;

\Yakamara\YForm\Manager\Table\Api::setTableField($table->getTableName(), $field);
\Yakamara\YForm\Manager\Table\Api::generateTableAndFields($table);
Api::setTableField($table->getTableName(), $field);
Api::generateTableAndFields($table);
}

public function testTableAPI()
Expand All @@ -61,10 +58,10 @@ public function testTableAPI()
$tableNameRelation = $prefix . 'related';

$table = self::setUpTable($tableName);
static::assertEquals(
self::assertEquals(
$table::class,
'rex_yform_manager_table',
'table creation failed. (rex_yform_manager_table_api::setTable)',
'Yakamara\YForm\Manager\Table\Table',
'table creation failed. (\Yakamara\YForm\Manager\Table\Api::setTable)',
);

if ($table) {
Expand All @@ -78,25 +75,25 @@ public function testTableAPI()

// prüfen ob es angelegt ist.

$fields = \Yakamara\YForm\Manager\Table\Table::get($tableName)->getFields();
static::assertEquals(
$fields = Table::get($tableName)->getFields();
self::assertEquals(
count($fields),
1,
'field creation failed (rex_yform_manager_table_api::setTableField)',
);

if (1 == count($fields)) {
static::assertEquals($fields[0]->getName(), $fieldName, 'fieldname validation failed');
self::assertEquals($fields[0]->getName(), $fieldName, 'fieldname validation failed');
}

// TODO: prüfen ob field gelöscht werden kann

// YORM - Datensatz anlegen
$dataset = \Yakamara\YForm\Manager\Dataset::create($tableName);
$dataset = Dataset::create($tableName);
$dataset->setValue($fieldName, $fieldValue);

static::assertTrue($dataset->save(), 'dataset creation failed (rex_yform_manager_dataset::create)');
static::assertEquals(
self::assertTrue($dataset->save(), 'dataset creation failed (rex_yform_manager_dataset::create)');
self::assertEquals(
count($dataset->getMessages()),
0,
'dataset creation failed with Messages: ' . implode(',', $dataset->getMessages()),
Expand All @@ -106,31 +103,31 @@ public function testTableAPI()
$SQLDatasets = rex_sql::factory()->getArray(
'select * from ' . $tableName,
);
static::assertEquals(count($SQLDatasets), 1, 'dataset not found - creation failed (SQL Test)');
self::assertEquals(count($SQLDatasets), 1, 'dataset not found - creation failed (SQL Test)');

// YORM - Datensatz auslesen
$datasetId = $dataset->getId();
$dataset = \Yakamara\YForm\Manager\Dataset::get($datasetId, $tableName);
static::assertNotNull($dataset, 'dataset not found - get via ID failed');
$dataset = Dataset::get($datasetId, $tableName);
self::assertNotNull($dataset, 'dataset not found - get via ID failed');

// YORM - Datensatz bearbeiten
$fieldValueEdit = $fieldValue . ' overwrite';
$dataset
->setValue($fieldName, $fieldValueEdit)
->save();
$dataset = \Yakamara\YForm\Manager\Dataset::get($datasetId, $tableName);
static::assertEquals(
$dataset = Dataset::get($datasetId, $tableName);
self::assertEquals(
$dataset->getValue($fieldName),
$fieldValueEdit,
'dataset update failes - YOrm update failed',
);

// YORM - Datensatz löschen
if ($dataset) {
static::assertTrue($dataset->delete(), 'dataset delete failed (rex_yform_manager_dataset::delete)');
self::assertTrue($dataset->delete(), 'dataset delete failed (rex_yform_manager_dataset::delete)');

$dataset = \Yakamara\YForm\Manager\Dataset::get($datasetId, $tableName);
static::assertFalse($dataset->exists(), 'dataset delete failed - YOrm delete failed');
$dataset = Dataset::get($datasetId, $tableName);
self::assertFalse($dataset->exists(), 'dataset delete failed - YOrm delete failed');
}

// TODO - YORM - Relationen mit Relationstabelle prüfen
Expand Down Expand Up @@ -187,23 +184,23 @@ public function testTableAPI()
'size' => 10,
]);

$cat1 = \Yakamara\YForm\Manager\Dataset::create($tableNameCategories)
$cat1 = Dataset::create($tableNameCategories)
->setValue($fieldName, 'Category 1');
$cat1->save();

$cat2 = \Yakamara\YForm\Manager\Dataset::create($tableNameCategories)
$cat2 = Dataset::create($tableNameCategories)
->setValue($fieldName, 'Category 2');
$cat2->save();

$cat3 = \Yakamara\YForm\Manager\Dataset::create($tableNameCategories)
$cat3 = Dataset::create($tableNameCategories)
->setValue($fieldName, 'Category 3');
$cat3->save();

\Yakamara\YForm\Manager\Dataset::create($tableName)
Dataset::create($tableName)
->setValue($fieldName, 'Mein Neuer Wert')
->save();

\Yakamara\YForm\Manager\Dataset::create($tableName)
Dataset::create($tableName)
->setValue($fieldName, 'Mein Neuer Wert mit 2 Kategorien')
->setValue('categories', [
$cat1->getId(),
Expand All @@ -213,11 +210,11 @@ public function testTableAPI()
}
}

\Yakamara\YForm\Manager\Table\Api::removeTable($tableName);
Api::removeTable($tableName);

$table = \Yakamara\YForm\Manager\Table\Table::get($tableName);
$table = Table::get($tableName);

static::assertNull($table, 'table schema removing failed');
self::assertNull($table, 'table schema removing failed');

// Cleanup
try {
Expand All @@ -231,19 +228,19 @@ public function testTableAPI()
'DROP Table ' . $tableNameRelation,
);
rex_sql::factory()->setQuery(
'delete from ' . \Yakamara\YForm\Manager\Table\Table::table() . ' where table_name LIKE :table_name ',
'delete from ' . Table::table() . ' where table_name LIKE :table_name ',
[
':table_name' => $prefix . '%',
],
);
rex_sql::factory()->setQuery(
'delete from ' . \Yakamara\YForm\Manager\Field::table() . ' where table_name LIKE :table_name ',
'delete from ' . Field::table() . ' where table_name LIKE :table_name ',
[
':table_name' => $prefix . '%',
],
);

\Yakamara\YForm\Manager\Table\Table::deleteCache();
Table::deleteCache();
} catch (Exception $e) {
}
}
Expand Down

0 comments on commit 1d1a435

Please sign in to comment.