test and corrections

This commit is contained in:
Benjamin Dalsass
2025-11-27 10:58:21 +01:00
parent d0a2af44ac
commit b8a093e625
22 changed files with 457 additions and 119 deletions

View File

@@ -12,7 +12,9 @@ use Combodo\iTop\Forms\Block\Base\FormBlock;
use Combodo\iTop\Forms\IO\Format\StringIOFormat;
use Combodo\iTop\Forms\IO\FormInput;
use Combodo\iTop\Forms\IO\FormOutput;
use Combodo\iTop\Forms\Register\IORegister;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use ReflectionClass;
/**
* @copyright Copyright (C) 2010-2025 Combodo SARL
@@ -35,15 +37,23 @@ abstract class AbstractFormsTest extends ItopDataTestCase
return new FormOutput($sName.'_output', $sType, $oBlock);
}
public function GivenFormBlock(string $sName, string $sBlockClass = FormBlock::class): AbstractFormBlock
public function GivenFormBlock(string $sName): FormBlock
{
return new $sBlockClass($sName, []);
return new FormBlock($sName, []);
}
public function GivenSubFormBlock(AbstractFormBlock $oParent, string $sName, string $ssBlockClass = FormBlock::class): AbstractFormBlock
public function GivenSubFormBlock(FormBlock $oParent, string $sName, string $ssBlockClass = FormBlock::class): AbstractFormBlock
{
$oParent->Add($sName, $ssBlockClass, []);
return $oParent->Get($sName);
}
public function GetIORegister(AbstractFormBlock $oFormBlock): IORegister
{
$reflection = new ReflectionClass(AbstractFormBlock::class);
$reflection_property = $reflection->getProperty('oIORegister');
$reflection_property->setAccessible(true);
return $reflection_property->getValue($oFormBlock);
}
}

View File

@@ -113,56 +113,14 @@ class BlockTest extends AbstractFormsTest
$oForm->get('birthdate');
}
public function testAddingTwiceTheSameInputThrowsException(): void
public function testIsRootBlock(): void
{
$oFormBlock = $this->GivenFormBlock('OneBlock')
->AddInput('test_input', StringIOFormat::class);
/** @var FormBlock $oFormBlock */
$oFormBlock = $this->GivenFormBlock('OneBlock');
$this->expectException(RegisterException::class);
$oFormBlock->AddInput('test_input', StringIOFormat::class);
}
$oFormBlock->Add('subform', FormBlock::class);
public function testAddingTwiceTheSameOutputThrowsException(): void
{
$oFormBlock = $this->GivenFormBlock('OneBlock')
->AddOutput('test_output', StringIOFormat::class);
$this->expectException(RegisterException::class);
$oFormBlock->AddOutput('test_output', StringIOFormat::class);
}
public function testDependingOnNonExistingInputThrowsException(): void
{
$oParentBlock = $this->GivenFormBlock('ParentBlock');
$oFormBlock = $this->GivenSubFormBlock($oParentBlock, 'OneBlock')
->AddInput('test_input', StringIOFormat::class);
$this->GivenSubFormBlock($oParentBlock, 'OtherBlock')
->AddOutput('test_output', StringIOFormat::class);
$this->expectException(RegisterException::class);
$oFormBlock->DependsOn('non_existing_input', 'OtherBlock', 'test_output');
}
public function testDependingOnNonExistingOutputThrowsException(): void
{
$oParentBlock = $this->GivenFormBlock('ParentBlock');
$oFormBlock = $this->GivenSubFormBlock($oParentBlock, 'OneBlock')
->AddInput('test_input', StringIOFormat::class);
$this->GivenSubFormBlock($oParentBlock, 'OtherBlock')
->AddOutput('test_output', StringIOFormat::class);
$this->expectException(RegisterException::class);
$oFormBlock->DependsOn('test_input', 'OtherBlock', 'non_existing_output');
}
public function testDependingOnNonExistingBlockThrowsException(): void
{
$oFormBlock = $this->GivenFormBlock('OneBlock')
->AddOutput('test_output', StringIOFormat::class);
$this->expectException(RegisterException::class);
$oFormBlock->DependsOn('test_input', 'UnknownBlock', 'test');
$this->assertTrue($oFormBlock->IsRootBlock());
$this->assertFalse($oFormBlock->Get('subform')->IsRootBlock());
}
}

View File

@@ -0,0 +1,194 @@
<?php
/*
* @copyright Copyright (C) 2010-2025 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Forms\Register;
use Combodo\iTop\Forms\Block\Base\CheckboxFormBlock;
use Combodo\iTop\Forms\Block\Base\FormBlock;
use Combodo\iTop\Forms\Block\Base\TextFormBlock;
use Combodo\iTop\Forms\IO\Format\BooleanIOFormat;
use Combodo\iTop\Forms\IO\Format\StringIOFormat;
use Combodo\iTop\Forms\Register\IORegister;
use Combodo\iTop\Forms\Register\RegisterException;
use Combodo\iTop\Test\UnitTest\sources\Forms\AbstractFormsTest;
class IORegisterTest extends AbstractFormsTest
{
private FormBlock $oFormBlock;
private IORegister $oIORegister;
protected function setUp(): void
{
parent::setUp();
$this->oFormBlock = $this->GivenFormBlock('OneBlock');
$this->oIORegister = $this->GetIORegister($this->oFormBlock);
}
public function testAddInput(): void
{
$this->oIORegister->AddInput('input', StringIOFormat::class);
$this->assertTrue($this->oIORegister->HasInput('input'));
$this->assertNotNull($this->oIORegister->GetInput('input'));
}
public function testGetInputs(): void
{
$iOriginInputCount = count($this->oIORegister->GetInputs());
$this->oIORegister->AddInput('input_1', StringIOFormat::class);
$this->oIORegister->AddInput('input_2', BooleanIOFormat::class);
$this->oIORegister->AddInput('input_3', StringIOFormat::class);
$this->assertCount(3 + $iOriginInputCount, $this->oIORegister->GetInputs());
$this->assertArrayHasKey('input_1', $this->oIORegister->GetInputs());
}
public function testGetOutputs(): void
{
$this->oIORegister->AddOutput('output_1', StringIOFormat::class);
$this->oIORegister->AddOutput('output_2', BooleanIOFormat::class);
$this->assertCount(2, $this->oIORegister->GetOutputs());
$this->assertArrayHasKey('output_1', $this->oIORegister->GetOutputs());
}
public function testMissingInput(): void
{
$this->expectException(RegisterException::class);
$this->oIORegister->GetInput('missing_input');
}
public function testMissingOutput(): void
{
$this->expectException(RegisterException::class);
$this->oIORegister->GetOutput('missing_output');
}
public function testGetBoundInputs(): void
{
$this->GivenSubFormBlock($this->oFormBlock, 'SubFormA', TextFormBlock::class);
$this->GivenSubFormBlock($this->oFormBlock, 'SubFormB', CheckboxFormBlock::class);
$this->GivenSubFormBlock($this->oFormBlock, 'SubFormC', TextFormBlock::class);
$oSubForm = $this->GivenSubFormBlock($this->oFormBlock, 'SubForm', TextFormBlock::class);
$oSubForm->AddInput('input_from_A', StringIOFormat::class);
$oSubForm->AddInput('input_from_B', BooleanIOFormat::class);
$oSubForm->AddInput('input_from_C', StringIOFormat::class);
$oSubForm->AddInput('unbound_input', StringIOFormat::class);
$this->GetIORegister($oSubForm)->DependsOn('input_from_A', 'SubFormA', TextFormBlock::OUTPUT_TEXT);
$this->GetIORegister($oSubForm)->DependsOn('input_from_B', 'SubFormB', CheckboxFormBlock::OUTPUT_CHECKED);
$this->GetIORegister($oSubForm)->DependsOn('input_from_C', 'SubFormC', TextFormBlock::OUTPUT_TEXT);
$aBoundInputs = $this->GetIORegister($oSubForm)->GetBoundInputs();
$this->assertCount(3, $aBoundInputs);
}
public function testGetBoundOutputs(): void
{
$this->oFormBlock->AddOutput('output', StringIOFormat::class);
$oSubFormA = $this->GivenSubFormBlock($this->oFormBlock, 'SubFormA', TextFormBlock::class);
$oIORegisterA = $this->GetIORegister($oSubFormA);
$oIORegisterA->ImpactParent(TextFormBlock::OUTPUT_TEXT, 'output');
$this->assertCount(1, $this->oIORegister->GetBoundOutputs());
}
public function testAddInputDependsOn(): void
{
$this->GivenSubFormBlock($this->oFormBlock, 'SubFormA', TextFormBlock::class);
$oSubFormB = $this->GivenSubFormBlock($this->oFormBlock, 'SubFormB', TextFormBlock::class);
$oIORegisterB = $this->GetIORegister($oSubFormB);
$oIORegisterB->AddInputDependsOn('input', 'SubFormA', TextFormBlock::OUTPUT_TEXT);
$this->assertNotNull($oIORegisterB->GetInput('input'));
}
public function testImpactParent(): void
{
$this->oFormBlock->AddOutput('output', StringIOFormat::class);
$oSubFormA = $this->GivenSubFormBlock($this->oFormBlock, 'SubFormA', TextFormBlock::class);
$oIORegisterA = $this->GetIORegister($oSubFormA);
$oIORegisterA->ImpactParent(TextFormBlock::OUTPUT_TEXT, 'output');
$this->assertTrue($this->oFormBlock->GetOutput('output')->IsBound());
}
public function testAddingTwiceTheSameInputThrowsException(): void
{
$this->oIORegister->AddInput('test_input', StringIOFormat::class);
$this->expectException(RegisterException::class);
$this->oIORegister->AddInput('test_input', StringIOFormat::class);
}
public function testAddingTwiceTheSameOutputThrowsException(): void
{
$this->oIORegister->AddOutput('test_output', StringIOFormat::class);
$this->expectException(RegisterException::class);
$this->oIORegister->AddOutput('test_output', StringIOFormat::class);
}
public function testDependingOnNonExistingInputThrowsException(): void
{
$this->oIORegister->AddInput('test_input', StringIOFormat::class);
$this->oIORegister->AddOutput('test_output', StringIOFormat::class);
$this->expectException(RegisterException::class);
$this->oIORegister->DependsOn('non_existing_input', 'OtherBlock', 'test_output');
}
public function testDependingOnNonExistingOutputThrowsException(): void
{
$this->oIORegister->AddInput('test_input', StringIOFormat::class);
$this->expectException(RegisterException::class);
$this->oIORegister->DependsOn('test_input', 'OtherBlock', 'non_existing_output');
}
public function testDependingOnNonExistingBlockThrowsException(): void
{
$this->oIORegister->AddInput('test_input', StringIOFormat::class);
$this->oIORegister->AddOutput('test_output', StringIOFormat::class);
$this->expectException(RegisterException::class);
$this->oIORegister->DependsOn('test_input', 'UnknownBlock', 'test');
}
public function testHasDependenciesBlocks(): void
{
$this->GivenSubFormBlock($this->oFormBlock, 'SubFormA', TextFormBlock::class);
$oSubForm = $this->GivenSubFormBlock($this->oFormBlock, 'SubForm', TextFormBlock::class);
$oSubForm->AddInput('input_from_A', StringIOFormat::class);
$this->GetIORegister($oSubForm)->DependsOn('input_from_A', 'SubFormA', TextFormBlock::OUTPUT_TEXT);
$this->assertTrue($this->GetIORegister($oSubForm)->HasDependenciesBlocks());
$this->assertFalse($this->oIORegister->HasDependenciesBlocks());
}
public function testImpactBlocks(): void
{
$oSubFormA = $this->GivenSubFormBlock($this->oFormBlock, 'SubFormA', TextFormBlock::class);
$oSubForm = $this->GivenSubFormBlock($this->oFormBlock, 'SubForm', TextFormBlock::class);
$oSubForm->AddInput('input_from_A', StringIOFormat::class);
$this->GetIORegister($oSubForm)->DependsOn('input_from_A', 'SubFormA', TextFormBlock::OUTPUT_TEXT);
$this->assertFalse($this->GetIORegister($oSubForm)->ImpactDependentsBlocks());
$this->assertTrue($this->GetIORegister($oSubFormA)->ImpactDependentsBlocks());
}
}

View File

@@ -30,6 +30,14 @@ class OptionsRegisterTest extends ItopDataTestCase
$this->oOptionsRegister->SetOption('not valid option name', 'value');
}
public function testSetOptionTwice(): void
{
$this->oOptionsRegister->SetOption('valid_option_name', 'value');
$this->oOptionsRegister->SetOption('valid_option_name', 'value2');
$this->assertEquals('value2', $this->oOptionsRegister->GetOption('valid_option_name'));
}
public function testSetNonTypeOption(): void
{
$this->oOptionsRegister->SetOption('not_a_type_option', 'value', false);