<?php

use Laravel\Lumen\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\DB;
use PHPUnit\Framework\Assert as PHPUnit;
abstract class TestCase extends BaseTestCase
{
    /**
     * Creates the application.
     *
     * @return \Laravel\Lumen\Application
     */
    public function createApplication()
    {
        return require __DIR__.'/../bootstrap/app.php';
    }

protected function assertDatabaseHasThroughNorujukan(string $table, array $data, string $connection = null): void
{
    $query = DB::connection($connection)->table($table);

    foreach ($data as $key => $value) {
        $query->where($key, $value);
    }

    $record = $query->first();

    if (!$record) {
        // Check if a record exists but doesn't match the expected attributes
        $actualRecord = DB::connection($connection)->table($table)->where('norujukan', $data['norujukan'])->first();

        if ($actualRecord) {
            $mismatchedColumns = [];
            foreach ($data as $key => $value) {
                if ($actualRecord->$key !== $value) {
                    $expectedValue = stripslashes($value); // Remove backslashes from expected
                    $mismatchedColumns[$key] = [                        
                        'expected' => $expectedValue,
                        'actual' => $actualRecord->$key,
                    ];
                }
            }

            PHPUnit::fail(
                "A record exists in the table [{$table}] but does not match the expected attributes. " .
                "Mismatched columns: " . json_encode($mismatchedColumns)
            );
        }

        PHPUnit::fail(
            "Failed asserting that a record exists in the table [{$table}] with the attributes " . json_encode($data)
        );
    }

    PHPUnit::assertNotNull($record, "Record exists and matches the expected attributes.");
}
}
