-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Added basic test for
getChildren
and Symfony Serialization
- Loading branch information
1 parent
6547e2a
commit a4a74e9
Showing
12 changed files
with
410 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']]) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
require dirname(__DIR__).'/vendor/autoload.php'; |
Oops, something went wrong.