Completed
Push — master ( 3b7290...73d268 )
by
unknown
11:56
created

SingleChannelModeExtensionTest::testBuildForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 2
eloc 12
nc 2
nop 3
1
<?php
2
3
namespace OroCRM\Bundle\ChannelBundle\Tests\Unit\Form\Extension;
4
5
use Symfony\Component\Form\FormEvent;
6
use Symfony\Component\Form\FormEvents;
7
use Symfony\Component\Form\FormView;
8
9
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
10
use OroCRM\Bundle\ChannelBundle\Form\Type\ChannelSelectType;
11
use OroCRM\Bundle\ChannelBundle\Form\Extension\SingleChannelModeExtension;
12
use OroCRM\Bundle\ChannelBundle\Provider\ChannelsByEntitiesProvider;
13
14
class SingleChannelModeExtensionTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @var SingleChannelModeExtension
18
     */
19
    protected $extension;
20
21
    /**
22
     * @var ChannelsByEntitiesProvider|\PHPUnit_Framework_MockObject_MockObject
23
     */
24
    protected $channelsProvider;
25
26
    protected function setUp()
27
    {
28
        $this->channelsProvider = $this
29
            ->getMockBuilder('OroCRM\Bundle\ChannelBundle\Provider\ChannelsByEntitiesProvider')
30
            ->disableOriginalConstructor()
31
            ->getMock();
32
33
        $this->extension = new SingleChannelModeExtension($this->channelsProvider);
34
    }
35
36
37
    public function testGetExtendedType()
38
    {
39
        $this->assertEquals(
40
            $this->extension->getExtendedType(),
41
            ChannelSelectType::NAME
42
        );
43
    }
44
45
    /**
46
     * @dataProvider testBuildFormDataProvider
47
     */
48
    public function testBuildForm(array $entities, array $channels, callable $callback = null)
49
    {
50
        $builder = $this->getMock('Symfony\Component\Form\Test\FormBuilderInterface');
51
        $this->channelsProvider
52
            ->expects($this->once())
53
            ->method('getChannelsByEntities')
54
            ->with($entities)
55
            ->willReturn($channels);
56
        if (count($channels) === 1) {
57
            $builder->expects($this->once())
58
                ->method('addEventListener')
59
                ->with(FormEvents::PRE_SET_DATA, $callback);
60
        }
61
62
        $this->extension->buildForm($builder, ['entities' => $entities, 'single_channel_mode' => true]);
63
64
    }
65
66
    /**
67
     * @dataProvider testBuildViewDataProvider
68
     */
69
    public function testBuildView(array $entities, array $channels, $readOnly = false, $hide = false)
70
    {
71
        $view = new FormView();
72
73
        $form = $this->getMock('Symfony\Component\Form\FormInterface');
74
        $options = ['entities' => $entities, 'single_channel_mode' => true];
75
76
        $view->vars['read_only'] = false;
77
78
        $this->channelsProvider
79
            ->expects($this->once())
80
            ->method('getChannelsByEntities')
81
            ->with($entities)
82
            ->willReturn($channels);
83
        $this->extension->buildView($view, $form, $options);
84
85
        $this->assertEquals($readOnly, $view->vars['read_only']);
86
        if ($hide) {
87
            $this->assertEquals('hide', $view->vars['attr']['class']);
88
        }
89
    }
90
91
    public function testSetDefaultOptions()
92
    {
93
        $resolver = $this->getMock('Symfony\Component\OptionsResolver\OptionsResolverInterface');
94
        $resolver->expects($this->once())
95
            ->method('setDefaults')
96
            ->with(['single_channel_mode' => true]);
97
98
        $this->extension->setDefaultOptions($resolver);
99
    }
100
101
    public function testBuildFormDataProvider()
102
    {
103
        $channel = new Channel();
104
105
        return [
106
            'one channel' => [
107
                ['Entity1'],
108
                [$channel],
109
                function (FormEvent $event) use ($channel) {
110
                    $event->setData($channel);
111
                }
112
            ],
113
            'more channels' => [
114
                ['Entity1'],
115
                [$channel, new Channel()]
116
            ]
117
        ];
118
    }
119
120
    public function testBuildViewDataProvider()
121
    {
122
        return [
123
            'one channel' => [
124
                ['Entity1'],
125
                [new Channel()],
126
                true,
127
                true
128
            ],
129
            'more channels' => [
130
                ['Entity1'],
131
                [new Channel(), new Channel()]
132
            ]
133
        ];
134
    }
135
}
136