diff --git a/src/actions/EditComponentData.php b/src/actions/EditComponentData.php index 14ea1de..0f90725 100644 --- a/src/actions/EditComponentData.php +++ b/src/actions/EditComponentData.php @@ -10,7 +10,7 @@ class EditComponentData { public function handle(Component $component, array $data) { - if ($this->isEqual($component->data->data, $data)) { + if ($this->isEqual($component->data->getData(), $data)) { return $component; } diff --git a/src/actions/MakeModelFromArray.php b/src/actions/MakeModelFromArray.php index 4697848..ff4514c 100644 --- a/src/actions/MakeModelFromArray.php +++ b/src/actions/MakeModelFromArray.php @@ -6,6 +6,8 @@ use markhuot\keystone\db\ActiveRecord; use yii\base\ModelEvent; +use function markhuot\craftpest\helpers\test\dd; + /** * Recursively create models from an array. * @@ -29,7 +31,7 @@ class MakeModelFromArray * @param class-string $className * @return T */ - public function handle(string $className, array $data, $validate = true, $errorOnMissing = false, $createOnMissing = true): mixed + public function handle(string $className, mixed $data, $validate = true, $errorOnMissing = false, $createOnMissing = true): mixed { if (is_subclass_of($className, ActiveRecord::class)) { $primaryKey = $className::primaryKey(); @@ -38,7 +40,12 @@ public function handle(string $className, array $data, $validate = true, $errorO } $condition = array_flip($primaryKey); foreach ($condition as $key => &$value) { - $value = $data[$key]; + if (is_array($data)) { + $value = $data[$key]; + } + // if (count($condition) === 1) { + // $value = $data; + // } } $condition = array_filter($condition); @@ -61,15 +68,19 @@ public function handle(string $className, array $data, $validate = true, $errorO $reflect = new \ReflectionClass($model); + // if (is_array($data)) { foreach ($data as $key => &$value) { if ($reflect->hasProperty($key)) { $property = $reflect->getProperty($key); - $type = $property->getType(); + $type = $property->getType()->getName(); - if (class_exists($type)) { + if (enum_exists($type)) { + $value = $type::from($value); + } + else if (class_exists($type)) { $value = (new static) ->handle( - className: $type->getName(), + className: $type, data: $value, validate: true, errorOnMissing: false, @@ -78,6 +89,7 @@ className: $type->getName(), } } } + // } $reflect = new \ReflectionClass($model); foreach ($reflect->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { @@ -86,6 +98,9 @@ className: $type->getName(), unset($data[$property->getName()]); } } + if ($model instanceof \markhuot\keystone\models\http\MoveComponentRequest) { + // \markhuot\craftpest\helpers\test\dd(array_keys($data)); + } $model->load($data, ''); diff --git a/src/actions/MoveComponent.php b/src/actions/MoveComponent.php index bff29d3..144d9a0 100644 --- a/src/actions/MoveComponent.php +++ b/src/actions/MoveComponent.php @@ -19,7 +19,7 @@ public function handle(Component $source, Component $target, MoveComponentPositi } } - public function handleAboveOrBelow(Component $source, Component $target, string $position) + public function handleAboveOrBelow(Component $source, Component $target, MoveComponentPosition $position) { // get the change in depth/level $originalChildPath = implode('/', array_filter([$source->path, $source->id])); diff --git a/src/enums/MoveComponentPosition.php b/src/enums/MoveComponentPosition.php index c0c9c8c..04d455a 100644 --- a/src/enums/MoveComponentPosition.php +++ b/src/enums/MoveComponentPosition.php @@ -4,7 +4,7 @@ enum MoveComponentPosition: string { - case BEFOREEND = 'beforeEnd'; + case BEFOREEND = 'beforeend'; case BEFORE = 'before'; case AFTER = 'after'; } diff --git a/tests/MoveComponentsTest.php b/tests/MoveComponentsTest.php index a5f6021..80afb50 100644 --- a/tests/MoveComponentsTest.php +++ b/tests/MoveComponentsTest.php @@ -23,7 +23,6 @@ 'target' => ['id' => $target->id, 'fieldId' => $target->fieldId, 'elementId' => $target->elementId], 'position' => 'beforeend' ]); - \markhuot\craftpest\helpers\test\dd($data); expect($data) ->errors->toBeEmpty() @@ -50,7 +49,7 @@ Component::factory()->create(['sortOrder' => 2]), ]); - (new MoveComponent())->handle($components[0], $components[2], MoveComponentPosition::BEFORE); + (new MoveComponent())->handle($components[0], $components[2], MoveComponentPosition::AFTER); $components->each->refresh(); expect($components[0])->sortOrder->toBe(2); diff --git a/tests/Pest.php b/tests/Pest.php index ba80d66..8d0274c 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -11,6 +11,8 @@ | */ +use markhuot\keystone\behaviors\CssRuleBehavior; + uses( markhuot\craftpest\test\TestCase::class, markhuot\craftpest\test\RefreshesDatabase::class, @@ -18,12 +20,11 @@ uses()->beforeEach(function () { Craft::setAlias('@templates', __DIR__.'/templates'); + Craft::$app->getView()->attachBehaviors(['cssRules' => CssRuleBehavior::class]); })->in('./'); uses()->afterEach(function () { - if (Craft::$app->getView()->getBehavior('cssRules')) { - Craft::$app->getView()->clearCssRules(); - } + Craft::$app->getView()->clearCssRules(); })->in('./'); /*