give
/
vendor
/
vendor-prefixed
/
stellarwp
/
field-conditions
/
tests
/
unit
/
SimpleConditionSetTest.php
SimpleConditionSetTest.php in GiveWP – Donation Plugin and Fundraising Platform 4.17.0, at vendor/vendor-prefixed/stellarwp/field-conditions/tests/unit/SimpleConditionSetTest.php
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | use Give\Vendors\StellarWP\FieldConditions\FieldCondition; |
| 6 | use Give\Vendors\StellarWP\FieldConditions\SimpleConditionSet; |
| 7 | use Give\Vendors\StellarWP\FieldConditions\Tests\TestCase; |
| 8 | |
| 9 | class SimpleConditionSetTest extends TestCase |
| 10 | { |
| 11 | /** |
| 12 | * @since 1.0.0 |
| 13 | */ |
| 14 | public function testShouldRunAndPassConditionsWithLogicalAnd() |
| 15 | { |
| 16 | $condition1 = $this->createMock(FieldCondition::class); |
| 17 | $condition1->method('passes')->with([])->willReturn(true); |
| 18 | $condition1->method('getLogicalOperator')->willReturn('and'); |
| 19 | |
| 20 | $condition2 = $this->createMock(FieldCondition::class); |
| 21 | $condition2->method('passes')->with([])->willReturn(true); |
| 22 | $condition2->method('getLogicalOperator')->willReturn('and'); |
| 23 | |
| 24 | $conditionSet = new SimpleConditionSet($condition1, $condition2); |
| 25 | |
| 26 | self::assertTrue($conditionSet->passes([])); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | public function testShouldRunAndPassConditionsWithLogicalOr() |
| 33 | { |
| 34 | $condition1 = $this->createMock(FieldCondition::class); |
| 35 | $condition1->method('passes')->with([])->willReturn(true); |
| 36 | $condition1->method('getLogicalOperator')->willReturn('or'); |
| 37 | |
| 38 | $condition2 = $this->createMock(FieldCondition::class); |
| 39 | $condition2->method('passes')->with([])->willReturn(false); |
| 40 | $condition2->method('getLogicalOperator')->willReturn('or'); |
| 41 | |
| 42 | $conditionSet = new SimpleConditionSet($condition1, $condition2); |
| 43 | |
| 44 | self::assertTrue($conditionSet->passes([])); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public function testShouldRunAndFailConditionsWithLogicalAnd() |
| 51 | { |
| 52 | $condition1 = $this->createMock(FieldCondition::class); |
| 53 | $condition1->method('passes')->with([])->willReturn(true); |
| 54 | $condition1->method('getLogicalOperator')->willReturn('and'); |
| 55 | |
| 56 | $condition2 = $this->createMock(FieldCondition::class); |
| 57 | $condition2->method('passes')->with([])->willReturn(false); |
| 58 | $condition2->method('getLogicalOperator')->willReturn('and'); |
| 59 | |
| 60 | $conditionSet = new SimpleConditionSet($condition1, $condition2); |
| 61 | |
| 62 | self::assertTrue($conditionSet->fails([])); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @since 1.0.0 |
| 67 | */ |
| 68 | public function testShouldRunAndFailConditionsWithLogicalOr() |
| 69 | { |
| 70 | $condition1 = $this->createMock(FieldCondition::class); |
| 71 | $condition1->method('passes')->with([])->willReturn(false); |
| 72 | $condition1->method('getLogicalOperator')->willReturn('and'); |
| 73 | |
| 74 | $condition2 = $this->createMock(FieldCondition::class); |
| 75 | $condition2->method('passes')->with([])->willReturn(false); |
| 76 | $condition2->method('getLogicalOperator')->willReturn('or'); |
| 77 | |
| 78 | $conditionSet = new SimpleConditionSet($condition1, $condition2); |
| 79 | |
| 80 | self::assertTrue($conditionSet->fails([])); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @since 1.0.0 |
| 85 | */ |
| 86 | public function testShouldReturnConditions() |
| 87 | { |
| 88 | $condition = $this->createMock(FieldCondition::class); |
| 89 | $conditionSet = new SimpleConditionSet($condition); |
| 90 | |
| 91 | $this->assertSame([$condition], $conditionSet->getConditions()); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @since 1.0.0 |
| 96 | */ |
| 97 | public function testShouldAddConditions() |
| 98 | { |
| 99 | $condition1 = $this->createMock(FieldCondition::class); |
| 100 | $condition2 = $this->createMock(FieldCondition::class); |
| 101 | $conditionSet = new SimpleConditionSet($condition1); |
| 102 | |
| 103 | $conditionSet->append($condition2); |
| 104 | |
| 105 | $this->assertSame([$condition1, $condition2], $conditionSet->getConditions()); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @since 1.0.0 |
| 110 | */ |
| 111 | public function testShouldReturnJsonSerializedConditions() |
| 112 | { |
| 113 | $condition = $this->createMock(FieldCondition::class); |
| 114 | $condition->method('jsonSerialize')->willReturn(['foo' => 'bar']); |
| 115 | $conditionSet = new SimpleConditionSet($condition); |
| 116 | |
| 117 | $this->assertSame([['foo' => 'bar']], $conditionSet->jsonSerialize()); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @since 1.0.0 |
| 122 | */ |
| 123 | public function testShouldAllowIterationOverConditions() |
| 124 | { |
| 125 | $condition = $this->createMock(FieldCondition::class); |
| 126 | $conditionSet = new SimpleConditionSet($condition); |
| 127 | |
| 128 | foreach($conditionSet as $condition) { |
| 129 | self::assertInstanceOf(FieldCondition::class, $condition); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 |