Add Phrasea subdef option types

This commit is contained in:
Romain Neutron
2012-04-24 23:49:29 +02:00
parent 03631b874b
commit c09ee1e451
7 changed files with 374 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
<?php
namespace Alchemy\Phrasea\Media\Subdef\OptionType;
class Boolean implements OptionType
{
protected $name;
protected $default_value;
protected $value;
public function __construct($name, $default_value = null)
{
$this->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;
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Alchemy\Phrasea\Media\Subdef\OptionType;
class Enum implements OptionType
{
protected $default_value;
protected $value;
protected $available;
public function __construct($name, Array $available, $default_value = null)
{
$this->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;
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Alchemy\Phrasea\Media\Subdef\OptionType;
interface OptionType
{
const TYPE_RANGE = 'Range';
const TYPE_ENUM = 'Enum';
const TYPE_BOOLEAN = 'Boolean';
public function getType();
public function getName();
public function getValue();
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Alchemy\Phrasea\Media\Subdef\OptionType;
class Range implements OptionType
{
protected $min_value;
protected $max_value;
protected $default_value;
protected $value;
protected $step;
public function __construct($name, $min_value, $max_value, $default_value = null, $step = 1)
{
$this->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;
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Alchemy\Phrasea\Media\Subdef\OptionType;
class BooleanTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Boolean
*/
protected $object;
protected function setUp()
{
$this->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());
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Alchemy\Phrasea\Media\Subdef\OptionType;
class EnumTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Enum
*/
protected $object;
protected function setUp()
{
$this->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());
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Alchemy\Phrasea\Media\Subdef\OptionType;
class RangeTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Range
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->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());
}
}