|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace spec\Sylius\Bundle\ShippingBundle\Form\EventSubscriber; |
|
13
|
|
|
|
|
14
|
|
|
use PhpSpec\ObjectBehavior; |
|
15
|
|
|
use Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistryInterface; |
|
16
|
|
|
use Sylius\Bundle\ShippingBundle\Form\EventSubscriber\BuildShippingMethodFormSubscriber; |
|
17
|
|
|
use Sylius\Component\Registry\ServiceRegistryInterface; |
|
18
|
|
|
use Sylius\Component\Shipping\Calculator\CalculatorInterface; |
|
19
|
|
|
use Sylius\Component\Shipping\Model\ShippingMethod; |
|
20
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
21
|
|
|
use Symfony\Component\Form\FormEvent; |
|
22
|
|
|
use Symfony\Component\Form\FormEvents; |
|
23
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
24
|
|
|
use Symfony\Component\Form\FormInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class BuildShippingMethodFormSubscriberSpec extends ObjectBehavior |
|
27
|
|
|
{ |
|
28
|
|
|
function let(ServiceRegistryInterface $calculatorRegistry, FormFactoryInterface $factory, FormTypeRegistryInterface $formRegistry) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->beConstructedWith($calculatorRegistry, $factory, $formRegistry); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
function it_is_initializable() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->shouldHaveType(BuildShippingMethodFormSubscriber::class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
function it_is_a_subscriber() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->shouldImplement(EventSubscriberInterface::class); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
function it_subscribes_to_event() |
|
44
|
|
|
{ |
|
45
|
|
|
$this::getSubscribedEvents()->shouldReturn([ |
|
46
|
|
|
FormEvents::PRE_SET_DATA => 'preSetData', |
|
47
|
|
|
FormEvents::PRE_SUBMIT => 'preSubmit', |
|
48
|
|
|
]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
function it_adds_configuration_field_on_pre_set_data( |
|
52
|
|
|
$calculatorRegistry, |
|
53
|
|
|
$factory, |
|
54
|
|
|
$formRegistry, |
|
55
|
|
|
FormEvent $event, |
|
56
|
|
|
FormInterface $form, |
|
57
|
|
|
ShippingMethod $shippingMethod, |
|
58
|
|
|
FormInterface $formConfiguration, |
|
59
|
|
|
CalculatorInterface $calculator |
|
60
|
|
|
) { |
|
61
|
|
|
$event->getData()->shouldBeCalled()->willReturn($shippingMethod); |
|
62
|
|
|
$event->getForm()->shouldBeCalled()->willReturn($form); |
|
63
|
|
|
|
|
64
|
|
|
$shippingMethod->getId()->shouldBeCalled()->willreturn(12); |
|
65
|
|
|
$shippingMethod->getCalculator()->shouldBeCalled()->willreturn('calculator_type'); |
|
66
|
|
|
$shippingMethod->getConfiguration()->shouldBeCalled()->willreturn([]); |
|
67
|
|
|
|
|
68
|
|
|
$calculatorRegistry->get('calculator_type')->shouldBeCalled()->willReturn($calculator); |
|
69
|
|
|
$calculator->getType()->shouldBeCalled()->willReturn('calculator_type'); |
|
70
|
|
|
|
|
71
|
|
|
$formRegistry->has('calculator_type', 'default')->shouldBeCalled()->willReturn(true); |
|
72
|
|
|
$formRegistry->get('calculator_type', 'default')->shouldBeCalled()->willReturn('sylius_shipping_calculator_calculator_type'); |
|
73
|
|
|
|
|
74
|
|
|
$factory->createNamed( |
|
75
|
|
|
'configuration', |
|
76
|
|
|
'sylius_shipping_calculator_calculator_type', |
|
77
|
|
|
[], |
|
78
|
|
|
['auto_initialize' => false] |
|
79
|
|
|
)->shouldBeCalled()->willReturn($formConfiguration); |
|
80
|
|
|
|
|
81
|
|
|
$form->add($formConfiguration)->shouldBeCalled(); |
|
82
|
|
|
|
|
83
|
|
|
$this->preSetData($event); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
function it_adds_configuration_field_on_post_submit( |
|
87
|
|
|
$calculatorRegistry, |
|
88
|
|
|
$factory, |
|
89
|
|
|
$formRegistry, |
|
90
|
|
|
FormEvent $event, |
|
91
|
|
|
FormInterface $form, |
|
92
|
|
|
FormInterface $formConfiguration, |
|
93
|
|
|
CalculatorInterface $calculator |
|
94
|
|
|
) { |
|
95
|
|
|
$event->getData()->shouldBeCalled()->willReturn(['calculator' => 'calculator_type']); |
|
96
|
|
|
$event->getForm()->shouldBeCalled()->willReturn($form); |
|
97
|
|
|
|
|
98
|
|
|
$calculatorRegistry->get('calculator_type')->shouldBeCalled()->willReturn($calculator); |
|
99
|
|
|
$calculator->getType()->shouldBeCalled()->willReturn('calculator_type'); |
|
100
|
|
|
|
|
101
|
|
|
$formRegistry->has('calculator_type', 'default')->shouldBeCalled()->willReturn(true); |
|
102
|
|
|
$formRegistry->get('calculator_type', 'default')->shouldBeCalled()->willReturn('sylius_shipping_calculator_calculator_type'); |
|
103
|
|
|
|
|
104
|
|
|
$factory->createNamed( |
|
105
|
|
|
'configuration', |
|
106
|
|
|
'sylius_shipping_calculator_calculator_type', |
|
107
|
|
|
[], |
|
108
|
|
|
['auto_initialize' => false] |
|
109
|
|
|
)->shouldBeCalled()->willReturn($formConfiguration); |
|
110
|
|
|
|
|
111
|
|
|
$form->add($formConfiguration)->shouldBeCalled(); |
|
112
|
|
|
|
|
113
|
|
|
$this->preSubmit($event); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|