Skip to content

Commit d6d52ac

Browse files
committed
add test case
1 parent 4425fd5 commit d6d52ac

File tree

4 files changed

+144
-3
lines changed

4 files changed

+144
-3
lines changed

composer.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,27 @@
1414
"issues": "https://github.com/AxiosCros/php-tools/issues"
1515
},
1616
"require": {
17-
"php": ">=7.2",
18-
"symfony/var-dumper": "*"
17+
"php": ">=7.2"
18+
},
19+
"require-dev": {
20+
"symfony/var-dumper": "*",
21+
"phpunit/phpunit": "^8.5"
1922
},
2023
"autoload": {
2124
"psr-4": {
2225
"axios\\tools\\": "src"
2326
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"axios\\tools\\tests\\": "tests"
31+
}
32+
},
33+
"scripts": {
34+
"test": [
35+
"@clearCache",
36+
"./vendor/bin/phpunit --colors=always"
37+
],
38+
"clearCache": "rm -rf cache/*"
2439
}
25-
}
40+
}

phpunit.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php" colors="true" processIsolation="false" stopOnFailure="false"
3+
convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true"
4+
testSuiteLoaderFile="phpunit/src/Runner/StandardTestSuiteLoader.php">
5+
6+
<testsuites>
7+
<testsuite name="All">
8+
<directory>tests</directory>
9+
</testsuite>
10+
<testsuite name="Unit">
11+
<directory suffix="Test.php">./tests/unit</directory>
12+
</testsuite>
13+
</testsuites>
14+
15+
<groups>
16+
<exclude>
17+
<group>integration</group>
18+
</exclude>
19+
</groups>
20+
21+
<logging>
22+
<log type="coverage-html" target="cache/coverage" lowUpperBound="35" highLowerBound="70"/>
23+
<log type="coverage-clover" target="cache/coverage.clover"/>
24+
</logging>
25+
26+
27+
<filter>
28+
<whitelist processUncoveredFilesFromWhitelist="true">
29+
<directory suffix=".php">./src</directory>
30+
</whitelist>
31+
</filter>
32+
</phpunit>

tests/bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

tests/unit/ListTreeTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace axios\tools\tests\unit;
6+
7+
use axios\tools\ListToTree;
8+
use axios\tools\TreeToList;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* @internal
13+
* @coversNothing
14+
*/
15+
class ListTreeTest extends TestCase
16+
{
17+
public function testListToTree()
18+
{
19+
$data = [
20+
['id' => 1, 'parent_id' => 0],
21+
['id' => 2, 'parent_id' => 3],
22+
['id' => 3, 'parent_id' => 1],
23+
['id' => 4, 'parent_id' => 2],
24+
['id' => 5, 'parent_id' => 6],
25+
['id' => 6, 'parent_id' => 7],
26+
['id' => 7, 'parent_id' => 5],
27+
];
28+
$ListToTree = new ListToTree($data);
29+
$this->assertEquals([
30+
[
31+
'id' => 1,
32+
'parent_id' => 0,
33+
'child' => [
34+
0 => [
35+
'id' => 3,
36+
'parent_id' => 1,
37+
'child' => [
38+
0 => [
39+
'id' => 2,
40+
'parent_id' => 3,
41+
'child' => [
42+
0 => [
43+
'id' => 4,
44+
'parent_id' => 2,
45+
],
46+
],
47+
],
48+
],
49+
],
50+
],
51+
],
52+
], $ListToTree->toTree());
53+
}
54+
55+
public function testTreeToList()
56+
{
57+
$data = [
58+
[
59+
'id' => 1,
60+
'parent_id' => 0,
61+
'child' => [
62+
0 => [
63+
'id' => 3,
64+
'parent_id' => 1,
65+
'child' => [
66+
0 => [
67+
'id' => 2,
68+
'parent_id' => 3,
69+
'child' => [
70+
0 => [
71+
'id' => 4,
72+
'parent_id' => 2,
73+
],
74+
],
75+
],
76+
],
77+
],
78+
],
79+
],
80+
];
81+
$TreeToList = new TreeToList($data);
82+
$this->assertEquals([
83+
['id' => 1, 'parent_id' => 0],
84+
['id' => 2, 'parent_id' => 1],
85+
['id' => 3, 'parent_id' => 2],
86+
['id' => 4, 'parent_id' => 3],
87+
], $TreeToList->toList());
88+
}
89+
}

0 commit comments

Comments
 (0)