8f7212ef65
- Add sonar-project.properties templates for CakePHP and Laravel - Update install.sh to automatically provision SonarQube config - Enhance cakephp-unit-test skill with exhaustive branch coverage rules - Update SonarQube guide with CakePHP specific rules and exclusion patterns - Enhance TableTest template with buildRules and validation failure cases
132 lines
3.1 KiB
PHP
132 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 if unique rule exists
|
|
if (!empty($entity2->getErrors())) {
|
|
$this->assertFalse($result);
|
|
} else {
|
|
$this->assertTrue(true);
|
|
}
|
|
}
|
|
}
|