Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion tests/NestedSetsQueryBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use LogicException;
use yii\helpers\ArrayHelper;
use yii2\extensions\nestedsets\NestedSetsQueryBehavior;
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree, TreeQuery};
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree, TreeQuery, TreeWithStrictValidation};

final class NestedSetsQueryBehaviorTest extends TestCase
{
Expand Down Expand Up @@ -70,4 +70,57 @@ public function testThrowLogicExceptionWhenBehaviorIsDetachedFromOwner(): void

$behavior->leaves();
}

public function testAppendToWithRunValidationParameterUsingStrictValidation(): void
{
$this->generateFixtureTree();

$targetNode = Tree::findOne(2);

self::assertNotNull(
$targetNode,
'Target node with ID \'2\' should exist before calling \'appendTo\'.',
);

$invalidNode = new TreeWithStrictValidation(['name' => 'x']);

$result1 = $invalidNode->appendTo($targetNode);
$hasError1 = $invalidNode->hasErrors();

self::assertFalse(
$result1,
'\'appendTo()\' should return \'false\' when \'runValidation=true\' and data fails validation.',
);
self::assertTrue(
$hasError1,
'Node should have validation errors when \'runValidation=true\' and data is invalid.',
);

$invalidNode2 = new TreeWithStrictValidation(['name' => 'x']);

$result2 = $invalidNode2->appendTo($targetNode, false);
$hasError2 = $invalidNode2->hasErrors();

self::assertTrue(
$result2,
'\'appendTo()\' should return \'true\' when \'runValidation=false\', even with invalid data ' .
'that would fail validation.',
);
self::assertFalse(
$hasError2,
'Node should not have validation errors when \'runValidation=false\' because validation was skipped.',
);

$persistedNode = TreeWithStrictValidation::findOne($invalidNode2->id);

self::assertNotNull(
$persistedNode,
"Node with ID '{$persistedNode?->id}' should exist after appending to target node.",
);
self::assertNotEquals(
$hasError1,
$hasError2,
'Validation error states should differ between \'runValidation=true\' and \'runValidation=false\'.',
);
}
}
11 changes: 10 additions & 1 deletion tests/phpstan-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

use yii\db\{ActiveQuery, ActiveRecord};
use yii2\extensions\nestedsets\{NestedSetsBehavior, NestedSetsQueryBehavior};
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, MultipleTreeQuery, Tree, TreeQuery};
use yii2\extensions\nestedsets\tests\support\model\{
MultipleTree,
MultipleTreeQuery,
Tree,
TreeQuery,
TreeWithStrictValidation,
};

return [
'behaviors' => [
Expand All @@ -26,5 +32,8 @@
TreeQuery::class => [
NestedSetsQueryBehavior::class,
],
TreeWithStrictValidation::class => [
NestedSetsBehavior::class,
],
],
];
24 changes: 24 additions & 0 deletions tests/support/model/TreeWithStrictValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace yii2\extensions\nestedsets\tests\support\model;

/**
* @property int $id
* @property int $lft
* @property int $rgt
* @property int $depth
* @property string $name
*/
final class TreeWithStrictValidation extends Tree
{
public function rules(): array
{
return [
['name', 'required', 'message' => 'Name cannot be blank.'],
['name', 'string', 'min' => 5, 'message' => 'Name must be at least 5 characters long.'],
['name', 'match', 'pattern' => '/^[A-Z]/', 'message' => 'Name must start with an uppercase letter.'],
];
}
}