da1bd48444
- 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
129 lines
3.1 KiB
PHP
129 lines
3.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Test\TestCase\Model\Table;
|
|
|
|
use App\Model\Table\{{TABLE_NAME}}Table;
|
|
use Cake\TestSuite\TestCase;
|
|
|
|
/**
|
|
* App\Model\Table\{{TABLE_NAME}}Table Test Case
|
|
*/
|
|
class {{TABLE_NAME}}TableTest extends TestCase
|
|
{
|
|
/**
|
|
* Test subject
|
|
*
|
|
* @var \App\Model\Table\{{TABLE_NAME}}Table
|
|
*/
|
|
protected {{TABLE_NAME}}Table ${{VARIABLE_NAME}};
|
|
|
|
/**
|
|
* Fixtures
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected array $fixtures = [
|
|
'app.{{TABLE_NAME}}',
|
|
];
|
|
|
|
/**
|
|
* setUp method
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$config = $this->getTableLocator()->exists('{{TABLE_NAME}}') ? [] : ['className' => {{TABLE_NAME}}Table::class];
|
|
$this->{{VARIABLE_NAME}} = $this->getTableLocator()->get('{{TABLE_NAME}}', $config);
|
|
}
|
|
|
|
/**
|
|
* tearDown method
|
|
*/
|
|
protected function tearDown(): void
|
|
{
|
|
unset($this->{{VARIABLE_NAME}});
|
|
parent::tearDown();
|
|
}
|
|
|
|
/**
|
|
* Test validationDefault method with valid data
|
|
*/
|
|
public function testValidationDefaultSuccess(): void
|
|
{
|
|
// Arrange
|
|
$data = [
|
|
'name' => 'Valid Title',
|
|
'status' => 'active',
|
|
];
|
|
|
|
// Act
|
|
$entity = $this->{{VARIABLE_NAME}}->newEntity($data);
|
|
|
|
// Assert
|
|
$this->assertEmpty($entity->getErrors());
|
|
}
|
|
|
|
/**
|
|
* Test validationDefault method with missing required fields
|
|
*/
|
|
public function testValidationDefaultFailsOnRequiredFields(): void
|
|
{
|
|
// Arrange
|
|
$data = [];
|
|
|
|
// Act
|
|
$entity = $this->{{VARIABLE_NAME}}->newEntity($data);
|
|
|
|
// Assert
|
|
$this->assertNotEmpty($entity->getErrors());
|
|
$this->assertArrayHasKey('name', $entity->getErrors());
|
|
}
|
|
|
|
/**
|
|
* Test save and find
|
|
*/
|
|
public function testSaveAndFind(): void
|
|
{
|
|
// Arrange
|
|
$entity = $this->{{VARIABLE_NAME}}->newEntity([
|
|
'name' => 'Sample Record',
|
|
'status' => 'active',
|
|
]);
|
|
|
|
// Act
|
|
$result = $this->{{VARIABLE_NAME}}->save($entity);
|
|
|
|
// Assert
|
|
$this->assertNotFalse($result);
|
|
$this->assertNotNull($result->id);
|
|
|
|
$saved = $this->{{VARIABLE_NAME}}->get($result->id);
|
|
$this->assertSame('Sample Record', $saved->name);
|
|
}
|
|
|
|
/**
|
|
* Test buildRules method for uniqueness
|
|
*/
|
|
public function testBuildRulesUnique(): void
|
|
{
|
|
// Arrange
|
|
$entity1 = $this->{{VARIABLE_NAME}}->newEntity([
|
|
'name' => 'Unique Item',
|
|
'email' => 'unique@example.com',
|
|
]);
|
|
$this->{{VARIABLE_NAME}}->save($entity1);
|
|
|
|
// Act - Attempt to save duplicate
|
|
$entity2 = $this->{{VARIABLE_NAME}}->newEntity([
|
|
'name' => 'Duplicate Item',
|
|
'email' => 'unique@example.com',
|
|
]);
|
|
$result = $this->{{VARIABLE_NAME}}->save($entity2);
|
|
|
|
// Assert - Rule should fail when duplicate unique field is saved
|
|
$this->assertFalse($result);
|
|
$this->assertNotEmpty($entity2->getErrors());
|
|
}
|
|
}
|