PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0.1 4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 All 258 releases
give / vendor / vendor-prefixed / stellarwp / field-conditions / tests / unit / FieldConditionTest.php

FieldConditionTest.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at vendor/vendor-prefixed/stellarwp/field-conditions/tests/unit/FieldConditionTest.php

171 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 use Give\Vendors\StellarWP\FieldConditions\FieldCondition;
6 use Give\Vendors\StellarWP\FieldConditions\Tests\TestCase;
7
8 class FieldConditionTest extends TestCase
9 {
10 /**
11 * @since 1.0.0
12 *
13 * @dataProvider passConditionsDataProviders
14 */
15 public function testPassingConditions($valueToCompare, $comparisonOperator, $conditionalValue)
16 {
17 $condition = new FieldCondition('field', $comparisonOperator, $conditionalValue);
18
19 $this->assertTrue($condition->passes(['field' => $valueToCompare]));
20 }
21
22 /**
23 * @since 1.1.0 add loosely equal conditions
24 * @since 1.0.0
25 */
26 public function passConditionsDataProviders(): array
27 {
28 return [
29 ['foo', '=', 'foo'],
30 [10, '=', '10.00'],
31 ['foo', '!=', 'bar'],
32 [5, '>', 1],
33 [10, '>', '1'],
34 [5, '>=', 5],
35 [1, '<', 5],
36 [5, '<=', 5],
37 ['foo', 'contains', 'oo'],
38 ['1234', 'contains', '3'],
39 ['foo', 'not_contains', 'bar'],
40 ];
41 }
42
43 /**
44 * @since 1.1.0
45 *
46 * @dataProvider passStrictConditionsDataProviders
47 */
48 public function testPassingStrictConditions($valueToCompare, $comparisonOperator, $conditionalValue)
49 {
50 $condition = new FieldCondition('field', $comparisonOperator, $conditionalValue);
51 $condition->strict();
52
53 $this->assertTrue($condition->passes(['field' => $valueToCompare]));
54 }
55
56 public function passStrictConditionsDataProviders(): array
57 {
58 return [
59 [10, '=', 10],
60 [10, '!=', '10'],
61 [10, '>', 5],
62 [10, '>=', 10],
63 [10, '<', 15],
64 [10, '<=', 10],
65 ];
66 }
67
68
69 /**
70 * @since 1.0.0
71 *
72 * @dataProvider failConditionsDataProviders
73 */
74 public function testFailingConditions($valueToCompare, $comparisonOperator, $conditionalValue)
75 {
76 $condition = new FieldCondition('field', $comparisonOperator, $conditionalValue);
77
78 $this->assertTrue($condition->fails(['field' => $valueToCompare]));
79 }
80
81 /**
82 * @since 1.0.0
83 */
84 public function failConditionsDataProviders(): array
85 {
86 return [
87 ['foo', '=', 'bar'],
88 ['foo', '!=', 'foo'],
89 [5, '>', 10],
90 [5, '>=', 10],
91 [1, '<', 0],
92 [5, '<=', 0],
93 ['foo', 'contains', 'bar'],
94 ['foo', 'not_contains', 'foo'],
95 ];
96 }
97
98 /**
99 * @since 1.1.0
100 *
101 * @dataProvider failStrictConditionsDataProviders
102 */
103 public function testFailingStrictConditions($valueToCompare, $comparisonOperator, $conditionalValue)
104 {
105 $condition = new FieldCondition('field', $comparisonOperator, $conditionalValue);
106 $condition->strict();
107
108 $this->assertTrue($condition->fails(['field' => $valueToCompare]));
109 }
110
111 /**
112 * @since 1.1.0
113 */
114 public function failStrictConditionsDataProviders(): array
115 {
116 return [
117 [10, '=', '10'],
118 [10, '!=', 10],
119 [10, '>', 15],
120 [10, '>=', 15],
121 [10, '<', 5],
122 [10, '<=', 5],
123 ['2', 'contains', 2],
124 ['2', 'not_contains', '2'],
125 ];
126 }
127
128 /**
129 * @since 1.0.0
130 */
131 public function testConditionSerialization()
132 {
133 $condition = new FieldCondition('field', '=', 'foo');
134
135 $this->assertEquals([
136 'type' => 'basic',
137 'field' => 'field',
138 'comparisonOperator' => '=',
139 'strictComparison' => false,
140 'value' => 'foo',
141 'logicalOperator' => 'and',
142 ], $condition->jsonSerialize());
143 }
144
145 /**
146 * @since 1.0.0
147 */
148 public function testGettingLogicalOperator()
149 {
150 $condition = new FieldCondition('field', '=', 'foo', 'or');
151
152 $this->assertEquals('or', $condition->getLogicalOperator());
153 }
154
155 public function testConstructorThrowsExceptionForInvalidComparisonOperator()
156 {
157 $this->expectException(InvalidArgumentException::class);
158 $this->expectExceptionMessage('Invalid comparison operator');
159
160 new FieldCondition('field', 'foo', 'bar');
161 }
162
163 public function testConstructorThrowsExceptionForInvalidLogicalOperator()
164 {
165 $this->expectException(InvalidArgumentException::class);
166 $this->expectExceptionMessage('Invalid logical operator');
167
168 new FieldCondition('field', '=', 'bar', 'foo');
169 }
170 }
171