how to unit test symfony form which uses custom data mapper?

I’ve run into few problems with unit testing the Symfony Form.
First of them was how to properly pass the EntityManager to constructor, which I found answer for here – My approach to test class is strongly based on this. Also I tried to follow Symfony Data Mapper Tutorial and Forms Unit Testing Tutorial to the best of my understanding.

Now I don’t know how to proceed, because the test doesn’t go into the mapFormsToData() nor mapDataToForms() methods.
Most fields of the form are mapped in various ways to group of objects owned by Investment, but not directly, there is more complex logic underneath, therefore I implemented DataMapperInterface.

Now, in the test, while creating a form object:

    $objectToCompare = new Investment();
    $form = $this->factory->create(InvestmentType::class, $objectToCompare);

I get the error:

SymfonyComponentPropertyAccessExceptionNoSuchPropertyException :
Neither the property “firstCheck” nor one of the methods
“getFirstCheck()”, “firstCheck()”, “isFirstCheck()”,
“hasFirstCheck()”, “__get()” exist and have public access in class
“AppEntityInvestment”.

As you would probably figure, firstCheck is name of the non-mapped field.

These are my setUp() and getExtensions() methods of the test class:

protected function setUp(): void
    {
        $this->entityManager = DoctrineTestHelper::createTestEntityManager();
        $this->emRegistry = $this->createRegistryMock('default', $this->entityManager);
        $this->userProvider = $this->createMock(UserProvider::class);
        $this->datetimeHelper = $this->createMock(DatetimeHelper::class);

    $this->user = $this->createMock(User::class);
    $this->userProvider->method('getUser')->willReturn($this->user);

    parent::setUp();

    $schemaTool = new SchemaTool($this->entityManager);

    // This is the important part for you !
    $classes = [
        $this->entityManager->getClassMetadata(Investment::class),
        $this->entityManager->getClassMetadata(Pair::class)
    ];

    try {
        $schemaTool->dropSchema($classes);
    } catch (Exception $e) {
    }

    try {
        $schemaTool->createSchema($classes);
    } catch (Exception $e) {
    }
}


protected function getExtensions()
{
    $type = new InvestmentType($this->userProvider, $this->entityManager, $this->datetimeHelper);
    return [
        new PreloadedExtension([$type], []),
        new DoctrineOrmExtension($this->emRegistry)
    ];
}

And this are the mapFormsToData() and mapDataToForms() implemenation of DataMapperInterface methods from the tested form class:

/**
 * @param FormInterface[]|Traversable $forms
 * @param mixed $viewData
 * @throws ORMException
 */
public function mapFormsToData($forms, &$viewData)
{
    if (!$viewData instanceof Investment) {
        throw new UnexpectedTypeException($viewData, Investment::class);
    }
$forms = iterator_to_array($forms);

$positionStatus = null;
if (null !== $viewData->getId()) {
    /** @var Pair $pair */
    $pair = $this->entityManager->getRepository(Pair::class)->find($forms['pair']);

    $viewData->setPair($pair);
    $viewData->setAmount($forms['amount']);
} else {
    $positionStatus = $viewData->getCurrentPosition()->getStatus();
}

$this->clearChecks($viewData);

$this->generateChecks($forms, $viewData, $positionStatus);
$viewData->setUser($this->userProvider->getUser());

}


public function mapDataToForms($viewData, $forms)
{
    if (null === $viewData) {
        return;
    }

    if (!$viewData instanceof Investment) {
        throw new UnexpectedTypeException($viewData, Investment::class);
    }

    $forms = iterator_to_array($forms);

    $checks = $viewData->getTimespanChecks();
    $positionStatus = $viewData->getCurrentPosition()->getStatus() ?? null;
    $forms = $this->parseChecks($checks, $forms, $positionStatus);

    if ($checks->last()->getNext() instanceof TimespanCheck) {
        $forms['autoRefresh'] = true;
    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *