Skip to content

Commit

Permalink
Merge pull request #7 from itinance/ProfileExtension_fixed_Serialization
Browse files Browse the repository at this point in the history
Profile extension fixed serialization
  • Loading branch information
EmanueleMinotto committed Oct 4, 2015
2 parents 9a21f6b + 9c38d05 commit 0e0f9be
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
54 changes: 54 additions & 0 deletions Tests/ProfilerExtension/ProfilerExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace EmanueleMinotto\TwigCacheBundle\Tests\DependencyInjection;

use EmanueleMinotto\TwigCacheBundle\Tests\AppKernel;
use EmanueleMinotto\TwigCacheBundle\Twig\ProfilerExtension;
use PHPUnit_Framework_TestCase;

/**
* @coversDefaultClass \EmanueleMinotto\TwigCacheBundle\Twig\ProfilerExtension
*/
class ProfilerExtensionTest extends PHPUnit_Framework_TestCase
{
/**
* @var \Symfony\Component\HttpKernel\Kernel
*/
private $kernel;

/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->kernel = new AppKernel('TwigCacheExtensionTest', true);
$this->kernel->boot();
}

/**
* @covers ::serialize
* @covers ::unserialize
*/
public function testSerialization()
{
$container = $this->kernel->getContainer();

$cacheStrategy = $container->get('twig_cache.strategy.generational');
$extension = new ProfilerExtension($cacheStrategy);

$extension->addFetchBlock('foo', true);
$extension->addGenerateKey('generation_key', 123);

$data = $extension->getData();

$serializedData = serialize($extension);

/** @var ProfilerExtension $unserialized */
$unserialized = unserialize($serializedData);

$this->assertInstanceOf('EmanueleMinotto\TwigCacheBundle\Twig\ProfilerExtension', $unserialized);
$dataUnserialized = $unserialized->getData();

$this->assertEquals($data, $dataUnserialized);
}
}
40 changes: 39 additions & 1 deletion Twig/ProfilerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use Asm89\Twig\CacheExtension\Extension as Asm89_Extension;
use EmanueleMinotto\TwigCacheBundle\Strategy\ProfilerStrategy;
use Exception;
use Serializable;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;

/**
* {@inheritdoc}
*/
class ProfilerExtension extends Asm89_Extension implements DataCollectorInterface
class ProfilerExtension extends Asm89_Extension implements DataCollectorInterface, Serializable
{
/**
* Data about fetchBlock requests.
Expand Down Expand Up @@ -108,4 +109,41 @@ public function getData()
'strategyClass' => $this->strategyClass,
];
}

/**
* String representation of object.
*
* @link http://php.net/manual/en/serializable.serialize.php
*
* @return string the string representation of the object or null
*
* @since 5.1.0
*/
public function serialize()
{
return serialize($this->getData());
}

/**
* Constructs the object.
*
* @link http://php.net/manual/en/serializable.unserialize.php
*
* @param string $serialized
*
* @return void
*
* @since 5.1.0
*/
public function unserialize($serialized)
{
$data = unserialize($serialized);

if (is_array($data)) {
$this->fetchBlock = $data['fetchBlock'];
$this->generateKey = $data['generateKey'];
$this->hits = $data['hits'];
$this->strategyClass = $data['strategyClass'];
}
}
}

0 comments on commit 0e0f9be

Please sign in to comment.