示例#1
0
 public function testSerializationMetadataStack()
 {
     $object = new Node([$child = new InlineChild()]);
     $exclusionStrategy = $this->createMock(ExclusionStrategyInterface::class);
     $exclusionStrategy->expects($this->any())->method('shouldSkipClass')->will($this->returnValue(false));
     $exclusionStrategy->expects($this->any())->method('shouldSkipProperty')->will($this->returnCallback(function (PropertyMetadata $propertyMetadata, SerializationContext $context) use($object, $child) {
         $stack = $context->getMetadataStack();
         if (Node::class === $propertyMetadata->class && $propertyMetadata->name === 'children') {
             $this->assertEquals(0, $stack->count());
         }
         if (InlineChild::class === $propertyMetadata->class) {
             $this->assertEquals(1, $stack->count());
             $this->assertEquals('children', $stack->getCurrent()->getName());
         }
         return false;
     }));
     $serializer = SerializerBuilder::create()->build();
     $serializer->serialize($object, 'json', SerializationContext::create()->addExclusionStrategy($exclusionStrategy));
 }
示例#2
0
 protected function setUp()
 {
     $connection = $this->createConnection();
     $entityManager = $this->createEntityManager($connection);
     $this->registry = $registry = new SimpleManagerRegistry(function ($id) use($connection, $entityManager) {
         switch ($id) {
             case 'default_connection':
                 return $connection;
             case 'default_manager':
                 return $entityManager;
             default:
                 throw new \RuntimeException(sprintf('Unknown service id "%s".', $id));
         }
     });
     $loader = new AnnotationLoader();
     $loader->setReader(new AnnotationReader());
     $this->serializer = SerializerBuilder::create()->setMetadataLoader(new DoctrineTypeLoader($loader, $registry))->setEventDispatcher(new EventDispatcher())->build();
     $this->prepareDatabase();
 }
 protected function setUp()
 {
     $this->builder = SerializerBuilder::create();
 }
 public function setUp()
 {
     $this->serializer = SerializerBuilder::create()->addDefaultHandlers()->build();
 }
示例#5
0
{
    $collection = [];
    for ($i = 0; $i < 50; ++$i) {
        $collection[] = createObject();
    }
    return $collection;
}
function createObject()
{
    $post = new \Kcs\Serializer\Tests\Fixtures\BlogPost('FooooooooooooooooooooooBAR', new \Kcs\Serializer\Tests\Fixtures\Author('Foo'), new \DateTime(), new \Kcs\Serializer\Tests\Fixtures\Publisher('Bar'));
    for ($i = 0; $i < 10; ++$i) {
        $post->addComment(new \Kcs\Serializer\Tests\Fixtures\Comment(new \Kcs\Serializer\Tests\Fixtures\Author('foo'), 'foobar'));
    }
    return $post;
}
$serializer = \Kcs\Serializer\SerializerBuilder::create()->setEventDispatcher(new \Symfony\Component\EventDispatcher\EventDispatcher())->build();
$collection = createCollection();
$metrics = [];
$f = function ($format) use($serializer, $collection) {
    $serializer->serialize($collection, $format);
};
// Load all necessary classes into memory.
$f('array');
$table = new \Symfony\Component\Console\Helper\Table($output);
$table->setHeaders(['Format', 'Direction', 'Time']);
$progressBar = new \Symfony\Component\Console\Helper\ProgressBar($output, 8);
$progressBar->start();
foreach (['array', 'json', 'yml', 'xml'] as $format) {
    $table->addRow([$format, 'serialize', benchmark($f, $format)]);
    $progressBar->advance();
}