Completed
Push — pagerfanta-fix ( 187c46...923c07 )
by Kamil
25:06 queued 03:45
created

MoneyFilterSpec   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 209
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A it_is_initializable() 0 4 1
A it_implements_filter_interface() 0 4 1
A it_does_nothing_when_there_is_no_data() 0 9 1
B it_filters_by_total_alone_in_all_currencies_when_none_has_been_given() 0 32 1
B it_filters_by_given_currency() 0 38 1
B it_filters_money_greater_than() 0 25 1
B it_filters_money_less_than() 0 25 1
B it_filters_money_in_specified_range() 0 29 1
B its_amount_scale_can_be_configured() 0 32 1
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\Component\Grid\Filter;
13
14
use PhpSpec\ObjectBehavior;
15
use Prophecy\Argument;
16
use Sylius\Component\Grid\Data\DataSourceInterface;
17
use Sylius\Component\Grid\Data\ExpressionBuilderInterface;
18
use Sylius\Component\Grid\Filter\MoneyFilter;
19
use Sylius\Component\Grid\Filtering\FilterInterface;
20
21
/**
22
 * @author Jan Góralski <[email protected]>
23
 */
24
final class MoneyFilterSpec extends ObjectBehavior
25
{
26
    function it_is_initializable()
27
    {
28
        $this->shouldHaveType(MoneyFilter::class);
29
    }
30
31
    function it_implements_filter_interface()
32
    {
33
        $this->shouldImplement(FilterInterface::class);
34
    }
35
36
    function it_does_nothing_when_there_is_no_data(DataSourceInterface $dataSource)
37
    {
38
        $this->apply(
39
            $dataSource,
40
            'total',
41
            [],
42
            ['currency_field' => 'currencyCode']
43
        );
44
    }
45
46
    function it_filters_by_total_alone_in_all_currencies_when_none_has_been_given(
47
        DataSourceInterface $dataSource,
48
        ExpressionBuilderInterface $expressionBuilder
49
    ) {
50
        $dataSource->getExpressionBuilder()->willReturn($expressionBuilder);
51
52
        $expressionBuilder
53
            ->greaterThan('total', 1200)
54
            ->shouldBeCalledTimes(2)
55
        ;
56
57
        $this->apply(
58
            $dataSource,
59
            'total',
60
            [
61
                'greaterThan' => '12.00',
62
                'lessThan' => '',
63
                'currency' => '',
64
            ],
65
            ['currency_field' => 'currencyCode']
66
        );
67
68
        $this->apply(
69
            $dataSource,
70
            'total',
71
            [
72
                'greaterThan' => '12.00',
73
                'lessThan' => '',
74
            ],
75
            ['currency_field' => 'currencyCode']
76
        );
77
    }
78
79
    function it_filters_by_given_currency(
80
        DataSourceInterface $dataSource,
81
        ExpressionBuilderInterface $expressionBuilder
82
    ) {
83
        $dataSource->getExpressionBuilder()->willReturn($expressionBuilder);
84
85
        $expressionBuilder->equals('currencyCode', 'GBP')->willReturn('EXPR');
86
        $dataSource->restrict('EXPR')->shouldBeCalled();
87
88
        $expressionBuilder
89
            ->greaterThan('total', Argument::any())
90
            ->shouldNotBeCalled()
91
        ;
92
        $expressionBuilder
93
            ->lessThan('total', Argument::any())
94
            ->shouldNotBeCalled()
95
        ;
96
97
        $this->apply(
98
            $dataSource,
99
            'total',
100
            [
101
                'currency' => 'GBP',
102
            ],
103
            ['currency_field' => 'currencyCode']
104
        );
105
106
        $this->apply(
107
            $dataSource,
108
            'total',
109
            [
110
                'greaterThan' => '',
111
                'lessThan' => '',
112
                'currency' => 'GBP',
113
            ],
114
            ['currency_field' => 'currencyCode']
115
        );
116
    }
117
118
    function it_filters_money_greater_than(
119
        DataSourceInterface $dataSource,
120
        ExpressionBuilderInterface $expressionBuilder
121
    ) {
122
        $dataSource->getExpressionBuilder()->willReturn($expressionBuilder);
123
124
        $expressionBuilder->equals('currencyCode', 'GBP')->willReturn('EXPR');
125
        $dataSource->restrict('EXPR')->shouldBeCalled();
126
127
        $expressionBuilder
128
            ->greaterThan('total', 1200)
129
            ->shouldBeCalled()
130
        ;
131
132
        $this->apply(
133
            $dataSource,
134
            'total',
135
            [
136
                'greaterThan' => '12.00',
137
                'lessThan' => '',
138
                'currency' => 'GBP',
139
            ],
140
            ['currency_field' => 'currencyCode']
141
        );
142
    }
143
144
    function it_filters_money_less_than(
145
        DataSourceInterface $dataSource,
146
        ExpressionBuilderInterface $expressionBuilder
147
    ) {
148
        $dataSource->getExpressionBuilder()->willReturn($expressionBuilder);
149
150
        $expressionBuilder->equals('currencyCode', 'GBP')->willReturn('EXPR');
151
        $dataSource->restrict('EXPR')->shouldBeCalled();
152
153
        $expressionBuilder
154
            ->lessThan('total', 12000)
155
            ->shouldBeCalled()
156
        ;
157
158
        $this->apply(
159
            $dataSource,
160
            'total',
161
            [
162
                'greaterThan' => '',
163
                'lessThan' => '120.00',
164
                'currency' => 'GBP',
165
            ],
166
            ['currency_field' => 'currencyCode']
167
        );
168
    }
169
170
    function it_filters_money_in_specified_range(
171
        DataSourceInterface $dataSource,
172
        ExpressionBuilderInterface $expressionBuilder
173
    ) {
174
        $dataSource->getExpressionBuilder()->willReturn($expressionBuilder);
175
176
        $expressionBuilder->equals('currencyCode', 'GBP')->willReturn('EXPR');
177
        $dataSource->restrict('EXPR')->shouldBeCalled();
178
179
        $expressionBuilder
180
            ->greaterThan('total', 1200)
181
            ->shouldBeCalled()
182
        ;
183
        $expressionBuilder
184
            ->lessThan('total', 12000)
185
            ->shouldBeCalled()
186
        ;
187
188
        $this->apply(
189
            $dataSource,
190
            'total',
191
            [
192
                'greaterThan' => '12.00',
193
                'lessThan' => '120.00',
194
                'currency' => 'GBP',
195
            ],
196
            ['currency_field' => 'currencyCode']
197
        );
198
    }
199
200
    function its_amount_scale_can_be_configured(
201
        DataSourceInterface $dataSource,
202
        ExpressionBuilderInterface $expressionBuilder
203
    ) {
204
        $dataSource->getExpressionBuilder()->willReturn($expressionBuilder);
205
206
        $expressionBuilder->equals('currencyCode', 'GBP')->willReturn('EXPR');
207
        $dataSource->restrict('EXPR')->shouldBeCalled();
208
209
        $expressionBuilder
210
            ->greaterThan('total', 1200000)
211
            ->shouldBeCalled()
212
        ;
213
        $expressionBuilder
214
            ->lessThan('total', 12000000)
215
            ->shouldBeCalled()
216
        ;
217
218
        $this->apply(
219
            $dataSource,
220
            'total',
221
            [
222
                'greaterThan' => '12',
223
                'lessThan' => '120',
224
                'currency' => 'GBP',
225
            ],
226
            [
227
                'currency_field' => 'currencyCode',
228
                'scale' => 5,
229
            ]
230
        );
231
    }
232
}
233