Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Oct 27, 2023
1 parent 2d05820 commit c305ec3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/actions/EditComponentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
25 changes: 20 additions & 5 deletions src/actions/MakeModelFromArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -29,7 +31,7 @@ class MakeModelFromArray
* @param class-string<T> $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();
Expand All @@ -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);

Expand All @@ -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,
Expand All @@ -78,6 +89,7 @@ className: $type->getName(),
}
}
}
// }

$reflect = new \ReflectionClass($model);
foreach ($reflect->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
Expand All @@ -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, '');

Expand Down
2 changes: 1 addition & 1 deletion src/actions/MoveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down
2 changes: 1 addition & 1 deletion src/enums/MoveComponentPosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

enum MoveComponentPosition: string
{
case BEFOREEND = 'beforeEnd';
case BEFOREEND = 'beforeend';
case BEFORE = 'before';
case AFTER = 'after';
}
3 changes: 1 addition & 2 deletions tests/MoveComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
|
*/

use markhuot\keystone\behaviors\CssRuleBehavior;

uses(
markhuot\craftpest\test\TestCase::class,
markhuot\craftpest\test\RefreshesDatabase::class,
)->in('./');

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('./');

/*
Expand Down

0 comments on commit c305ec3

Please sign in to comment.