Skip to content

Commit

Permalink
tests updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Apr 15, 2026
1 parent 9ffc7ca commit fa5fe53
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 175 deletions.
65 changes: 25 additions & 40 deletions app/tests/TestCase/ApplicationTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

/**
Expand All @@ -14,73 +15,57 @@
* @since 3.3.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace App\Test\TestCase;

use App\Application;
use Cake\Core\Configure;
use Cake\Error\Middleware\ErrorHandlerMiddleware;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\Middleware\AssetMiddleware;
use Cake\Routing\Middleware\RoutingMiddleware;
use Cake\TestSuite\IntegrationTestCase;
use InvalidArgumentException;
use Cake\TestSuite\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* ApplicationTest class
*/
class ApplicationTest extends IntegrationTestCase
#[CoversClass(Application::class)]
final class ApplicationTest extends TestCase
{
/**
* testBootstrap
*
* @return void
*/
public function testBootstrap()
public function testBootstrap(): void
{
$app = new Application(dirname(dirname(__DIR__)) . '/config');
Configure::write('debug', false);

$app = new Application(dirname(__DIR__, 2) . '/config');
$app->bootstrap();
$plugins = $app->getPlugins();

$this->assertCount(3, $plugins);
$this->assertSame('Bake', $plugins->get('Bake')->getName());
$this->assertSame('DebugKit', $plugins->get('DebugKit')->getName());
$this->assertSame('Migrations', $plugins->get('Migrations')->getName());
$this->assertTrue($plugins->has('Bake'), 'plugins has Bake?');
$this->assertFalse($plugins->has('DebugKit'), 'plugins has DebugKit?');
$this->assertTrue($plugins->has('Migrations'), 'plugins has Migrations?');
}

/**
* testBootstrapPluginWitoutHalt
*
* @return void
*/
public function testBootstrapPluginWithoutHalt()
public function testBootstrapInDebug(): void
{
$this->expectException(InvalidArgumentException::class);

$app = $this->getMockBuilder(Application::class)
->setConstructorArgs([dirname(dirname(__DIR__)) . '/config'])
->onlyMethods(['addPlugin'])
->getMock();

$app->method('addPlugin')
->will($this->throwException(new InvalidArgumentException('test exception.')));
Configure::write('debug', true);

$app = new Application(dirname(__DIR__, 2) . '/config');
$app->bootstrap();
$plugins = $app->getPlugins();

$this->assertTrue($plugins->has('DebugKit'), 'plugins has DebugKit?');
}

/**
* testMiddleware
*
* @return void
*/
public function testMiddleware()
public function testMiddleware(): void
{
$app = new Application(dirname(dirname(__DIR__)) . '/config');
$middleware = new MiddlewareQueue();
$app = new Application(dirname(__DIR__, 2) . '/config');

$middleware = new MiddlewareQueue();
$middleware = $app->middleware($middleware);

$this->assertInstanceOf(ErrorHandlerMiddleware::class, $middleware->current());

$middleware->seek(1);
$this->assertInstanceOf(AssetMiddleware::class, $middleware->current());

$middleware->seek(2);
$this->assertInstanceOf(RoutingMiddleware::class, $middleware->current());
}
Expand Down
66 changes: 66 additions & 0 deletions app/tests/TestCase/Command/RegistrySetupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace App\Test\TestCase\Command;

use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

#[Group('registry-setup')]
#[CoversClass('SetupCommand')]
final class RegistrySetupTest extends TestCase
{
use ConsoleIntegrationTestTrait;

public function testRegistrySetupRunsSuccessfully(): void
{
$appDir = dirname(__DIR__, 3);

$adminGiven = getenv('COMANAGE_REGISTRY_ADMIN_GIVEN_NAME') ?: 'Admin';
$adminFamily = getenv('COMANAGE_REGISTRY_ADMIN_FAMILY_NAME') ?: 'User';
$adminUser = getenv('COMANAGE_REGISTRY_ADMIN_USERNAME') ?: 'admin';

// The test bootstrap already sets the Security salt and prepares the test DB schema.
// Here we only validate that the setup command can run successfully.
$this->exec(sprintf(
'setup --admin-given-name %s --admin-family-name %s --admin-username %s',
$adminGiven,
$adminFamily,
$adminUser,
));
$this->assertExitCode(0, 'bin/cake setup should exit successfully');

$connection = ConnectionManager::get('default');

// Verify we can talk to the DB and it has tables.
$tables = $connection->getSchemaCollection()->listTables();
$this->assertNotEmpty($tables, 'Expected at least one table in the test database');

// Assert schema.json tables exist in the DB.
$schemaFile = $appDir . '/config/schema/schema.json';
$this->assertFileExists($schemaFile, 'Expected schema.json to exist at config/schema/schema.json');

$json = file_get_contents($schemaFile);
$this->assertNotFalse($json, 'Failed to read schema file: ' . $schemaFile);

$data = json_decode($json, true);
$this->assertIsArray($data, 'schema.json did not decode into an array');
$this->assertArrayHasKey('tables', $data, 'schema.json is missing the "tables" key');
$this->assertIsArray($data['tables'], 'schema.json "tables" should be an object/map');

$expectedTables = array_keys($data['tables']);
$missing = array_values(array_diff($expectedTables, $tables));

$this->assertSame(
[],
$missing,
"Some tables from config/schema/schema.json are missing in the database.\n"
. 'Missing: ' . implode(', ', $missing) . "\n"
. 'DB_ENGINE=' . (getenv('DB_ENGINE') ?: '(not set)')
);
}
}
98 changes: 98 additions & 0 deletions app/tests/TestCase/Controller/MostlyStaticPagesControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace App\Test\TestCase\Controller;

use App\Lib\Enum\PageContextEnum;
use App\Lib\Enum\SuspendableStatusEnum;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;

#[CoversClass('MostlyStaticPagesController')]
final class MostlyStaticPagesControllerTest extends TestCase
{
use IntegrationTestTrait;

public static function defaultPagesProvider(): array
{
return [
['duplicate-landing', PageContextEnum::EnrollmentHandoff],
['default-handoff', PageContextEnum::EnrollmentHandoff],
['error-landing', PageContextEnum::ErrorLanding],
['mfa-required', PageContextEnum::ErrorLanding],
['petition-complete', PageContextEnum::EnrollmentHandoff],
];
}

public static function defaultPageSlugsProvider(): array
{
return [
['duplicate-landing'],
['default-handoff'],
['error-landing'],
['mfa-required'],
['petition-complete'],
];
}

private function getComanageCoId(): int
{
$Cos = TableRegistry::getTableLocator()->get('Cos');
$co = $Cos->find('COmanageCO')->firstOrFail();

return (int)$co->id;
}

private function assertDefaultPageRendersOverHttp(string $slug): void
{
$coId = $this->getComanageCoId();

$MostlyStaticPages = TableRegistry::getTableLocator()->get('MostlyStaticPages');
$page = $MostlyStaticPages->find()
->where(['co_id' => $coId, 'name' => $slug])
->firstOrFail();

// Correct route is /{coid}/{name}
$this->get("/{$coId}/{$slug}");

$this->assertResponseOk();
$this->assertResponseContains((string)$page->title);
}

#[DataProvider('defaultPageSlugsProvider')]
public function testDefaultPagesRenderOverHttp(string $slug): void
{
$this->assertDefaultPageRendersOverHttp($slug);
}

private function assertDefaultPageExists(string $name, string $expectedContext): void
{
$coId = $this->getComanageCoId();

$MostlyStaticPages = TableRegistry::getTableLocator()->get('MostlyStaticPages');

$page = $MostlyStaticPages->find()
->where([
'co_id' => $coId,
'name' => $name,
'status' => SuspendableStatusEnum::Active,
'context' => $expectedContext,
])
->first();

$this->assertNotEmpty(
$page,
sprintf('Expected Mostly Static Page "%s" to exist for CO %d after setup', $name, $coId)
);
}

#[DataProvider('defaultPagesProvider')]
public function testDefaultPagesExist(string $name, string $expectedContext): void
{
$this->assertDefaultPageExists($name, $expectedContext);
}
}
Loading

0 comments on commit fa5fe53

Please sign in to comment.