2015-11-03 20:41:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Component\Serializer\Tests\Normalizer;
|
|
|
|
|
|
|
|
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
|
|
|
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
|
|
|
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a dummy Normalizer which extends the AbstractNormalizer.
|
|
|
|
*
|
|
|
|
* @author Konstantin S. M. Möllers <ksm.moellers@gmail.com>
|
|
|
|
*/
|
|
|
|
class AbstractNormalizerTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var AbstractNormalizerDummy
|
|
|
|
*/
|
|
|
|
private $normalizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ClassMetadataFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $classMetadata;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2015-12-09 11:45:47 +00:00
|
|
|
$loader = $this->getMock('Symfony\Component\Serializer\Mapping\Loader\LoaderChain', array(), array(array()));
|
|
|
|
$this->classMetadata = $this->getMock('Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory', array(), array($loader));
|
2015-11-03 20:41:27 +00:00
|
|
|
$this->normalizer = new AbstractNormalizerDummy($this->classMetadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAllowedAttributesAsString()
|
|
|
|
{
|
|
|
|
$classMetadata = new ClassMetadata('c');
|
|
|
|
|
|
|
|
$a1 = new AttributeMetadata('a1');
|
|
|
|
$classMetadata->addAttributeMetadata($a1);
|
|
|
|
|
|
|
|
$a2 = new AttributeMetadata('a2');
|
|
|
|
$a2->addGroup('test');
|
|
|
|
$classMetadata->addAttributeMetadata($a2);
|
|
|
|
|
|
|
|
$a3 = new AttributeMetadata('a3');
|
|
|
|
$a3->addGroup('other');
|
|
|
|
$classMetadata->addAttributeMetadata($a3);
|
|
|
|
|
|
|
|
$a4 = new AttributeMetadata('a4');
|
|
|
|
$a4->addGroup('test');
|
|
|
|
$a4->addGroup('other');
|
|
|
|
$classMetadata->addAttributeMetadata($a4);
|
|
|
|
|
|
|
|
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
|
|
|
|
|
2015-12-09 11:45:47 +00:00
|
|
|
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('test')), true);
|
|
|
|
$this->assertEquals(array('a2', 'a4'), $result);
|
2015-11-03 20:41:27 +00:00
|
|
|
|
2015-12-09 11:45:47 +00:00
|
|
|
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('other')), true);
|
|
|
|
$this->assertEquals(array('a3', 'a4'), $result);
|
2015-11-03 20:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAllowedAttributesAsObjects()
|
|
|
|
{
|
|
|
|
$classMetadata = new ClassMetadata('c');
|
|
|
|
|
|
|
|
$a1 = new AttributeMetadata('a1');
|
|
|
|
$classMetadata->addAttributeMetadata($a1);
|
|
|
|
|
|
|
|
$a2 = new AttributeMetadata('a2');
|
|
|
|
$a2->addGroup('test');
|
|
|
|
$classMetadata->addAttributeMetadata($a2);
|
|
|
|
|
|
|
|
$a3 = new AttributeMetadata('a3');
|
|
|
|
$a3->addGroup('other');
|
|
|
|
$classMetadata->addAttributeMetadata($a3);
|
|
|
|
|
|
|
|
$a4 = new AttributeMetadata('a4');
|
|
|
|
$a4->addGroup('test');
|
|
|
|
$a4->addGroup('other');
|
|
|
|
$classMetadata->addAttributeMetadata($a4);
|
|
|
|
|
|
|
|
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
|
|
|
|
|
2015-12-09 11:45:47 +00:00
|
|
|
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('test')), false);
|
|
|
|
$this->assertEquals(array($a2, $a4), $result);
|
2015-11-03 20:41:27 +00:00
|
|
|
|
2015-12-09 11:45:47 +00:00
|
|
|
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('other')), false);
|
|
|
|
$this->assertEquals(array($a3, $a4), $result);
|
2015-11-03 20:41:27 +00:00
|
|
|
}
|
|
|
|
}
|