diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index 42fa5a2..a675ae5 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -10,7 +10,13 @@ use yii\db\{ActiveRecord, Exception, StaleObjectException}; use yii\helpers\ArrayHelper; use yii2\extensions\nestedsets\NestedSetsBehavior; -use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree, TreeWithStrictValidation}; +use yii2\extensions\nestedsets\tests\support\model\{ + ExtendableMultipleTree, + MultipleTree, + Tree, + TreeWithStrictValidation, +}; +use yii2\extensions\nestedsets\tests\support\stub\ExtendableNestedSetsBehavior; use function get_class; use function sprintf; @@ -2045,4 +2051,38 @@ public function testPrependToWithRunValidationParameterUsingStrictValidation(): 'Node name should remain unchanged after \'prependTo()\' with \'runValidation=false\'.', ); } + + public function testProtectedApplyTreeAttributeConditionRemainAccessibleToSubclasses(): void + { + $this->createDatabase(); + + $testNode = new ExtendableMultipleTree( + [ + 'name' => 'Extensibility Test Node', + 'tree' => 1, + ], + ); + + $extendableBehavior = $testNode->getBehavior('nestedSetsBehavior'); + + self::assertInstanceOf( + ExtendableNestedSetsBehavior::class, + $extendableBehavior, + '\'ExtendableMultipleTree\' should use \'ExtendableNestedSetsBehavior\'.', + ); + + $condition = ['name' => 'test']; + + $extendableBehavior->exposedApplyTreeAttributeCondition($condition); + + self::assertTrue( + $extendableBehavior->wasMethodCalled('applyTreeAttributeCondition'), + '\'applyTreeAttributeCondition\' method should remain protected to allow subclass access.', + ); + self::assertEquals( + ['and', ['name' => 'test'], ['tree' => 1]], + $condition, + '\'Tree\' attribute condition should be applied correctly when \'treeAttribute\' is enabled.', + ); + } } diff --git a/tests/phpstan-config.php b/tests/phpstan-config.php index d25d0c1..1b93c30 100644 --- a/tests/phpstan-config.php +++ b/tests/phpstan-config.php @@ -5,6 +5,7 @@ use yii\db\{ActiveQuery, ActiveRecord}; use yii2\extensions\nestedsets\{NestedSetsBehavior, NestedSetsQueryBehavior}; use yii2\extensions\nestedsets\tests\support\model\{ + ExtendableMultipleTree, MultipleTree, MultipleTreeQuery, Tree, @@ -20,6 +21,9 @@ ActiveQuery::class => [ NestedSetsQueryBehavior::class, ], + ExtendableMultipleTree::class => [ + NestedSetsBehavior::class, + ], MultipleTree::class => [ NestedSetsBehavior::class, ], diff --git a/tests/support/model/ExtendableMultipleTree.php b/tests/support/model/ExtendableMultipleTree.php new file mode 100644 index 0000000..c7e3335 --- /dev/null +++ b/tests/support/model/ExtendableMultipleTree.php @@ -0,0 +1,59 @@ + [ + 'class' => ExtendableNestedSetsBehavior::class, + 'treeAttribute' => 'tree', + ], + ]; + } + + public function rules(): array + { + return [ + ['name', 'required'], + ]; + } + + /** + * @phpstan-return array + */ + public function transactions(): array + { + return [ + self::SCENARIO_DEFAULT => self::OP_ALL, + ]; + } + + /** + * @phpstan-return ExtendableMultipleTreeQuery + */ + public static function find(): ExtendableMultipleTreeQuery + { + return new ExtendableMultipleTreeQuery(static::class); + } +} diff --git a/tests/support/model/ExtendableMultipleTreeQuery.php b/tests/support/model/ExtendableMultipleTreeQuery.php new file mode 100644 index 0000000..adb17d9 --- /dev/null +++ b/tests/support/model/ExtendableMultipleTreeQuery.php @@ -0,0 +1,32 @@ + + */ +final class ExtendableMultipleTreeQuery extends ActiveQuery +{ + /** + * @phpstan-param class-string $modelClass + * @phpstan-param array $config + */ + public function __construct(string $modelClass, array $config = []) + { + parent::__construct($modelClass, $config); + } + + public function behaviors(): array + { + return [ + 'nestedSetsQueryBehavior' => NestedSetsQueryBehavior::class, + ]; + } +} diff --git a/tests/support/stub/ExtendableNestedSetsBehavior.php b/tests/support/stub/ExtendableNestedSetsBehavior.php new file mode 100644 index 0000000..9f56be0 --- /dev/null +++ b/tests/support/stub/ExtendableNestedSetsBehavior.php @@ -0,0 +1,91 @@ + + */ +final class ExtendableNestedSetsBehavior extends NestedSetsBehavior +{ + /** + * @phpstan-var array + */ + public array $calledMethods = []; + + /** + * @phpstan-param array $condition + */ + public function exposedApplyTreeAttributeCondition(array &$condition): void + { + $this->calledMethods['applyTreeAttributeCondition'] = true; + + $this->applyTreeAttributeCondition($condition); + } + + public function exposedBeforeInsertNode(int|null $value, int $depth): void + { + $this->calledMethods['beforeInsertNode'] = true; + + $this->beforeInsertNode($value, $depth); + } + + public function exposedBeforeInsertRootNode(): void + { + $this->calledMethods['beforeInsertRootNode'] = true; + + $this->beforeInsertRootNode(); + } + + public function exposedDeleteWithChildrenInternal(): bool|int + { + $this->calledMethods['deleteWithChildrenInternal'] = true; + + return $this->deleteWithChildrenInternal(); + } + + public function exposedMoveNode(ActiveRecord $node, int $value, int $depth): void + { + $this->calledMethods['moveNode'] = true; + + $this->moveNode($node, $value, $depth); + } + + public function exposedMoveNodeAsRoot(): void + { + $this->calledMethods['moveNodeAsRoot'] = true; + + $this->moveNodeAsRoot(); + } + + public function exposedShiftLeftRightAttribute(int $value, int $delta): void + { + $this->calledMethods['shiftLeftRightAttribute'] = true; + + $this->shiftLeftRightAttribute($value, $delta); + } + + public function resetMethodCallTracking(): void + { + $this->calledMethods = []; + } + + public function wasMethodCalled(string $methodName): bool + { + return $this->calledMethods[$methodName] ?? false; + } + + /** + * @phpstan-return array + */ + public function getCalledMethods(): array + { + return $this->calledMethods; + } +}