Skip to content

Commit

Permalink
fix issue where unserialized objects do not work
Browse files Browse the repository at this point in the history
  • Loading branch information
havvg committed Sep 20, 2012
1 parent 34f1919 commit 5d59b59
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
12 changes: 10 additions & 2 deletions Propel/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function serialize()
$this->locked,
$this->credentials_expired,
$this->enabled,
$this->_new,
)
);
}
Expand All @@ -57,6 +58,12 @@ public function serialize()
*/
public function unserialize($serialized)
{
$data = unserialize($serialized);

// add a few extra elements in the array to ensure that we have enough keys when unserializing
// older data which does not include all properties.
$data = array_merge($data, array_fill(0, 1, null));

list(
$this->id,
$this->username,
Expand All @@ -65,8 +72,9 @@ public function unserialize($serialized)
$this->expired,
$this->locked,
$this->credentials_expired,
$this->enabled
) = unserialize($serialized);
$this->enabled,
$this->_new
) = $data;
}

/**
Expand Down
50 changes: 50 additions & 0 deletions Tests/Propel/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\UserBundle\Propel;

class UserTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!class_exists('Propel')) {
$this->markTestSkipped('Propel not installed');
}
}

public function testSerialize()
{
$group = new Group();
$group->setName('Developers');

$user = new User();
$user->setEmail('foobar@example.com');
$user->setPassword('123456');
$user->addGroup($group);
$user->save();

$userId = $user->getId();
$this->assertInternalType('int', $userId);

$serialized = serialize($user);
UserPeer::clearInstancePool();
$this->assertCount(0, UserPeer::$instances);

$unserialized = unserialize($serialized);
$fetchedUser = UserQuery::create()->findOneById($userId);

$this->assertInstanceOf('FOS\UserBundle\Propel\User', $unserialized);
$this->assertCount(1, UserPeer::$instances);
$this->assertTrue($fetchedUser->equals($unserialized));

$this->assertCount(1, $unserialized->getGroups());
}
}
3 changes: 2 additions & 1 deletion Tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
$builder = new \PropelQuickBuilder();
$builder->getConfig()->setBuildProperty('behavior.typehintable.class', $class->getFileName());
$builder->setSchema(file_get_contents(__DIR__.'/../Resources/config/propel/schema.xml'));
$builder->buildClasses();
$builder->setClassTargets(array('tablemap', 'peer', 'object', 'query'));
$builder->build();
}

0 comments on commit 5d59b59

Please sign in to comment.