| 1 |
<?php |
| 2 |
|
| 3 |
namespace unit\Rules; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use Give\Vendors\StellarWP\Validation\Rules\In; |
| 7 |
use Give\Vendors\StellarWP\Validation\Tests\TestCase; |
| 8 |
|
| 9 |
class InTest extends TestCase |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @since 1.2.0 |
| 13 |
*/ |
| 14 |
public function testShouldAllowEquivalentValuesInArray() |
| 15 |
{ |
| 16 |
$rule = new In('foo', 1); |
| 17 |
|
| 18 |
$this->assertValidationRulePassed($rule, 'foo'); |
| 19 |
$this->assertValidationRulePassed($rule, 1); |
| 20 |
$this->assertValidationRulePassed($rule, '1'); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @since 1.2.0 |
| 25 |
*/ |
| 26 |
public function testShouldFailValuesNotInArray() |
| 27 |
{ |
| 28 |
$rule = new In('foo', 1); |
| 29 |
|
| 30 |
$this->assertValidationRuleFailed($rule, 'bar'); |
| 31 |
$this->assertValidationRuleFailed($rule, 2); |
| 32 |
$this->assertValidationRuleFailed($rule, false); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @since 1.2.0 |
| 37 |
*/ |
| 38 |
public function testShouldCreateRuleFromCommaDelimitedList() |
| 39 |
{ |
| 40 |
$rule = In::fromString('foo,1'); |
| 41 |
|
| 42 |
$this->assertValidationRulePassed($rule, 'foo'); |
| 43 |
$this->assertValidationRulePassed($rule, 1); |
| 44 |
$this->assertValidationRulePassed($rule, '1'); |
| 45 |
$this->assertValidationRuleFailed($rule, 'qux'); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @since 1.2.0 |
| 50 |
*/ |
| 51 |
public function testFromStringShouldRequireValues() |
| 52 |
{ |
| 53 |
$this->expectException(InvalidArgumentException::class); |
| 54 |
|
| 55 |
In::fromString(''); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @since 1.2.0 |
| 60 |
*/ |
| 61 |
public function testShouldThrowInvalidExceptionWithoutValues() |
| 62 |
{ |
| 63 |
$this->expectException(InvalidArgumentException::class); |
| 64 |
|
| 65 |
new In(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @since 1.2.0 |
| 70 |
*/ |
| 71 |
public function testSerializeOptionShouldRetunValuesArray() |
| 72 |
{ |
| 73 |
$rule = new In('foo', 'bar', 'baz'); |
| 74 |
$this->assertEquals(['foo', 'bar', 'baz'], $rule->serializeOption()); |
| 75 |
} |
| 76 |
} |
| 77 |
|