Files
agent-unit-testing/templates/cakephp/phpunit/ControllerTest.template.php
T
Pham-Tan-Thien da1bd48444 feat: decouple Laravel and CakePHP installers, remove SonarQube artifacts, and enforce complete unit testing standards
- Decouple framework installation: add install-laravel.sh and install-cakephp.sh, isolate skills and docs per framework, and auto-clean leftover artifacts
- Remove all SonarQube configs, templates, and references
- Enforce comprehensive test completeness: 100% public methods, edge cases, full branch coverage, exception paths, and state assertions
- Replace all placeholder assertions in PHPUnit and Pest templates with full realistic implementations
- Rename and update 10-comprehensive-test-coverage-guide.md
2026-09-17 14:25:02 +07:00

156 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* App\Controller\{{CONTROLLER_NAME}}Controller Test Case
*
* @uses \App\Controller\{{CONTROLLER_NAME}}Controller
*/
class {{CONTROLLER_NAME}}ControllerTest extends TestCase
{
use IntegrationTestTrait;
/**
* Fixtures
*
* @var list<string>
*/
protected array $fixtures = [
'app.{{MODEL_NAME}}',
];
/**
* Test index method
*/
public function testIndex(): void
{
// Act
$this->get('/{{ROUTE_PATH}}');
// Assert
$this->assertResponseOk();
$this->assertResponseContains('{{EXPECTED_TEXT}}');
}
/**
* Test view method with valid ID
*/
public function testViewValidId(): void
{
// Act
$this->get('/{{ROUTE_PATH}}/view/1');
// Assert
$this->assertResponseOk();
}
/**
* Test add method GET renders form
*/
public function testAddGet(): void
{
// Act
$this->get('/{{ROUTE_PATH}}/add');
// Assert
$this->assertResponseOk();
}
/**
* Test add method POST with valid data
*/
public function testAddPostSuccess(): void
{
// Arrange
$this->enableCsrfToken();
$this->enableSecurityToken();
$data = [
'name' => 'New Item',
'status' => 'active',
];
// Act
$this->post('/{{ROUTE_PATH}}/add', $data);
// Assert
$this->assertResponseSuccess();
$this->assertRedirect(['action' => 'index']);
$this->assertFlashMessage('The {{ENTITY_LOWER}} has been saved.');
}
/**
* Test add method POST with validation errors
*/
public function testAddPostValidationFailure(): void
{
// Arrange
$this->enableCsrfToken();
$this->enableSecurityToken();
$data = []; // Empty or invalid data
// Act
$this->post('/{{ROUTE_PATH}}/add', $data);
// Assert
$this->assertResponseOk(); // Renders form view with errors
}
/**
* Test view method with non-existent ID (404 Not Found)
*/
public function testViewNotFound(): void
{
// Act
$this->get('/{{ROUTE_PATH}}/view/99999');
// Assert
$this->assertResponseError();
$this->assertResponseCode(404);
}
/**
* Test edit method POST with valid data
*/
public function testEditSuccess(): void
{
// Arrange
$this->enableCsrfToken();
$this->enableSecurityToken();
$data = [
'name' => 'Updated Item',
];
// Act
$this->post('/{{ROUTE_PATH}}/edit/1', $data);
// Assert
$this->assertResponseSuccess();
$this->assertRedirect(['action' => 'index']);
}
/**
* Test delete method POST/DELETE
*/
public function testDeleteSuccess(): void
{
// Arrange
$this->enableCsrfToken();
$this->enableSecurityToken();
// Act
$this->delete('/{{ROUTE_PATH}}/delete/1');
// Assert
$this->assertResponseSuccess();
$this->assertRedirect(['action' => 'index']);
}
}