Skip to content

Commit 8006bf0

Browse files
committed
Added even more tests
1 parent db1daee commit 8006bf0

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/JsonAttributeBehaviorTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ public function testValidateAttributeWithArray(): void
1616
$this->assertTrue((new Item(['data_json' => ['a' => 'b']]))->validate());
1717
}
1818

19+
public function testAllowNull(): void
20+
{
21+
$this->assertTrue((new Item(['data_json' => null, 'scenario' => Item::SCENARIO_ALLOW_EMPTY]))->validate());
22+
}
23+
24+
public function testAllowEmptyString(): void
25+
{
26+
$this->assertTrue((new Item(['data_json' => '', 'scenario' => Item::SCENARIO_ALLOW_EMPTY]))->validate());
27+
}
28+
29+
public function testAllowEmptyArray(): void
30+
{
31+
$this->assertTrue((new Item(['data_json' => [], 'scenario' => Item::SCENARIO_ALLOW_EMPTY]))->validate());
32+
}
33+
1934
public function testValueIsOriginalTypeAfterValidationForString(): void
2035
{
2136
$item = new Item(['data_json' => '{"a": "b"}']);
@@ -33,6 +48,19 @@ public function testValueIsOriginalTypeAfterValidationForArray(): void
3348

3449
class Item extends Model
3550
{
51+
52+
public const SCENARIO_ALLOW_EMPTY = 'allowEmpty';
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
public function scenarios(): array
58+
{
59+
$scenarios = parent::scenarios();
60+
$scenarios[self::SCENARIO_ALLOW_EMPTY] = ['data_json'];
61+
return $scenarios;
62+
}
63+
3664
/**
3765
* @var string|array
3866
*/
@@ -52,4 +80,15 @@ public function behaviors(): array
5280
];
5381
return $behaviors;
5482
}
83+
84+
/**
85+
* @inheritdoc
86+
*/
87+
public function rules(): array
88+
{
89+
$rules = parent::rules();
90+
$rules[] = [['data_json'], 'required', 'except' => 'allowEmpty'];
91+
$rules[] = [['data_json'], 'safe', 'on' => 'allowEmpty'];
92+
return $rules;
93+
}
5594
}

0 commit comments

Comments
 (0)