Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata stack tests #57

Merged
merged 2 commits into from
Mar 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/JMS/Serializer/GraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ public function accept($data, array $type = null, Context $context)
$metadata = $this->resolveMetadata($context, $data, $metadata);
}

$context->pushClassMetadata($metadata);

if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipClass($metadata, $context)) {
$this->leaveScope($context, $data);

return null;
}

$context->pushClassMetadata($metadata);

if ($context instanceof SerializationContext) {
foreach ($metadata->preSerializeMethods as $method) {
$method->invoke($data);
Expand Down
150 changes: 150 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/ContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

/*
* Copyright 2013 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\Serializer\Tests\Serializer;

use JMS\Serializer\Context;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Tests\Fixtures\InlineChild;
use JMS\Serializer\Tests\Fixtures\Node;
use JMS\Serializer\SerializerBuilder;

class ContextTest extends \PHPUnit_Framework_TestCase
{
public function testSerializationContextPathAndDepth()
{
$object = new Node(array(
new Node(),
new Node(array(
new Node()
)),
));
$objects = array($object, $object->children[0], $object->children[1], $object->children[1]->children[0]);

$self = $this;

$exclusionStrategy = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface');
$exclusionStrategy->expects($this->any())
->method('shouldSkipClass')
->with($this->anything(), $this->callback(function (SerializationContext $context) use ($self, $objects) {
$expectedDepth = $expectedPath = null;

if ($context->getObject() === $objects[0]) {
$expectedDepth = 1;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node';
} elseif ($context->getObject() === $objects[1]) {
$expectedDepth = 2;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node';
} elseif ($context->getObject() === $objects[2]) {
$expectedDepth = 2;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node';
} elseif ($context->getObject() === $objects[3]) {
$expectedDepth = 3;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node';
}

$self->assertEquals($expectedDepth, $context->getDepth(), 'shouldSkipClass depth');
$self->assertEquals($expectedPath, $context->getPath(), 'shouldSkipClass path');

return true;
}))
->will($this->returnValue(false));

$exclusionStrategy->expects($this->any())
->method('shouldSkipProperty')
->with($this->anything(), $this->callback(function (SerializationContext $context) use ($self, $objects) {
$expectedDepth = $expectedPath = null;

if ($context->getObject() === $objects[0]) {
$expectedDepth = 1;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node';
} elseif ($context->getObject() === $objects[1]) {
$expectedDepth = 2;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node';
} elseif ($context->getObject() === $objects[2]) {
$expectedDepth = 2;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node';
} elseif ($context->getObject() === $objects[3]) {
$expectedDepth = 3;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node';
}

$self->assertEquals($expectedDepth, $context->getDepth(), 'shouldSkipProperty depth');
$self->assertEquals($expectedPath, $context->getPath(), 'shouldSkipProperty path');

return true;
}))
->will($this->returnValue(false));

$serializer = SerializerBuilder::create()->build();

$serializer->serialize($object, 'json', SerializationContext::create()->addExclusionStrategy($exclusionStrategy));
}

public function testSerializationMetadataStack()
{
$object = new Node(array(
$child = new InlineChild(),
));
$self = $this;

$exclusionStrategy = $this->getMock('JMS\Serializer\Exclusion\ExclusionStrategyInterface');
$exclusionStrategy->expects($this->any())
->method('shouldSkipClass')
->will($this->returnCallback(function (ClassMetadata $classMetadata, SerializationContext $context) use ($self, $object, $child) {
$stack = $context->getMetadataStack();

if ($object === $context->getObject()) {
$self->assertEquals(0, $stack->count());
}

if ($child === $context->getObject()) {
$self->assertEquals(2, $stack->count());
$self->assertEquals('JMS\Serializer\Tests\Fixtures\Node', $stack[1]->name);
$self->assertEquals('children', $stack[0]->name);
}

return false;
}));

$exclusionStrategy->expects($this->any())
->method('shouldSkipProperty')
->will($this->returnCallback(function (PropertyMetadata $propertyMetadata, SerializationContext $context) use ($self, $object, $child) {
$stack = $context->getMetadataStack();

if ('JMS\Serializer\Tests\Fixtures\Node' === $propertyMetadata->class && $propertyMetadata->name === 'children') {
$self->assertEquals(1, $stack->count());
$self->assertEquals('JMS\Serializer\Tests\Fixtures\Node', $stack[0]->name);
}

if ('JMS\Serializer\Tests\Fixtures\InlineChild' === $propertyMetadata->class) {
$self->assertEquals(3, $stack->count());
$self->assertEquals('JMS\Serializer\Tests\Fixtures\Node', $stack[2]->name);
$self->assertEquals('children', $stack[1]->name);
$self->assertEquals('JMS\Serializer\Tests\Fixtures\InlineChild', $stack[0]->name);
}

return false;
}));

$serializer = SerializerBuilder::create()->build();
$serializer->serialize($object, 'json', SerializationContext::create()->addExclusionStrategy($exclusionStrategy));
}
}
97 changes: 0 additions & 97 deletions tests/JMS/Serializer/Tests/Serializer/NavigatorContextTest.php

This file was deleted.