From 02672885ca38a5beb9cdae0491460674819d2bd0 Mon Sep 17 00:00:00 2001 From: Adam Prager Date: Mon, 16 Nov 2015 20:00:24 +0100 Subject: [PATCH] Fixed multiple choice value storage --- Entity/Attribute.php | 8 +++++ Tests/Entity/AttributeTest.php | 64 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 Tests/Entity/AttributeTest.php diff --git a/Entity/Attribute.php b/Entity/Attribute.php index 8ba62b5..00a32a8 100644 --- a/Entity/Attribute.php +++ b/Entity/Attribute.php @@ -57,6 +57,10 @@ public function getId() */ public function setValue($value) { + if (is_array($value)) { + $value = serialize($value); + } + $this->value = $value; return $this; @@ -67,6 +71,10 @@ public function setValue($value) */ public function getValue() { + if (false !== $unserialized = @unserialize($this->value)) { + return $unserialized; + } + return $this->value; } diff --git a/Tests/Entity/AttributeTest.php b/Tests/Entity/AttributeTest.php new file mode 100644 index 0000000..bb5783f --- /dev/null +++ b/Tests/Entity/AttributeTest.php @@ -0,0 +1,64 @@ +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()); + } +}