Use ReflectionClass for CompositeValidator tests

This commit is contained in:
cpenny 2020-05-28 09:52:28 +12:00
parent bca4be77ed
commit 2765b65f42
2 changed files with 12 additions and 29 deletions

View File

@ -2,13 +2,14 @@
namespace SilverStripe\Forms\Tests; namespace SilverStripe\Forms\Tests;
use ReflectionClass;
use ReflectionException;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\Tests\ValidatorTest\TestValidator; use SilverStripe\Forms\Tests\ValidatorTest\TestValidator;
use SilverStripe\Forms\Tests\ValidatorTest\TestCompositeValidator;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
use SilverStripe\Forms\CompositeValidator; use SilverStripe\Forms\CompositeValidator;
@ -45,23 +46,30 @@ class CompositeValidatorTest extends SapphireTest
$this->assertCount(2, $compositeValidator->getValidators()); $this->assertCount(2, $compositeValidator->getValidators());
} }
/**
* @throws ReflectionException
*/
public function testSetForm(): void public function testSetForm(): void
{ {
$form = $this->getForm(); $form = $this->getForm();
$compositeValidator = new TestCompositeValidator(); $reflectionClass = new ReflectionClass(CompositeValidator::class);
$property = $reflectionClass->getProperty('form');
$property->setAccessible(true);
$compositeValidator = new CompositeValidator();
$validator = new TestValidator(); $validator = new TestValidator();
$compositeValidator->addValidator($validator); $compositeValidator->addValidator($validator);
$compositeValidator->setForm($form); $compositeValidator->setForm($form);
$this->assertNotNull($compositeValidator->getForm()); $this->assertNotNull($property->getValue($compositeValidator));
$this->assertCount(1, $compositeValidator->getValidators()); $this->assertCount(1, $compositeValidator->getValidators());
foreach ($compositeValidator->getValidators() as $validator) { foreach ($compositeValidator->getValidators() as $validator) {
/** @var TestValidator $validator */ /** @var TestValidator $validator */
$this->assertNotNull($validator->getForm()); $this->assertNotNull($property->getValue($validator));
} }
} }

View File

@ -1,25 +0,0 @@
<?php
namespace SilverStripe\Forms\Tests\ValidatorTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\CompositeValidator;
/**
* Class TestCompositeValidator
*
* @package SilverStripe\Forms\Tests\ValidatorTest
*/
class TestCompositeValidator extends CompositeValidator implements TestOnly
{
/**
* Allow us to access the form for test purposes.
*
* @return Form|null
*/
public function getForm(): ?Form
{
return $this->form;
}
}