Skip to content

Commit 0cf2d55

Browse files
committed
feat: Update composer.json to require ext-dom and enhance TestCase.php with improved documentation and structure.
1 parent c1ca443 commit 0cf2d55

File tree

2 files changed

+111
-16
lines changed

2 files changed

+111
-16
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"yiisoft/yii2": "^2.0.53|^22"
1515
},
1616
"require-dev": {
17+
"ext-dom": "*",
1718
"ext-simplexml": "*",
1819
"infection/infection": "^0.27|^0.30",
1920
"maglnet/composer-require-checker": "^4.1",

tests/TestCase.php

Lines changed: 110 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,47 @@
77
use RuntimeException;
88
use SimpleXMLElement;
99
use Yii;
10-
use yii\base\InvalidArgumentException;
10+
use yii\base\{InvalidArgumentException, InvalidConfigException};
1111
use yii\console\Application;
12-
use yii\db\{ActiveQuery, ActiveRecord, Connection, Query, SchemaBuilderTrait};
12+
use yii\db\{ActiveQuery, ActiveRecord, Connection, Exception, SchemaBuilderTrait};
1313
use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree};
1414
use yii2\extensions\nestedsets\tests\support\stub\EchoMigrateController;
1515

1616
use function array_merge;
1717
use function array_values;
18+
use function dirname;
1819
use function dom_import_simplexml;
1920
use function file_get_contents;
21+
use function is_int;
22+
use function is_string;
23+
use function ob_get_clean;
24+
use function ob_implicit_flush;
25+
use function ob_start;
2026
use function preg_replace;
2127
use function simplexml_load_string;
2228
use function str_replace;
2329

2430
/**
31+
* Base test case for nested sets behavior test suites.
32+
*
33+
* Provides common setup, database management, fixture loading, and assertion utilities for all nested sets tests.
34+
*
35+
* This class centralizes logic for initializing the test environment, managing database state, and verifying tree
36+
* structures, ensuring consistency and reducing duplication across test cases for different database drivers and
37+
* scenarios.
38+
*
39+
* Key features.
40+
* - Assertion helpers for validating node order and query structure.
41+
* - Integration with custom migration and schema management.
42+
* - Shared database connection and fixture directory configuration.
43+
* - Support for both single-tree and multi-tree models.
44+
* - Utilities for creating, resetting, and populating test databases.
45+
* - XML fixture generation and loading for reproducible test data.
46+
*
47+
* @see EchoMigrateController for migration handling.
48+
* @see MultipleTree for multi-tree model.
49+
* @see Tree for single-tree model.
50+
*
2551
* @phpstan-type DataSetType = list<
2652
* array{
2753
* id: int,
@@ -36,35 +62,50 @@
3662
* @phpstan-type NodeChildren array<string|array{name: string, children?: array<mixed>}>
3763
* @phpstan-type TreeStructure array<array<mixed>>
3864
* @phpstan-type UpdateData array<array{name: string, lft?: int, rgt?: int, depth?: int}>
65+
*
66+
* @copyright Copyright (C) 2023 Terabytesoftw.
67+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
3968
*/
40-
class TestCase extends \PHPUnit\Framework\TestCase
69+
abstract class TestCase extends \PHPUnit\Framework\TestCase
4170
{
4271
use SchemaBuilderTrait;
4372

4473
/**
74+
* Database connection configuration.
75+
*
4576
* @phpstan-var string[]
4677
*/
4778
protected array $connection = [];
79+
80+
/**
81+
* Directory where fixture XML files are stored.
82+
*/
4883
protected string $fixtureDirectory = __DIR__ . '/support/data/';
4984

85+
/**
86+
* @throws InvalidConfigException if the configuration is invalid or incomplete.
87+
*/
5088
protected function setUp(): void
5189
{
5290
parent::setUp();
5391

5492
$this->mockConsoleApplication();
5593
}
5694

57-
public function getDb(): Connection
95+
/**
96+
* Returns the database connection used for tests.
97+
*/
98+
protected function getDb(): Connection
5899
{
59100
return Yii::$app->getDb();
60101
}
61102

62103
/**
63104
* Asserts that a list of tree nodes matches the expected order.
64105
*
65-
* @param array $nodesList List of tree nodes to validate
66-
* @param array $expectedOrder Expected order of node names
67-
* @param string $nodeType Type of nodes being tested (for error messages)
106+
* @param array $nodesList List of tree nodes to validate.
107+
* @param array $expectedOrder Expected order of node names.
108+
* @param string $nodeType Type of nodes being tested (for error messages).
68109
*
69110
* @phpstan-param array<ActiveRecord> $nodesList
70111
* @phpstan-param array<string> $expectedOrder
@@ -97,8 +138,8 @@ protected function assertNodesInCorrectOrder(array $nodesList, array $expectedOr
97138
/**
98139
* Asserts that a query contains ORDER BY clause with 'lft' column.
99140
*
100-
* @param ActiveQuery $query The query to check
101-
* @param string $methodName Name of the method being tested
141+
* @param ActiveQuery $query Query to check.
142+
* @param string $methodName Name of the method being tested.
102143
*
103144
* @phpstan-param ActiveQuery<ActiveRecord> $query
104145
*/
@@ -120,6 +161,10 @@ protected function assertQueryHasOrderBy(ActiveQuery $query, string $methodName)
120161
}
121162

122163
/**
164+
* Builds a flat XML dataset from a given data set array.
165+
*
166+
* @return string Formatted XML string.
167+
*
123168
* @phpstan-import-type DataSetType from TestCase
124169
*
125170
* @phpstan-param DataSetType $dataSet
@@ -167,6 +212,14 @@ protected function buildFlatXMLDataSet(array $dataSet): string
167212
);
168213
}
169214

215+
/**
216+
* Creates the database schema and resets the tables for testing.
217+
*
218+
* This method drops existing tables and runs migrations to ensure a clean state.
219+
*
220+
* @throws Exception if an unexpected error occurs during execution.
221+
* @throws RuntimeException if a runtime error prevents the operation from completing successfully.
222+
*/
170223
protected function createDatabase(): void
171224
{
172225
$command = $this->getDb()->createCommand();
@@ -193,13 +246,14 @@ protected function createDatabase(): void
193246
/**
194247
* Creates a tree structure based on a hierarchical definition.
195248
*
196-
* @param array $structure Hierarchical tree structure definition
197-
* @param array $updates Database updates to apply after creation
198-
* @param string $modelClass The model class to use (Tree::class or MultipleTree::class)
249+
* @param array $structure Hierarchical tree structure definition.
250+
* @param array $updates Database updates to apply after creation.
251+
* @param string $modelClass Model class to use ({@see Tree::class} or {@see MultipleTree::class}.
199252
*
200-
* @throws InvalidArgumentException if the structure array is empty.
253+
* @throws Exception if an unexpected error occurs during execution.
254+
* @throws InvalidArgumentException if one or more arguments are invalid, of incorrect type or format.
201255
*
202-
* @return MultipleTree|Tree The root node
256+
* @return MultipleTree|Tree Root node.
203257
*
204258
* @phpstan-param TreeStructure $structure
205259
* @phpstan-param UpdateData $updates
@@ -240,6 +294,16 @@ protected function createTreeStructure(
240294
return $rootNode;
241295
}
242296

297+
/**
298+
* Generates fixture data for testing tree structures.
299+
*
300+
* This method creates a database schema and populates it with predefined XML fixture data.
301+
*
302+
* It is used to set up the initial state of the database for tests that require specific tree structures.
303+
*
304+
* @throws Exception if an unexpected error occurs during execution.
305+
* @throws RuntimeException if a runtime error prevents the operation from completing successfully.
306+
*/
243307
protected function generateFixtureTree(): void
244308
{
245309
$this->createDatabase();
@@ -277,6 +341,10 @@ protected function generateFixtureTree(): void
277341
}
278342

279343
/**
344+
* Returns a dataset containing all tree nodes from both {@see Tree} and {@see MultipleTree} models.
345+
*
346+
* @return array Dataset containing all tree nodes.
347+
*
280348
* @phpstan-import-type DataSetType from TestCase
281349
*
282350
* @phpstan-return DataSetType
@@ -300,6 +368,10 @@ protected function getDataSet(): array
300368
}
301369

302370
/**
371+
* Returns a dataset containing only {@see MultipleTree} nodes.
372+
*
373+
* @return array Dataset containing only {@see MultipleTree} nodes.
374+
*
303375
* @phpstan-import-type DataSetType from TestCase
304376
*
305377
* @phpstan-return DataSetType
@@ -315,6 +387,13 @@ protected function getDataSetMultipleTree(): array
315387
return array_values($dataSetMultipleTree);
316388
}
317389

390+
/**
391+
* Returns a dataset containing only {@see Tree} nodes.
392+
*
393+
* @throws RuntimeException if a runtime error prevents the operation from completing successfully.
394+
*
395+
* @return SimpleXMLElement Dataset containing only {@see Tree} nodes.
396+
*/
318397
protected function loadFixtureXML(string $fileName): SimpleXMLElement
319398
{
320399
$filePath = "{$this->fixtureDirectory}/{$fileName}";
@@ -334,6 +413,15 @@ protected function loadFixtureXML(string $fileName): SimpleXMLElement
334413
return $simpleXML;
335414
}
336415

416+
/**
417+
* Mocks the console application for testing purposes.
418+
*
419+
* This method initializes a new console application instance with a database connection.
420+
*
421+
* It is used to set up the environment for running console commands in tests.
422+
*
423+
* @throws InvalidConfigException if the configuration is invalid or incomplete.
424+
*/
337425
protected function mockConsoleApplication(): void
338426
{
339427
new Application(
@@ -382,6 +470,10 @@ protected function replaceQuotes(string $sql): string
382470
}
383471

384472
/**
473+
* Runs a migration action with the specified parameters.
474+
*
475+
* @return mixed Result of the migration action.
476+
*
385477
* @phpstan-param array<array-key, mixed> $params
386478
*/
387479
protected function runMigrate(string $action, array $params = []): mixed
@@ -415,6 +507,8 @@ protected function runMigrate(string $action, array $params = []): mixed
415507
* @param array $updates Array of updates to apply.
416508
* @param string $tableName Name of the table to apply updates to.
417509
*
510+
* @throws Exception if an unexpected error occurs during execution.
511+
*
418512
* @phpstan-param UpdateData $updates
419513
*/
420514
private function applyUpdates(array $updates, string $tableName): void
@@ -437,8 +531,8 @@ private function applyUpdates(array $updates, string $tableName): void
437531
/**
438532
* Recursively creates children for a given parent node.
439533
*
440-
* @param MultipleTree|Tree $parent The parent node
441-
* @param array $nodes Children definition (can be strings or arrays)
534+
* @param MultipleTree|Tree $parent Parent node.
535+
* @param array $nodes Children definition (can be strings or arrays).
442536
*
443537
* @phpstan-param NodeChildren $nodes
444538
*/

0 commit comments

Comments
 (0)