Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Tests/unit/Helper/FormWidgets/FormWidgetTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Tests\Helper\FormWidgets;
4
5
use ArrayIterator;
6
use Doctrine\ORM\EntityManager;
7
use Kunstmaan\AdminBundle\Entity\User;
8
use Kunstmaan\AdminBundle\Form\ColorType;
9
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget;
10
use Kunstmaan\AdminBundle\Tests\unit\Helper\FormWidgets\FakeView;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\Form\FormBuilder;
13
use Symfony\Component\Form\FormBuilderInterface;
14
use Symfony\Component\Form\FormError;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * Class FormWidgetTest
19
 */
20
class FormWidgetTest extends TestCase
21
{
22
    public function testWidget()
23
    {
24
        $views = new ArrayIterator();
25
        $views->vars = ['errors' => [new FormError('bang')]];
0 ignored issues
show
The property vars does not seem to exist in ArrayIterator.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
26
        $view = new FakeView();
27
        $view->offsetSet('a', $views);
28
29
        $builder = $this->createMock(FormBuilder::class);
30
        $em = $this->createMock(EntityManager::class);
31
        $em->expects($this->once())->method('persist')->willReturn(new User());
32
        $builder->expects($this->once())->method('getData');
33
        $builder->expects($this->atLeastOnce())->method('add');
34
        $builder->expects($this->once())->method('setData');
35
36
        $widget = new FormWidget(['a' => new ColorType()], ['a' => 'data'], ['a' => ['options' => 'here']]);
37
        $widget->bindRequest(new Request());
38
        $widget->setIdentifier('id');
39
        $widget->persist($em);
40
41
        $this->assertEquals('@KunstmaanAdmin/FormWidgets/FormWidget/widget.html.twig', $widget->getTemplate());
42
        $this->assertEquals('id', $widget->getIdentifier());
43
        $this->assertCount(1, $widget->getFormErrors($view));
44
        $this->assertCount(0, $widget->getExtraParams(new Request()));
45
        $this->assertCount(1, $widget->getOptions());
46
        $this->assertCount(1, $widget->getData());
47
        $this->assertCount(1, $widget->getTypes());
48
49
        $widget->addType('test', new ColorType());
50
        $this->assertCount(2, $widget->getTypes());
51
52
        /* @var FormBuilderInterface $builder*/
53
        $widget->buildForm($builder);
54
    }
55
}
56