From c09ee1e4518219122a2efd59b3d08a47816fe20e Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 24 Apr 2012 23:49:29 +0200 Subject: [PATCH] Add Phrasea subdef option types --- .../Media/Subdef/OptionType/Boolean.php | 45 ++++++++++ .../Phrasea/Media/Subdef/OptionType/Enum.php | 56 +++++++++++++ .../Media/Subdef/OptionType/OptionType.php | 14 ++++ .../Phrasea/Media/Subdef/OptionType/Range.php | 70 ++++++++++++++++ .../Media/Subdef/OptionType/BooleanTest.php | 45 ++++++++++ .../Media/Subdef/OptionType/EnumTest.php | 62 ++++++++++++++ .../Media/Subdef/OptionType/RangeTest.php | 82 +++++++++++++++++++ 7 files changed, 374 insertions(+) create mode 100644 lib/Alchemy/Phrasea/Media/Subdef/OptionType/Boolean.php create mode 100644 lib/Alchemy/Phrasea/Media/Subdef/OptionType/Enum.php create mode 100644 lib/Alchemy/Phrasea/Media/Subdef/OptionType/OptionType.php create mode 100644 lib/Alchemy/Phrasea/Media/Subdef/OptionType/Range.php create mode 100644 tests/Alchemy/Phrasea/Media/Subdef/OptionType/BooleanTest.php create mode 100644 tests/Alchemy/Phrasea/Media/Subdef/OptionType/EnumTest.php create mode 100644 tests/Alchemy/Phrasea/Media/Subdef/OptionType/RangeTest.php diff --git a/lib/Alchemy/Phrasea/Media/Subdef/OptionType/Boolean.php b/lib/Alchemy/Phrasea/Media/Subdef/OptionType/Boolean.php new file mode 100644 index 0000000000..2bc3f9051d --- /dev/null +++ b/lib/Alchemy/Phrasea/Media/Subdef/OptionType/Boolean.php @@ -0,0 +1,45 @@ +name = $name; + $this->default_value = $default_value; + + if ($default_value) + { + $this->setValue($default_value); + } + } + + public function setValue($value) + { + $this->value = (boolean) $value; + + return $this; + } + + public function getType() + { + return self::TYPE_BOOLEAN; + } + + public function getName() + { + return $this->name; + } + + public function getValue() + { + return $this->value; + } + +} \ No newline at end of file diff --git a/lib/Alchemy/Phrasea/Media/Subdef/OptionType/Enum.php b/lib/Alchemy/Phrasea/Media/Subdef/OptionType/Enum.php new file mode 100644 index 0000000000..8fceae35be --- /dev/null +++ b/lib/Alchemy/Phrasea/Media/Subdef/OptionType/Enum.php @@ -0,0 +1,56 @@ +name = $name; + $this->available = $available; + $this->default_value = $default_value; + + if ($default_value) + { + $this->setValue($default_value); + } + } + + public function setValue($value) + { + if ( ! in_array($value, $this->available)) + { + throw new \Exception_InvalidArgument('The value provided does not fit in range'); + } + + $this->value = $value; + + return $this; + } + + public function getType() + { + return self::TYPE_ENUM; + } + + public function getAvailableValues() + { + return $this->available; + } + + public function getName() + { + return $this->name; + } + + public function getValue() + { + return $this->value; + } + +} \ No newline at end of file diff --git a/lib/Alchemy/Phrasea/Media/Subdef/OptionType/OptionType.php b/lib/Alchemy/Phrasea/Media/Subdef/OptionType/OptionType.php new file mode 100644 index 0000000000..b23e459434 --- /dev/null +++ b/lib/Alchemy/Phrasea/Media/Subdef/OptionType/OptionType.php @@ -0,0 +1,14 @@ +name = $name; + $this->min_value = $min_value; + $this->max_value = $max_value; + $this->default_value = $default_value; + $this->step = $step; + + if ($default_value) + { + $this->setValue($default_value); + } + } + + public function setValue($value) + { + if ($value > $this->max_value || $value < $this->min_value) + { + throw new \Exception_InvalidArgument('The value provided does not fit in range'); + } + + $this->value = $value; + + return $this; + } + + public function getType() + { + return self::TYPE_RANGE; + } + + public function getName() + { + return $this->name; + } + + public function getValue() + { + return $this->value; + } + + public function getStep() + { + return $this->step; + } + + public function getMinValue() + { + return $this->min_value; + } + + public function getMaxValue() + { + return $this->max_value; + } + +} \ No newline at end of file diff --git a/tests/Alchemy/Phrasea/Media/Subdef/OptionType/BooleanTest.php b/tests/Alchemy/Phrasea/Media/Subdef/OptionType/BooleanTest.php new file mode 100644 index 0000000000..b2830edc66 --- /dev/null +++ b/tests/Alchemy/Phrasea/Media/Subdef/OptionType/BooleanTest.php @@ -0,0 +1,45 @@ +object = new Boolean('boolean', true); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Boolean::setValue + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Boolean::getValue + */ + public function testSetValue() + { + $this->assertTrue($this->object->getValue()); + $this->object->setValue(false); + $this->assertFalse($this->object->getValue()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Boolean::getType + */ + public function testGetType() + { + $this->assertEquals(OptionType::TYPE_BOOLEAN, $this->object->getType()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Boolean::getName + */ + public function testGetName() + { + $this->assertEquals('boolean', $this->object->getName()); + } + +} diff --git a/tests/Alchemy/Phrasea/Media/Subdef/OptionType/EnumTest.php b/tests/Alchemy/Phrasea/Media/Subdef/OptionType/EnumTest.php new file mode 100644 index 0000000000..b8932650d8 --- /dev/null +++ b/tests/Alchemy/Phrasea/Media/Subdef/OptionType/EnumTest.php @@ -0,0 +1,62 @@ +object = new Enum('enumerateur', array('un', 'dos', 'tres'), 'dos'); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Enum::setValue + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Enum::getValue + */ + public function testSetValue() + { + $this->assertEquals('dos', $this->object->getValue()); + $this->object->setValue('tres'); + $this->assertEquals('tres', $this->object->getValue()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Enum::setValue + * @expectedException \Exception_InvalidArgument + */ + public function testSetWrongValue() + { + $this->object->setValue('deux'); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Enum::getType + */ + public function testGetType() + { + $this->assertEquals(OptionType::TYPE_ENUM, $this->object->getType()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Enum::getAvailableValues + */ + public function testGetAvailableValues() + { + $this->assertEquals(array('un', 'dos', 'tres'), $this->object->getAvailableValues()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Enum::getName + */ + public function testGetName() + { + $this->assertEquals('enumerateur', $this->object->getName()); + } + +} diff --git a/tests/Alchemy/Phrasea/Media/Subdef/OptionType/RangeTest.php b/tests/Alchemy/Phrasea/Media/Subdef/OptionType/RangeTest.php new file mode 100644 index 0000000000..e27d40e650 --- /dev/null +++ b/tests/Alchemy/Phrasea/Media/Subdef/OptionType/RangeTest.php @@ -0,0 +1,82 @@ +object = new Range('name', 3, 8, 6, 2); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Range::setValue + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Range::getValue + */ + public function testSetValue() + { + $this->assertEquals(6, $this->object->getValue()); + $this->object->setValue(8); + $this->assertEquals(8, $this->object->getValue()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Range::setValue + * @expectedException \Exception_InvalidArgument + */ + public function testSetWrongValue() + { + $this->object->setValue(9); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Range::getType + */ + public function testGetType() + { + $this->assertEquals(OptionType::TYPE_RANGE, $this->object->getType()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Range::getName + */ + public function testGetName() + { + $this->assertEquals('name', $this->object->getName()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Range::getStep + */ + public function testGetStep() + { + $this->assertEquals(2, $this->object->getStep()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Range::getMinValue + */ + public function testGetMinValue() + { + $this->assertEquals(3, $this->object->getMinValue()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Range::getMaxValue + */ + public function testGetMaxValue() + { + $this->assertEquals(8, $this->object->getMaxValue()); + } + +}