Files
dockerfluxodecaixapro/tests/ModuleTestCase.php
T
2026-09-01 10:46:29 -03:00

122 lines
3.7 KiB
PHP
Executable File

<?php
namespace Tests;
use PHPUnit\Framework\TestCase;
use Kernel\Application;
use Illuminate\Database\Capsule\Manager as Capsule;
/**
* Base class for SysCMS Module Testing
*/
abstract class ModuleTestCase extends TestCase
{
protected Application $app;
protected string $moduleName = '';
protected function setUp(): void
{
parent::setUp();
// Ensure Application is initialized
if (Application::getInstance() === null) {
new Application();
}
$this->app = Application::getInstance();
}
/**
* Asserts that all translation keys are present in all 5 supported languages
*/
protected function assertTranslationsComplete(string $module): void
{
$languages = ['pt_BR', 'en_US', 'es_ES', 'de_DE', 'fr_FR'];
$basePath = dirname(__DIR__) . "/modules/{$module}/locales";
if (!is_dir($basePath)) {
$this->fail("Locales directory not found for module: {$module}");
}
$keys = [];
foreach ($languages as $lang) {
$file = "{$basePath}/{$lang}/{$module}.json";
if (!file_exists($file)) {
$this->fail("Translation file missing for language {$lang} in module {$module}");
}
$content = json_decode(file_get_contents($file), true);
$keys[$lang] = array_keys($this->flattenArray($content));
}
$referenceKeys = $keys['pt_BR'];
foreach ($languages as $lang) {
$diff = array_diff($referenceKeys, $keys[$lang]);
$this->assertEmpty($diff, "Missing translation keys in {$lang} for module {$module}: " . implode(', ', $diff));
}
}
/**
* Asserts that a route exists in the system
*/
protected function assertRouteExists(string $method, string $uri): void
{
$routes = $this->app->getRoutes();
$found = false;
foreach ($routes as $route) {
if ($route[0] === strtoupper($method) && $route[1] === $uri) {
$found = true;
break;
}
}
$this->assertTrue($found, "Route [{$method}] {$uri} not found.");
}
/**
* Verifies if a table exists and has specific data
*/
protected function assertDatabaseHas(string $table, array $data): void
{
$query = Capsule::table($table);
foreach ($data as $key => $value) {
$query->where($key, $value);
}
$this->assertGreaterThan(0, $query->count(), "Data not found in table {$table}");
}
/**
* Verifies if a table does not have specific data
*/
protected function assertDatabaseMissing(string $table, array $data): void
{
$query = Capsule::table($table);
foreach ($data as $key => $value) {
$query->where($key, $value);
}
$this->assertEquals(0, $query->count(), "Data found in table {$table} when it should be missing");
}
/**
* Verifies the count of records in a table
*/
protected function assertDatabaseCount(string $table, int $count): void
{
$this->assertEquals($count, Capsule::table($table)->count(), "Table {$table} does not have {$count} records");
}
/**
* Helper to flatten nested translation arrays
*/
private function flattenArray(array $array, string $prefix = ''): array
{
$result = [];
foreach ($array as $key => $value) {
$newKey = $prefix . (empty($prefix) ? '' : '.') . $key;
if (is_array($value)) {
$result = array_merge($result, $this->flattenArray($value, $newKey));
} else {
$result[$newKey] = $value;
}
}
return $result;
}
}