Skip to content

Commit

Permalink
test: Added basic test for getChildren and Symfony Serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jul 1, 2024
1 parent 6547e2a commit a4a74e9
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Static analysis and code style
name: Unit tests, static analysis and code style

on:
push:
Expand Down Expand Up @@ -35,5 +35,8 @@ jobs:
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
- name: Run PHP Code Sniffer
run: vendor/bin/phpcs -p ./src
- name: Run unit tests
run: |
XDEBUG_MODE=coverage vendor/bin/phpunit -v
- name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ modules.xml
# Sonarlint plugin
.idea/sonarlint

# End of https://www.gitignore.io/api/composer,phpstorm+all
# End of https://www.gitignore.io/api/composer,phpstorm+all

coverage
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

test:
php vendor/bin/phpcbf -p
XDEBUG_MODE=coverage vendor/bin/phpunit -v
php vendor/bin/phpstan analyse -c phpstan.neon
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tree Walker

[![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)
[![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)

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

Expand Down
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.3",
"phpstan/phpstan": "^1.8.2"
"phpstan/phpstan": "^1.8.2",
"phpunit/phpunit": "^9.5",
"symfony/property-access": "^7.1"
},
"autoload": {
"psr-4": {
"RZ\\TreeWalker\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"RZ\\TreeWalker\\": "src/",
"RZ\\TreeWalker\\Tests\\": "tests/"
}
}
}
47 changes: 47 additions & 0 deletions tests/DummyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace RZ\TreeWalker\Tests;

use Doctrine\Common\Collections\Collection;
use RZ\TreeWalker\EmptyWalkerContext;
use RZ\TreeWalker\Tests\Mock\Dummy;
use RZ\TreeWalker\Tests\Walker\DummyWalker;

class DummyTest extends SerializerTestCase
{
public function testBuild(): void
{
$firstItem = new Dummy('ancestor');
$walker = DummyWalker::build(
$firstItem,
new EmptyWalkerContext(),
3 // max level count
);
$children = $walker->getChildren();
$this->assertInstanceOf(Collection::class, $children);
$this->assertCount(3, $children);

$this->assertInstanceOf(DummyWalker::class, $children[0]);

$this->assertEquals('ancestor - child 1', $children[0]->getItem()->name);
$this->assertEquals('ancestor - child 2', $children[1]->getItem()->name);
$this->assertEquals('ancestor - child 3', $children[2]->getItem()->name);
}

public function testSerialize(): void
{
$firstItem = new Dummy('ancestor');
$walker = DummyWalker::build(
$firstItem,
new EmptyWalkerContext(),
3 // max level count
);

$this->assertJsonStringEqualsJsonFile(
__DIR__ . '/fixtures/dummy-walker.json',
$this->getSerializer()->serialize($walker, 'json', ['groups' => ['children', 'walker', 'dummy']])
);
}
}
21 changes: 21 additions & 0 deletions tests/Mock/Dummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace RZ\TreeWalker\Tests\Mock;

use Symfony\Component\Serializer\Attribute\Groups;

class Dummy
{
public function __construct(
#[Groups(['dummy'])]
public string $name
) {
}

public function __toString(): string
{
return $this->name;
}
}
28 changes: 28 additions & 0 deletions tests/SerializerTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace RZ\TreeWalker\Tests;

use PHPUnit\Framework\TestCase;
use RZ\TreeWalker\WalkerInterface;
use Symfony\Component\Serializer\Context\Normalizer\ObjectNormalizerContextBuilder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class SerializerTestCase extends TestCase
{
protected function getSerializer(): Serializer
{
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());

$contextBuilder = (new ObjectNormalizerContextBuilder())
->withCircularReferenceHandler(function (WalkerInterface $object, string $format, array $context): string {
return (string) $object->getItem();
});
return new Serializer([new ObjectNormalizer(classMetadataFactory: $classMetadataFactory, defaultContext: $contextBuilder->toArray())], [new JsonEncoder()]);
}
}
23 changes: 23 additions & 0 deletions tests/Walker/Definition/DummyChildrenDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace RZ\TreeWalker\Tests\Walker\Definition;

use RZ\TreeWalker\Definition\ContextualDefinitionTrait;
use RZ\TreeWalker\Tests\Mock\Dummy;
use RZ\TreeWalker\WalkerInterface;

class DummyChildrenDefinition
{
use ContextualDefinitionTrait;

public function __invoke(Dummy $dummy, WalkerInterface $walker): array
{
return [
new Dummy($dummy->name . ' - child 1'),
new Dummy($dummy->name . ' - child 2'),
new Dummy($dummy->name . ' - child 3'),
];
}
}
17 changes: 17 additions & 0 deletions tests/Walker/DummyWalker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace RZ\TreeWalker\Tests\Walker;

use RZ\TreeWalker\AbstractCycleAwareWalker;
use RZ\TreeWalker\Tests\Mock\Dummy;
use RZ\TreeWalker\Tests\Walker\Definition\DummyChildrenDefinition;

class DummyWalker extends AbstractCycleAwareWalker
{
protected function initializeDefinitions(): void
{
$this->addDefinition(Dummy::class, new DummyChildrenDefinition($this->getContext()));
}
}
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require dirname(__DIR__).'/vendor/autoload.php';
Loading

0 comments on commit a4a74e9

Please sign in to comment.