Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8724663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:52:36+00:00 2026-06-13T07:52:36+00:00

I am trying to test a method on the bind event of a custom

  • 0

I am trying to test a method on the bind event of a custom form type.

Here is the code

public function bind(DataEvent $event)
{
    $form = $event->getForm();

    if($form->getNormData() instanceof BaseFileInterface && !$event->getData() instanceof UploadedFile) {
        $event->setData($form->getNormData());
    }

    if($event->getData() instanceof UploadedFile) {
        $hander = $this->handlerManager->getHandler(
            $form->getParent()->getConfig()->getDataClass(), 
            $form->getName()
        );

        $datas = $hander->createBaseFile($event->getData());
        $event->setData($datas);
    }

    if(is_string($event->getData())) {
        $event->setData(null);
    }
}

and the form builder of the type :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->addEventSubscriber(new BaseFileListener($this->handlerManager))
        ->addViewTransformer(new BaseFileToStringTransformer())
    ;
}

My test class inherits from Symfony\Component\Form\Tests\FormIntegrationTestCase and is doing this :

protected function setUp()
{
    parent::setUp();

    $this->handlerManager = $this->getHandlerManagerMock();

    $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
    $this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory);

    $this->form =  $this->factory->create('my_file');
    $this->form->setParent($this->getFormMock())->setData(new DummyEntity());
}

protected function getFormMock()
{

    $mock = $this
        ->getMockBuilder('Symfony\Component\Form\Tests\FormInterface')
        ->disableOriginalConstructor()
        ->getMock()
    ;

    return $mock;
}

public function testBindHandleNewFileWithNonEmptyField()
{
    $data = $file = new UploadedFile(
        __DIR__.'/../Fixtures/test.gif',
        'original.gif',
        'image/gif',
        filesize(__DIR__.'/../Fixtures/test.gif'),
        null
    );

    $event = new FormEvent($this->form, $data);

    $listener = new BaseFileListener($this->handlerManager);
    $listener->bind($event);

    $this->assertInstanceOf('My\FooBundle\Entity\BaseFileInterface', $event->getData());
    $this->assertNotEquals($event->getData(), $this->form->getNormData());
}

The probleme is that the $form->getParent()->getConfig()->getDataClass() throws an exception on getDataClass ().

The question is how to build the form correctly to perform the bind test ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:52:37+00:00Added an answer on June 13, 2026 at 7:52 am

    Ok, answering my self 🙂

    Here is the good mocking in phpunit :

    protected function setUp()
    {
        parent::setUp();
    
        $this->handlerManager = $this->getHandlerManagerMock();
    
        $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
        $this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory);
    
        $this->form =  $this->factory->create('my_file');
        $this->form->setParent($this->getFormMock());
    }
    
    protected function getFormMock()
    {
        $mock = $this->getMock('Symfony\Component\Form\Tests\FormInterface');
    
        $mock
            ->expects($this->any())
            ->method('getConfig')
            ->will($this->returnValue($this->getFormConfigMock()))
        ;
    
        return $mock;
    }
    
    protected function getFormConfigMock()
    {
        $mock = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')
            ->disableOriginalConstructor()
            ->getMock();
    
        $mock
            ->expects($this->any())
            ->method('getDataClass')
            ->will($this->returnValue('My\FooBundle\Tests\DummyEntity'))
        ;
    
        return $mock;
    }
    

    I thought I could manage to rebuild the entire form without using mock, but it seems impossible.

    If someone has a better solution to offer I’m interested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm am trying to test wcf method in C#. method return type is ilist.
I'm trying to write a method like this: public static T Test<T>() { if
Jython 2.5 I'm trying to bind a method to the focusGained event of a
I'm trying to test the change event of backbone collection, using this code: var
i am trying to test this method: [Test] public void Test_GetReferenceValue() { Person person=
I'm trying to test a service method that runs asynchronously (@Async). Here is the
I am trying to test that a method to load a UI matrix is
I'm trying to test a model method using Play Framework 2.0 and Specs2. Global.scala
I'm trying to test a presenter method using a Capybara RSpec matcher. Lets say
I'm trying to test an Order entity method called AddItem and I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.