|
7 | 7 |
|
8 | 8 | namespace yiiunit\framework\behaviors;
|
9 | 9 |
|
| 10 | +use ValueError; |
10 | 11 | use Yii;
|
11 | 12 | use yii\base\DynamicModel;
|
12 | 13 | use yii\base\Event;
|
13 | 14 | use yii\behaviors\AttributeTypecastBehavior;
|
14 | 15 | use yii\db\ActiveRecord;
|
| 16 | +use yiiunit\framework\db\enums\StatusTypeString; |
15 | 17 | use yiiunit\TestCase;
|
16 | 18 |
|
17 | 19 | /**
|
@@ -47,6 +49,7 @@ protected function setUp(): void
|
47 | 49 | 'price' => 'float',
|
48 | 50 | 'isActive' => 'boolean',
|
49 | 51 | 'callback' => 'string',
|
| 52 | + 'status' => 'string', |
50 | 53 | ];
|
51 | 54 | Yii::$app->getDb()->createCommand()->createTable('test_attribute_typecast', $columns)->execute();
|
52 | 55 | }
|
@@ -80,6 +83,55 @@ public function testTypecast()
|
80 | 83 | $this->assertSame('callback: foo', $model->callback);
|
81 | 84 | }
|
82 | 85 |
|
| 86 | + public function testTypecastEnum() |
| 87 | + { |
| 88 | + if (PHP_VERSION_ID < 80100) { |
| 89 | + $this->markTestSkipped('Can not be tested on PHP < 8.1'); |
| 90 | + } |
| 91 | + |
| 92 | + $model = new ActiveRecordAttributeTypecastWithEnum(); |
| 93 | + |
| 94 | + $model->status = StatusTypeString::ACTIVE; |
| 95 | + |
| 96 | + $model->getAttributeTypecastBehavior()->typecastAttributes(); |
| 97 | + |
| 98 | + $this->assertSame(StatusTypeString::ACTIVE, $model->status); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @depends testTypecastEnum |
| 103 | + */ |
| 104 | + public function testTypecastEnumFromString() |
| 105 | + { |
| 106 | + if (PHP_VERSION_ID < 80100) { |
| 107 | + $this->markTestSkipped('Can not be tested on PHP < 8.1'); |
| 108 | + } |
| 109 | + |
| 110 | + $model = new ActiveRecordAttributeTypecastWithEnum(); |
| 111 | + $model->status = 'active'; // Same as StatusTypeString::ACTIVE->value; |
| 112 | + |
| 113 | + $model->getAttributeTypecastBehavior()->typecastAttributes(); |
| 114 | + |
| 115 | + $this->assertSame(StatusTypeString::ACTIVE, $model->status); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @depends testTypecastEnum |
| 120 | + */ |
| 121 | + public function testTypecastEnumFailWithInvalidValue() |
| 122 | + { |
| 123 | + if (PHP_VERSION_ID < 80100) { |
| 124 | + $this->markTestSkipped('Can not be tested on PHP < 8.1'); |
| 125 | + } |
| 126 | + |
| 127 | + $model = new ActiveRecordAttributeTypecastWithEnum(); |
| 128 | + $model->status = 'invalid'; |
| 129 | + |
| 130 | + self::expectException(ValueError::class); |
| 131 | + |
| 132 | + $model->getAttributeTypecastBehavior()->typecastAttributes(); |
| 133 | + } |
| 134 | + |
83 | 135 | /**
|
84 | 136 | * @depends testTypecast
|
85 | 137 | */
|
@@ -339,3 +391,37 @@ public function getAttributeTypecastBehavior()
|
339 | 391 | return $this->getBehavior('attributeTypecast');
|
340 | 392 | }
|
341 | 393 | }
|
| 394 | + |
| 395 | +/** |
| 396 | + * Test Active Record class with [[AttributeTypecastBehavior]] behavior attached with an enum field. |
| 397 | + * |
| 398 | + * @property StatusTypeString $status |
| 399 | + */ |
| 400 | +class ActiveRecordAttributeTypecastWithEnum extends ActiveRecord |
| 401 | +{ |
| 402 | + public function behaviors() |
| 403 | + { |
| 404 | + return [ |
| 405 | + 'attributeTypecast' => [ |
| 406 | + 'class' => AttributeTypecastBehavior::className(), |
| 407 | + 'attributeTypes' => [ |
| 408 | + 'status' => StatusTypeString::class, |
| 409 | + ], |
| 410 | + 'typecastBeforeSave' => true, |
| 411 | + ], |
| 412 | + ]; |
| 413 | + } |
| 414 | + |
| 415 | + public static function tableName() |
| 416 | + { |
| 417 | + return 'test_attribute_typecast'; |
| 418 | + } |
| 419 | + |
| 420 | + /** |
| 421 | + * @return AttributeTypecastBehavior |
| 422 | + */ |
| 423 | + public function getAttributeTypecastBehavior() |
| 424 | + { |
| 425 | + return $this->getBehavior('attributeTypecast'); |
| 426 | + } |
| 427 | +} |
0 commit comments