Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 41 additions & 1 deletion tests/NestedSetsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.',
);
}
}
4 changes: 4 additions & 0 deletions tests/phpstan-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,6 +21,9 @@
ActiveQuery::class => [
NestedSetsQueryBehavior::class,
],
ExtendableMultipleTree::class => [
NestedSetsBehavior::class,
],
MultipleTree::class => [
NestedSetsBehavior::class,
],
Expand Down
59 changes: 59 additions & 0 deletions tests/support/model/ExtendableMultipleTree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

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

use yii\db\ActiveRecord;
use yii2\extensions\nestedsets\tests\support\stub\ExtendableNestedSetsBehavior;

/**
* @property int $id
* @property int $depth
* @property int $lft
* @property int $rgt
* @property int $tree
* @property string $name
*/
class ExtendableMultipleTree extends ActiveRecord
{
public static function tableName(): string
{
return '{{%multiple_tree}}';
}

public function behaviors(): array
{
return [
'nestedSetsBehavior' => [
'class' => ExtendableNestedSetsBehavior::class,
'treeAttribute' => 'tree',
],
];
}

public function rules(): array
{
return [
['name', 'required'],
];
}

/**
* @phpstan-return array<string, int>
*/
public function transactions(): array
{
return [
self::SCENARIO_DEFAULT => self::OP_ALL,
];
}

/**
* @phpstan-return ExtendableMultipleTreeQuery<static>
*/
public static function find(): ExtendableMultipleTreeQuery
{
return new ExtendableMultipleTreeQuery(static::class);
}
}
32 changes: 32 additions & 0 deletions tests/support/model/ExtendableMultipleTreeQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

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

use yii\db\ActiveQuery;
use yii2\extensions\nestedsets\NestedSetsQueryBehavior;

/**
* @template T of ExtendableMultipleTree
*
* @extends ActiveQuery<T>
*/
final class ExtendableMultipleTreeQuery extends ActiveQuery
{
/**
* @phpstan-param class-string<T> $modelClass
* @phpstan-param array<string, mixed> $config
*/
public function __construct(string $modelClass, array $config = [])
{
parent::__construct($modelClass, $config);
}

public function behaviors(): array
{
return [
'nestedSetsQueryBehavior' => NestedSetsQueryBehavior::class,
];
}
}
91 changes: 91 additions & 0 deletions tests/support/stub/ExtendableNestedSetsBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

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

use yii\db\ActiveRecord;
use yii2\extensions\nestedsets\NestedSetsBehavior;

/**
* @phpstan-template T of ActiveRecord
*
* @phpstan-extends NestedSetsBehavior<T>
*/
final class ExtendableNestedSetsBehavior extends NestedSetsBehavior
{
/**
* @phpstan-var array<string, bool>
*/
public array $calledMethods = [];

/**
* @phpstan-param array<int|string, mixed> $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<string, bool>
*/
public function getCalledMethods(): array
{
return $this->calledMethods;
}
}