Skip to content

Commit cfbfe27

Browse files
docs: Enhance test classes with comprehensive documentation for nested sets behavior. (#70)
1 parent 5018dd7 commit cfbfe27

11 files changed

+307
-15
lines changed

tests/base/AbstractCacheManagement.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,34 @@
1212
use yii2\extensions\nestedsets\tests\support\stub\ExtendableNestedSetsBehavior;
1313
use yii2\extensions\nestedsets\tests\TestCase;
1414

15+
/**
16+
* Base class for cache invalidation tests in nested sets tree behaviors.
17+
*
18+
* Provides a comprehensive suite of integration and unit tests for cache management in nested sets tree structures,
19+
* ensuring correct cache population, invalidation, and memoization across various node operations and scenarios.
20+
*
21+
* This class validates the cache lifecycle for the nested sets behavior by simulating node insertions, updates,
22+
* deletions, and structural changes, covering both single and multiple tree models.
23+
*
24+
* The tests ensure that cache values for depth, left, and right attributes are correctly populated, invalidated, and
25+
* memoized, and that cache invalidation is triggered by all relevant operations, including manual and automatic cases.
26+
*
27+
* Key features.
28+
* - Coverage for tree attribute handling and owner assignment.
29+
* - Integration tests for cache invalidation after node insert, update, append, delete, and makeRoot operations.
30+
* - Memoization tests for depth, left, and right value accessors.
31+
* - Support for both single-tree and multi-tree models.
32+
* - Tests for manual and automatic cache invalidation.
33+
* - Use of mock objects to verify memoization and cache state.
34+
* - Verification of cache state before and after invalidation events.
35+
*
36+
* @see MultipleTree for multi-tree model.
37+
* @see NestedSetsBehavior for behavior implementation.
38+
* @see Tree for single-tree model.
39+
*
40+
* @copyright Copyright (C) 2023 Terabytesoftw.
41+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
42+
*/
1543
abstract class AbstractCacheManagement extends TestCase
1644
{
1745
public function testAfterInsertCacheInvalidationIntegration(): void

tests/base/AbstractExceptionHandling.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,36 @@
99
use yii\base\NotSupportedException;
1010
use yii\db\{Exception, StaleObjectException};
1111
use yii2\extensions\nestedsets\NestedSetsBehavior;
12-
use yii2\extensions\nestedsets\tests\support\model\MultipleTree;
13-
use yii2\extensions\nestedsets\tests\support\model\Tree;
12+
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree};
1413
use yii2\extensions\nestedsets\tests\TestCase;
1514

15+
/**
16+
* Base class for exception handling tests in nested sets tree behaviors.
17+
*
18+
* Provides a comprehensive suite of unit tests for exception scenarios in nested sets tree structures, ensuring correct
19+
* exception throwing and error messages for invalid node operations and edge cases.
20+
*
21+
* This class validates the robustness of the nested sets behavior by simulating invalid operations such as appending,
22+
* inserting, deleting, and making root nodes under unsupported conditions, covering both single and multiple tree
23+
* models.
24+
*
25+
* The tests ensure that exceptions are thrown with the expected messages for cases like new records, root nodes, child
26+
* nodes, same node operations, and missing primary keys, as well as logic errors when the behavior is not attached.
27+
*
28+
* Key features.
29+
* - Coverage for invalid append, insert, delete, and makeRoot operations.
30+
* - Ensures error handling consistency for unsupported operations.
31+
* - Support for both single-tree and multi-tree models.
32+
* - Tests for exception messages and types in various edge cases.
33+
* - Validation of logic exceptions when the behavior is detached or not attached to the owner.
34+
*
35+
* @see MultipleTree for multi-tree model.
36+
* @see NestedSetsBehavior for behavior implementation.
37+
* @see Tree for single-tree model.
38+
*
39+
* @copyright Copyright (C) 2023 Terabytesoftw.
40+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
41+
*/
1642
abstract class AbstractExceptionHandling extends TestCase
1743
{
1844
public function testThrowExceptionWhenAppendToNewNodeTargetIsNewRecord(): void
@@ -84,8 +110,9 @@ public function testThrowExceptionWhenAppendToTargetIsSame(): void
84110
}
85111

86112
/**
87-
* @throws StaleObjectException
88-
* @throws Throwable
113+
* @throws StaleObjectException if optimistic, locking is enabled and the data to be deleted has been modified by
114+
* another process.
115+
* @throws Throwable if an unexpected error occurs during execution.
89116
*/
90117
public function testThrowExceptionWhenDeleteNodeIsNewRecord(): void
91118
{
@@ -442,8 +469,9 @@ public function testThrowLogicExceptionWhenBehaviorIsNotAttachedToOwner(): void
442469
}
443470

444471
/**
445-
* @throws StaleObjectException
446-
* @throws Throwable
472+
* @throws StaleObjectException if optimistic, locking is enabled and the data to be deleted has been modified by
473+
* another process.
474+
* @throws Throwable if an unexpected error occurs during execution.
447475
*/
448476
public function testThrowNotSupportedExceptionWhenDeleteIsCalledOnRootNode(): void
449477
{
@@ -465,7 +493,7 @@ public function testThrowNotSupportedExceptionWhenDeleteIsCalledOnRootNode(): vo
465493
}
466494

467495
/**
468-
* @throws Throwable
496+
* @throws Throwable if an unexpected error occurs during execution.
469497
*/
470498
public function testThrowNotSupportedExceptionWhenInsertIsCalledOnTree(): void
471499
{

tests/base/AbstractExtensibility.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@
88
use yii2\extensions\nestedsets\tests\support\stub\ExtendableNestedSetsBehavior;
99
use yii2\extensions\nestedsets\tests\TestCase;
1010

11+
/**
12+
* Base class for extensibility tests in nested sets tree behaviors.
13+
*
14+
* Provides a suite of unit tests to verify the extensibility and subclassing capabilities of the nested sets behavior,
15+
* ensuring protected methods remain accessible for customization and extension in descendant classes.
16+
*
17+
* This class validates that key internal methods of the nested sets behavior—such as node insertion, root insertion,
18+
* node movement, and attribute shifting—can be invoked and overridden by subclasses, supporting advanced use cases
19+
* and framework extensibility.
20+
*
21+
* The tests cover scenarios for exposing protected methods, confirming their correct execution and the ability to
22+
* customize node state during tree operations in both single-tree and multi-tree models.
23+
*
24+
* Key features.
25+
* - Ensures protected methods are accessible for subclass extension.
26+
* - Supports both single-tree and multi-tree model scenarios.
27+
* - Tests before-insert and move operations for extensibility.
28+
* - Validates extensibility for root and non-root node operations.
29+
* - Verifies correct attribute assignment by protected methods.
30+
*
31+
* @see ExtendableMultipleTree for extensible multi-tree model.
32+
* @see ExtendableNestedSetsBehavior for behavior subclass exposing protected methods.
33+
*
34+
* @copyright Copyright (C) 2023 Terabytesoftw.
35+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
36+
*/
1137
abstract class AbstractExtensibility extends TestCase
1238
{
1339
public function testProtectedBeforeInsertNodeRemainsAccessibleToSubclasses(): void

tests/base/AbstractNodeAppend.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@
88
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree, TreeWithStrictValidation};
99
use yii2\extensions\nestedsets\tests\TestCase;
1010

11+
/**
12+
* Base class for node append and root promotion tests in nested sets tree behaviors.
13+
*
14+
* Provides a comprehensive suite of unit and integration tests for appending nodes and promoting nodes to root in
15+
* nested sets tree structures, ensuring correct tree structure, attribute updates, and validation logic for both
16+
* single-tree and multi-tree models.
17+
*
18+
* This class validates the correctness of node append operations, strict validation scenarios, root promotion, and XML
19+
* dataset matching after structural changes.
20+
*
21+
* It covers edge cases such as validation bypass, attribute refresh requirements, and cross-tree operations, ensuring
22+
* robust behavior for all supported node manipulations.
23+
*
24+
* Key features.
25+
* - Covers both {@see Tree} and {@see MultipleTree} model scenarios.
26+
* - Cross-tree append operations for multi-tree models.
27+
* - Ensures correct left, right, depth, and tree attribute updates.
28+
* - Root promotion and attribute refresh verification.
29+
* - Tests for appending child nodes to root and other nodes.
30+
* - Validation of strict and non-strict append operations.
31+
* - XML dataset matching after structural changes.
32+
*
33+
* @see MultipleTree for multi-tree model.
34+
* @see Tree for single-tree model.
35+
* @see TreeWithStrictValidation for strict validation scenarios.
36+
*
37+
* @copyright Copyright (C) 2023 Terabytesoftw.
38+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
39+
*/
1140
abstract class AbstractNodeAppend extends TestCase
1241
{
1342
public function testAppendChildNodeToRootCreatesValidTreeStructure(): void

tests/base/AbstractNodeDelete.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,37 @@
44

55
namespace yii2\extensions\nestedsets\tests\base;
66

7+
use PHPUnit\Framework\MockObject\Exception;
78
use Throwable;
89
use yii\db\{ActiveRecord, StaleObjectException};
910
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree};
1011
use yii2\extensions\nestedsets\tests\TestCase;
1112

13+
/**
14+
* Base class for node deletion tests in nested sets tree behaviors.
15+
*
16+
* Provides a comprehensive suite of unit tests for node deletion operations in nested sets tree structures, ensuring
17+
* correct state transitions, affected row counts, and data integrity after node and subtree deletions.
18+
*
19+
* This class validates the behavior of the nested sets implementation by simulating node deletions, subtree removals,
20+
* and update operations, covering both single and multiple tree models.
21+
*
22+
* The tests also cover abort scenarios for deletions, transactional behavior, and update operations on node attributes.
23+
*
24+
* Key features.
25+
* - Covers update operations and affected row count for node attribute changes.
26+
* - Ensures correct affected row counts for node and subtree deletions in both {@see Tree} and {@see MultipleTree}
27+
* models.
28+
* - Tests aborting deletions via `beforeDelete()` and transactional behavior.
29+
* - Validates XML dataset consistency after deletions.
30+
* - Verifies node state transitions after `deleteWithChildren()` (new record status, old attributes).
31+
*
32+
* @see MultipleTree for multi-tree model.
33+
* @see Tree for single-tree model.
34+
*
35+
* @copyright Copyright (C) 2023 Terabytesoftw.
36+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
37+
*/
1238
abstract class AbstractNodeDelete extends TestCase
1339
{
1440
public function testNodeStateAfterDeleteWithChildren(): void
@@ -76,6 +102,9 @@ public function testReturnAffectedRowsAndMatchXmlAfterDeleteWithChildrenForTreeA
76102
);
77103
}
78104

105+
/**
106+
* @throws Exception if an unexpected error occurs during execution.
107+
*/
79108
public function testReturnFalseWhenDeleteWithChildrenIsAbortedByBeforeDelete(): void
80109
{
81110
$this->createDatabase();
@@ -112,8 +141,9 @@ public function testReturnFalseWhenDeleteWithChildrenIsAbortedByBeforeDelete():
112141
}
113142

114143
/**
115-
* @throws StaleObjectException
116-
* @throws Throwable
144+
* @throws StaleObjectException if optimistic, locking is enabled and the data to be deleted has been modified by
145+
* another process.
146+
* @throws Throwable if an unexpected error occurs during execution.
117147
*/
118148
public function testReturnOneWhenDeleteNodeForTreeAndMultipleTree(): void
119149
{
@@ -145,8 +175,9 @@ public function testReturnOneWhenDeleteNodeForTreeAndMultipleTree(): void
145175
}
146176

147177
/**
148-
* @throws Throwable
149-
* @throws StaleObjectException
178+
* @throws StaleObjectException if optimistic, locking is enabled and the data to be deleted has been modified by
179+
* another process.
180+
* @throws Throwable if an unexpected error occurs during execution.
150181
*/
151182
public function testReturnOneWhenUpdateNodeName(): void
152183
{

tests/base/AbstractNodeInsert.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@
77
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree, TreeWithStrictValidation};
88
use yii2\extensions\nestedsets\tests\TestCase;
99

10+
/**
11+
* Base class for node insertion tests in nested sets tree behaviors.
12+
*
13+
* Provides a comprehensive suite of unit tests for node insertion operations in nested sets tree structures, ensuring
14+
* correct behavior for inserting nodes before and after targets, with and without validation, and across both single
15+
* and multiple tree models.
16+
*
17+
* This class validates the insertion logic by simulating scenarios such as inserting new and existing nodes, handling
18+
* validation rules, and verifying the resulting tree structure against expected XML datasets.
19+
*
20+
* It covers edge cases for strict validation, cross-tree insertions, and ensures that the tree state matches the
21+
* expected outcome after each operation.
22+
*
23+
* Key features.
24+
* - Coverage for `insertAfter()` and `insertBefore()` operations with and without validation.
25+
* - Edge case handling for inserting new nodes, moving existing nodes, and cross-tree insertions.
26+
* - Support for both single-tree and multi-tree models.
27+
* - Tests for strict validation and bypassing validation logic.
28+
* - XML dataset comparison to verify tree structure after insertions.
29+
*
30+
* @see MultipleTree for multi-tree model.
31+
* @see Tree for single-tree model.
32+
* @see TreeWithStrictValidation for strict validation scenarios.
33+
*
34+
* @copyright Copyright (C) 2023 Terabytesoftw.
35+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
36+
*/
1037
abstract class AbstractNodeInsert extends TestCase
1138
{
1239
public function testInsertAfterWithRunValidationParameterUsingStrictValidation(): void

tests/base/AbstractNodePrepend.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@
77
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree, TreeWithStrictValidation};
88
use yii2\extensions\nestedsets\tests\TestCase;
99

10+
/**
11+
* Base class for node prepend tests in nested sets tree behaviors.
12+
*
13+
* Provides a comprehensive suite of unit and integration tests for prepending nodes in nested sets tree structures,
14+
* ensuring correct tree structure, attribute updates, and validation logic for both single-tree and multi-tree models.
15+
*
16+
* This class validates the correctness of node prepend operations, strict validation scenarios, and XML dataset
17+
* matching after structural changes.
18+
*
19+
* It covers edge cases such as validation bypass and attribute refresh requirements, ensuring robust behavior for all
20+
* supported node manipulations.
21+
*
22+
* Key features.
23+
* - Covers both {@see Tree} and {@see MultipleTree} model scenarios.
24+
* - Ensures correct left, right, depth, and tree attribute updates after prepend operations.
25+
* - Tests for prepending new and existing nodes, including cross-tree operations.
26+
* - Validation of strict and non-strict prepend operations.
27+
* - XML dataset matching after structural changes.
28+
*
29+
* @see MultipleTree for multi-tree model.
30+
* @see Tree for single-tree model.
31+
* @see TreeWithStrictValidation for strict validation scenarios.
32+
*
33+
* @copyright Copyright (C) 2023 Terabytesoftw.
34+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
35+
*/
1036
abstract class AbstractNodePrepend extends TestCase
1137
{
1238
public function testPrependToWithRunValidationParameterUsingStrictValidation(): void

tests/base/AbstractNodeState.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,30 @@
44

55
namespace yii2\extensions\nestedsets\tests\base;
66

7-
use yii2\extensions\nestedsets\tests\support\model\MultipleTree;
8-
use yii2\extensions\nestedsets\tests\support\model\Tree;
7+
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree};
98
use yii2\extensions\nestedsets\tests\TestCase;
109

10+
/**
11+
* Base class for node state and relationship tests in nested sets tree behaviors.
12+
*
13+
* Provides a suite of unit tests for verifying node state, parent-child relationships, and root/leaf detection in both
14+
* single-tree and multi-tree nested sets models.
15+
*
16+
* This class ensures the correctness of methods that determine node ancestry, root status, and leaf status by testing
17+
* various edge cases and boundary conditions, such as equal left/right values and ancestor chains.
18+
*
19+
* Key features.
20+
* - Coverage for both {@see Tree} and {@see MultipleTree} model implementations.
21+
* - Ensures correct behavior for left/right value manipulations and ancestor checks.
22+
* - Tests for `isChildOf()` under different ancestor and boundary scenarios.
23+
* - Validation of `isRoot()` and `isLeaf()` logic for root, leaf, and intermediate nodes.
24+
*
25+
* @see MultipleTree for multi-tree model.
26+
* @see Tree for single-tree model.
27+
*
28+
* @copyright Copyright (C) 2023 Terabytesoftw.
29+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
30+
*/
1131
abstract class AbstractNodeState extends TestCase
1232
{
1333
public function testIsChildOfReturnsFalseWhenLeftValuesAreEqual(): void

0 commit comments

Comments
 (0)