Skip to content

Commit a4a74e9

Browse files
test: Added basic test for getChildren and Symfony Serialization
1 parent 6547e2a commit a4a74e9

12 files changed

+410
-4
lines changed

.github/workflows/run-test.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Static analysis and code style
1+
name: Unit tests, static analysis and code style
22

33
on:
44
push:
@@ -35,5 +35,8 @@ jobs:
3535
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
3636
- name: Run PHP Code Sniffer
3737
run: vendor/bin/phpcs -p ./src
38+
- name: Run unit tests
39+
run: |
40+
XDEBUG_MODE=coverage vendor/bin/phpunit -v
3841
- name: Run PHPStan
3942
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,6 @@ modules.xml
103103
# Sonarlint plugin
104104
.idea/sonarlint
105105

106-
# End of https://www.gitignore.io/api/composer,phpstorm+all
106+
# End of https://www.gitignore.io/api/composer,phpstorm+all
107+
108+
coverage

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

22
test:
33
php vendor/bin/phpcbf -p
4+
XDEBUG_MODE=coverage vendor/bin/phpunit -v
45
php vendor/bin/phpstan analyse -c phpstan.neon

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tree Walker
22

3-
[![Build Status](https://travis-ci.org/rezozero/tree-walker.svg?branch=master)](https://travis-ci.org/rezozero/tree-walker) ![License](http://img.shields.io/:license-mit-blue.svg?style=flat) [![Packagist](https://img.shields.io/packagist/v/rezozero/tree-walker.svg?style=flat)](https://packagist.org/packages/rezozero/tree-walker)
3+
[![Tests status](https://github.com/rezozero/tree-walker/actions/workflows/run-test.yml/badge.svg)](https://github.com/rezozero/tree-walker/actions/workflows/run-test.yml) ![License](http://img.shields.io/:license-mit-blue.svg?style=flat) [![Packagist](https://img.shields.io/packagist/v/rezozero/tree-walker.svg?style=flat)](https://packagist.org/packages/rezozero/tree-walker)
44

55
**Creates a configurable tree walker using different definitions for each node based on its PHP class or interface.**
66

composer.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@
1919
},
2020
"require-dev": {
2121
"squizlabs/php_codesniffer": "^3.3",
22-
"phpstan/phpstan": "^1.8.2"
22+
"phpstan/phpstan": "^1.8.2",
23+
"phpunit/phpunit": "^9.5",
24+
"symfony/property-access": "^7.1"
2325
},
2426
"autoload": {
2527
"psr-4": {
2628
"RZ\\TreeWalker\\": "src/"
2729
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"RZ\\TreeWalker\\": "src/",
34+
"RZ\\TreeWalker\\Tests\\": "tests/"
35+
}
2836
}
2937
}

tests/DummyTest.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RZ\TreeWalker\Tests;
6+
7+
use Doctrine\Common\Collections\Collection;
8+
use RZ\TreeWalker\EmptyWalkerContext;
9+
use RZ\TreeWalker\Tests\Mock\Dummy;
10+
use RZ\TreeWalker\Tests\Walker\DummyWalker;
11+
12+
class DummyTest extends SerializerTestCase
13+
{
14+
public function testBuild(): void
15+
{
16+
$firstItem = new Dummy('ancestor');
17+
$walker = DummyWalker::build(
18+
$firstItem,
19+
new EmptyWalkerContext(),
20+
3 // max level count
21+
);
22+
$children = $walker->getChildren();
23+
$this->assertInstanceOf(Collection::class, $children);
24+
$this->assertCount(3, $children);
25+
26+
$this->assertInstanceOf(DummyWalker::class, $children[0]);
27+
28+
$this->assertEquals('ancestor - child 1', $children[0]->getItem()->name);
29+
$this->assertEquals('ancestor - child 2', $children[1]->getItem()->name);
30+
$this->assertEquals('ancestor - child 3', $children[2]->getItem()->name);
31+
}
32+
33+
public function testSerialize(): void
34+
{
35+
$firstItem = new Dummy('ancestor');
36+
$walker = DummyWalker::build(
37+
$firstItem,
38+
new EmptyWalkerContext(),
39+
3 // max level count
40+
);
41+
42+
$this->assertJsonStringEqualsJsonFile(
43+
__DIR__ . '/fixtures/dummy-walker.json',
44+
$this->getSerializer()->serialize($walker, 'json', ['groups' => ['children', 'walker', 'dummy']])
45+
);
46+
}
47+
}

tests/Mock/Dummy.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RZ\TreeWalker\Tests\Mock;
6+
7+
use Symfony\Component\Serializer\Attribute\Groups;
8+
9+
class Dummy
10+
{
11+
public function __construct(
12+
#[Groups(['dummy'])]
13+
public string $name
14+
) {
15+
}
16+
17+
public function __toString(): string
18+
{
19+
return $this->name;
20+
}
21+
}

tests/SerializerTestCase.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RZ\TreeWalker\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use RZ\TreeWalker\WalkerInterface;
9+
use Symfony\Component\Serializer\Context\Normalizer\ObjectNormalizerContextBuilder;
10+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
11+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
12+
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
13+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
14+
use Symfony\Component\Serializer\Serializer;
15+
16+
class SerializerTestCase extends TestCase
17+
{
18+
protected function getSerializer(): Serializer
19+
{
20+
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
21+
22+
$contextBuilder = (new ObjectNormalizerContextBuilder())
23+
->withCircularReferenceHandler(function (WalkerInterface $object, string $format, array $context): string {
24+
return (string) $object->getItem();
25+
});
26+
return new Serializer([new ObjectNormalizer(classMetadataFactory: $classMetadataFactory, defaultContext: $contextBuilder->toArray())], [new JsonEncoder()]);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RZ\TreeWalker\Tests\Walker\Definition;
6+
7+
use RZ\TreeWalker\Definition\ContextualDefinitionTrait;
8+
use RZ\TreeWalker\Tests\Mock\Dummy;
9+
use RZ\TreeWalker\WalkerInterface;
10+
11+
class DummyChildrenDefinition
12+
{
13+
use ContextualDefinitionTrait;
14+
15+
public function __invoke(Dummy $dummy, WalkerInterface $walker): array
16+
{
17+
return [
18+
new Dummy($dummy->name . ' - child 1'),
19+
new Dummy($dummy->name . ' - child 2'),
20+
new Dummy($dummy->name . ' - child 3'),
21+
];
22+
}
23+
}

tests/Walker/DummyWalker.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RZ\TreeWalker\Tests\Walker;
6+
7+
use RZ\TreeWalker\AbstractCycleAwareWalker;
8+
use RZ\TreeWalker\Tests\Mock\Dummy;
9+
use RZ\TreeWalker\Tests\Walker\Definition\DummyChildrenDefinition;
10+
11+
class DummyWalker extends AbstractCycleAwareWalker
12+
{
13+
protected function initializeDefinitions(): void
14+
{
15+
$this->addDefinition(Dummy::class, new DummyChildrenDefinition($this->getContext()));
16+
}
17+
}

tests/bootstrap.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require dirname(__DIR__).'/vendor/autoload.php';

0 commit comments

Comments
 (0)