Completed
Push — master ( c8d2bb...a2e09b )
by amaury
08:01 queued 02:59
created

provideComponentAndData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace OpenOrchestra\FunctionalTests\BackofficeBundle\EventSubscriber;
4
5
use OpenOrchestra\FunctionalTests\Utils\AbstractAuthenticatedTest;
6
use OpenOrchestra\DisplayBundle\DisplayBlock\Strategies\ConfigurableContentStrategy;
7
use OpenOrchestra\DisplayBundle\DisplayBlock\Strategies\ContentListStrategy;
8
use OpenOrchestra\DisplayBundle\DisplayBlock\Strategies\VideoStrategy;
9
use OpenOrchestra\ModelBundle\Document\Block;
10
use OpenOrchestra\ModelInterface\Model\BlockInterface;
11
use Symfony\Component\Form\FormFactoryInterface;
12
use OpenOrchestra\ModelInterface\Repository\ReadContentRepositoryInterface;
13
use OpenOrchestra\ModelInterface\Repository\RepositoryTrait\KeywordableTraitInterface;
14
15
/**
16
 * Class BlockFormTypeSubscriberTest
17
 *
18
 * @group backofficeTest
19
 */
20
class BlockFormTypeSubscriberTest extends AbstractAuthenticatedTest
21
{
22
    /**
23
     * @var FormFactoryInterface
24
     */
25
    protected $formFactory;
26
    protected $keywords;
27
    protected $keywordRepository;
28
29
    /**
30
     * Set up the test
31
     */
32
    public function setUp()
33
    {
34
        parent::setUp();
35
        $this->keywordRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.keyword');
36
        $this->formFactory = static::$kernel->getContainer()->get('form.factory');
37
    }
38
39
    /**
40
     * Test video block : checkbox unique to uncheck
41
     */
42
    public function testVideoBlock()
43
    {
44
        $block = new Block();
45
        $block->setComponent(VideoStrategy::NAME);
46
        $block->addAttribute('videoType', 'youtube');
47
        $block->addAttribute('youtubeFs', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
        $formType =  static::$kernel->getContainer()->get('open_orchestra_backoffice.generate_form_manager')->getFormType($block);
49
        $form = $this->formFactory->create($formType, $block, array('csrf_protection' => false));
50
51
        $form->submit(array(
52
            'style' => 'default',
53
            'videoType' => 'youtube',
54
            'youtubeVideoId' => 'videoId',
55
            'youtubeAutoplay' => true,
56
        ));
57
58
        $this->assertTrue($form->isSynchronized());
59
        /** @var BlockInterface $data */
60
        $data = $form->getConfig()->getData();
61
        $this->assertBlock($data);
62
        $this->assertSame('videoId', $data->getAttribute('youtubeVideoId'));
63
        $this->assertTrue($data->getAttribute('youtubeAutoplay'));
64
        $this->assertFalse($data->getAttribute('youtubeFs'));
65
    }
66
67
    /**
68
     * @param string $component
69
     * @param array  $value
70
     *
71
     * @dataProvider provideComponentAndData
72
     */
73
    public function testMultipleBlock($component, $value)
74
    {
75
        $block = new Block();
76
        $block->setComponent($component);
77
        $formType =  static::$kernel->getContainer()->get('open_orchestra_backoffice.generate_form_manager')->getFormType($block);
78
79
        $form = $this->formFactory->create($formType, $block, array('csrf_protection' => false));
80
81
        $submittedValue = array_merge(array('style' => 'default'), $value);
82
        $form->submit($submittedValue);
83
84
        $this->assertTrue($form->isSynchronized());
85
        /** @var BlockInterface $data */
86
        $data = $form->getConfig()->getData();
87
        $this->assertBlock($data);
88
        foreach ($value as $key => $sendData) {
89
            $this->assertSame($sendData, $data->getAttribute($key));
90
        }
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function provideComponentAndData()
97
    {
98
        return array(
99
            array(ContentListStrategy::NAME, array(
100
                'contentNodeId' => 'root',
101
                'contentSearch' => array(
102
                  'contentType' => 'news',
103
                  'choiceType' => 'choice_and',
104
                  'keywords' => null,
105
                ),
106
                'characterNumber' => 150
107
            )),
108
            array(ConfigurableContentStrategy::NAME, array(
109
                'contentSearch' => array(
110
                    'contentType' => 'car',
111
                    'choiceType' => ReadContentRepositoryInterface::CHOICE_AND,
112
                    'keywords' => null,
113
                    'contentId' => null,
114
                )
115
            ))
116
        );
117
    }
118
119
    /**
120
     * @param string $component
121
     * @param array  $value
122
     *
123
     * @dataProvider provideComponentAndDataAndTransformedValue
124
     */
125
    public function testMultipleBlockWithDataTransformation($component, $value)
126
    {
127
        $block = new Block();
128
        $block->setComponent($component);
129
        $formType =  static::$kernel->getContainer()->get('open_orchestra_backoffice.generate_form_manager')->getFormType($block);
130
131
        $form = $this->formFactory->create($formType, $block, array('csrf_protection' => false));
132
        $submittedValue = array_merge(array('style' => 'default'), $value);
133
        $value['contentSearch']['keywords'] = $this->replaceKeywordLabelById($value['contentSearch']['keywords']);
134
        $form->submit($submittedValue);
135
136
        $this->assertTrue($form->isSynchronized());
137
        /** @var BlockInterface $data */
138
        $data = $form->getConfig()->getData();
139
        $this->assertBlock($data);
140
        foreach ($value as $key => $receivedData) {
141
            $this->assertSame($receivedData, $data->getAttribute($key));
142
        }
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function provideComponentAndDataAndTransformedValue()
149
    {
150
        return array(
151
            array(ContentListStrategy::NAME, array(
152
                'contentNodeId' => 'root',
153
                'characterNumber' => 150,
154
                'contentSearch' => array(
155
                    'contentType' => 'news',
156
                    'choiceType' => ReadContentRepositoryInterface::CHOICE_AND,
157
                    'keywords' => 'lorem AND ipsum',
158
                    )
159
            )),
160
        );
161
    }
162
163
    /**
164
     * @param $data
165
     */
166
    protected function assertBlock($data)
167
    {
168
        $this->assertInstanceOf('OpenOrchestra\ModelInterface\Model\BlockInterface', $data);
169
        $this->assertSame('default', $data->getStyle());
170
    }
171
172
    /**
173
     * @param string $condition
174
     *
175
     * @return array
176
     */
177 View Code Duplication
    protected function replaceKeywordLabelById($condition)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
        $conditionWithoutOperator = preg_replace(explode('|', KeywordableTraitInterface::OPERATOR_SPLIT), ' ', $condition);
180
        $conditionArray = explode(' ', $conditionWithoutOperator);
181
182
        foreach ($conditionArray as $keyword) {
183
            if ($keyword != '') {
184
                $keywordDocument = $this->keywordRepository->findOneByLabel($keyword);
185
                if (!is_null($keywordDocument)) {
186
                    $condition = str_replace($keyword, $keywordDocument->getId(), $condition);
187
                } else {
188
                    return '';
189
                }
190
            }
191
        }
192
193
        return $condition;
194
    }
195
}
196