Issues (3641)

Zed/Form/_support/Helper/FormHelper.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerTest\Zed\Form\Helper;
9
10
use Codeception\Module;
11
use Codeception\Stub;
12
use Codeception\TestInterface;
13
use Spryker\Shared\FormExtension\Dependency\Plugin\FormPluginInterface;
14
use Spryker\Zed\Form\Communication\FormCommunicationFactory;
15
use Spryker\Zed\Form\Communication\Plugin\Application\FormApplicationPlugin;
16
use Spryker\Zed\Form\FormConfig;
17
use Spryker\Zed\Form\FormDependencyProvider;
18
use SprykerTest\Shared\Testify\Helper\ConfigHelperTrait;
19
use SprykerTest\Shared\Testify\Helper\ModuleHelperConfigTrait;
20
use SprykerTest\Zed\Application\Helper\ApplicationHelperTrait;
21
use SprykerTest\Zed\EventDispatcher\Helper\EventDispatcherHelperTrait;
22
use SprykerTest\Zed\Testify\Helper\Business\BusinessHelperTrait;
23
use SprykerTest\Zed\Testify\Helper\Communication\CommunicationHelperTrait;
24
use SprykerTest\Zed\Testify\Helper\Communication\DependencyProviderHelperTrait;
25
26
class FormHelper extends Module
27
{
28
    use ApplicationHelperTrait;
29
    use CommunicationHelperTrait;
30
    use BusinessHelperTrait;
31
    use ConfigHelperTrait;
32
    use DependencyProviderHelperTrait;
33
    use EventDispatcherHelperTrait;
34
    use ModuleHelperConfigTrait;
35
36
    /**
37
     * @var string
38
     */
39
    protected const MODULE_NAME = 'Form';
40
41
    /**
42
     * @var string
43
     */
44
    protected const CONFIG_KEY_FORM_PLUGINS = 'formPlugins';
45
46
    /**
47
     * @var array<\Spryker\Shared\FormExtension\Dependency\Plugin\FormPluginInterface>
48
     */
49
    protected $formPlugins = [];
50
51
    /**
52
     * @return void
53
     */
54
    public function _initialize(): void
55
    {
56
        foreach ($this->config[static::CONFIG_KEY_FORM_PLUGINS] as $formPlugin) {
57
            $this->formPlugins[$formPlugin] = new $formPlugin();
58
        }
59
    }
60
61
    /**
62
     * @return void
63
     */
64
    protected function setDefaultConfig(): void
65
    {
66
        $this->config = [
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
            static::CONFIG_KEY_FORM_PLUGINS => [],
68
        ];
69
    }
70
71
    /**
72
     * @return void
73
     */
74
    protected function addDependencies(): void
75
    {
76
        $this->getDependencyProviderHelper()->setDependency(FormDependencyProvider::PLUGINS_FORM, $this->formPlugins);
77
    }
78
79
    /**
80
     * @param \Codeception\TestInterface $test
81
     *
82
     * @return void
83
     */
84
    public function _before(TestInterface $test): void
85
    {
86
        parent::_before($test);
87
88
        $this->addDependencies();
89
90
        $this->getApplicationHelper()->addApplicationPlugin(
91
            $this->getFormApplicationPluginStub(),
92
        );
93
    }
94
95
    /**
96
     * @return \Spryker\Zed\Form\Communication\Plugin\Application\FormApplicationPlugin
97
     */
98
    protected function getFormApplicationPluginStub()
99
    {
100
        /** @var \Spryker\Zed\Form\Communication\Plugin\Application\FormApplicationPlugin $formApplicationPlugin */
101
        $formApplicationPlugin = Stub::make(FormApplicationPlugin::class, [
102
            'getFactory' => function () {
103
                return $this->getFactory();
104
            },
105
            'getConfig' => function () {
106
                return $this->getConfig();
107
            },
108
        ]);
109
110
        return $formApplicationPlugin;
111
    }
112
113
    /**
114
     * @return \Spryker\Zed\Form\Communication\FormCommunicationFactory
115
     */
116
    protected function getFactory(): FormCommunicationFactory
117
    {
118
        /** @var \Spryker\Zed\Form\Communication\FormCommunicationFactory $formCommunicationFactory */
119
        $formCommunicationFactory = $this->getCommunicationHelper()->getFactory(static::MODULE_NAME);
120
121
        return $formCommunicationFactory;
122
    }
123
124
    /**
125
     * @return \Spryker\Zed\Form\FormConfig
126
     */
127
    protected function getConfig(): FormConfig
128
    {
129
        /** @var \Spryker\Zed\Form\FormConfig $formConfig */
130
        $formConfig = $this->getConfigHelper()->getModuleConfig(static::MODULE_NAME);
131
132
        return $formConfig;
133
    }
134
135
    /**
136
     * @param \Spryker\Shared\FormExtension\Dependency\Plugin\FormPluginInterface $formPlugin
137
     *
138
     * @return $this
139
     */
140
    public function addFormPlugin(FormPluginInterface $formPlugin)
141
    {
142
        $this->formPlugins[] = $formPlugin;
143
144
        $this->addDependencies();
145
146
        return $this;
147
    }
148
}
149