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

Profile extension fixed serialization #7

Merged
merged 8 commits into from
Oct 4, 2015
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
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'];
}
}
}