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

Fixed multiple choice value storage #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions Entity/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function getId()
*/
public function setValue($value)
{
if (is_array($value)) {
$value = serialize($value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bunch of scalar values, so implode / explode might be enough for the job, what do you think ?

}

$this->value = $value;

return $this;
Expand All @@ -67,6 +71,10 @@ public function setValue($value)
*/
public function getValue()
{
if (false !== $unserialized = @unserialize($this->value)) {
return $unserialized;
}

return $this->value;
}

Expand Down
64 changes: 64 additions & 0 deletions Tests/Entity/AttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Padam87\AttributeBundle\Tests\Entity;

use \Mockery as m;
use Padam87\AttributeBundle\Entity\Attribute;

class AttributeTest extends \PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}

/**
* @test
*/
public function setValueScalar()
{
$attribute = new Attribute();
$attribute->setValue(1);

$refl = new \ReflectionProperty($attribute, 'value');
$refl->setAccessible(true);

$this->assertEquals(1, $refl->getValue($attribute));
}

/**
* @test
*/
public function setValueArray()
{
$attribute = new Attribute();
$attribute->setValue([1]);

$refl = new \ReflectionProperty($attribute, 'value');
$refl->setAccessible(true);

$this->assertEquals('a:1:{i:0;i:1;}', $refl->getValue($attribute));
}

/**
* @test
*/
public function getValueScalar()
{
$attribute = new Attribute();
$attribute->setValue(1);

$this->assertEquals(1, $attribute->getValue());
}

/**
* @test
*/
public function getValueArray()
{
$attribute = new Attribute();
$attribute->setValue([1]);

$this->assertEquals([1], $attribute->getValue());
}
}