diff --git a/lib/Alchemy/Phrasea/Exception/Exception.php b/lib/Alchemy/Phrasea/Exception/Exception.php new file mode 100644 index 0000000000..40f363cadb --- /dev/null +++ b/lib/Alchemy/Phrasea/Exception/Exception.php @@ -0,0 +1,17 @@ +displayName = $displayName; + $this->name = $name; + $this->available = array(); + foreach ($available as $a) { + $this->available[$a] = false; + } + $this->defaultValue = $defaultValue; + + if ($defaultValue) { + $this->setValue($defaultValue); + } + } + + public function setValue($value) + { + foreach ($this->available as $k => $v) { + $this->available[$k] = false; + } + + if ( ! $value) { + + return $this; + } + + foreach ((array) $value as $v) { + if ( ! array_key_exists($v, $this->available)) { + throw new \Exception_InvalidArgument( + sprintf( + 'The value provided `%s` for %s does not fit in range ; available are %s' + , $value + , $this->getName() + , implode(', ', $this->getAvailableValues()) + ) + ); + } + + $this->available[$v] = true; + } + + return $this; + } + + public function getDisplayName() + { + return $this->displayName; + } + + public function getType() + { + return self::TYPE_MULTI; + } + + public function getAvailableValues() + { + return array_keys($this->available); + } + + public function getName() + { + return $this->name; + } + + public function getValue($all = false) + { + if ($all) { + return $this->available; + } + + $value = array(); + + foreach ($this->available as $a => $selected) { + if ($selected) { + $value[] = $a; + } + } + + return $value; + } +} diff --git a/tests/Alchemy/Phrasea/Media/Subdef/OptionType/MultiTest.php b/tests/Alchemy/Phrasea/Media/Subdef/OptionType/MultiTest.php new file mode 100644 index 0000000000..334cb846e0 --- /dev/null +++ b/tests/Alchemy/Phrasea/Media/Subdef/OptionType/MultiTest.php @@ -0,0 +1,69 @@ +object = new Multi('MUMU', 'multiateur', array('un', 'dos', 'tres'), 'dos'); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Multi::setValue + */ + public function testSetValue() + { + $this->assertEquals(array('dos'), $this->object->getValue()); + $this->object->setValue('tres'); + $this->assertEquals(array('tres'), $this->object->getValue()); + $this->object->setValue(''); + $this->assertEquals(array(), $this->object->getValue()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Multi::setValue + * @expectedException \Exception_InvalidArgument + */ + public function testSetWrongValue() + { + $this->object->setValue('deux'); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Multi::getType + */ + public function testGetType() + { + $this->assertEquals(OptionType::TYPE_MULTI, $this->object->getType()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Multi::getAvailableValues + */ + public function testGetAvailableValues() + { + $this->assertEquals(array('un', 'dos', 'tres'), $this->object->getAvailableValues()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Multi::getName + */ + public function testGetName() + { + $this->assertEquals('multiateur', $this->object->getName()); + } + + /** + * @covers Alchemy\Phrasea\Media\Subdef\OptionType\Multi::getDisplayName + */ + public function testGetDisplayName() + { + $this->assertEquals('MUMU', $this->object->getDisplayName()); + } +} diff --git a/tests/media/media_subdefTest.php b/tests/media/media_subdefTest.php new file mode 100644 index 0000000000..5daa19974c --- /dev/null +++ b/tests/media/media_subdefTest.php @@ -0,0 +1,273 @@ +generate_subdefs(self::$recordonbleu->get_databox()); + + foreach (self::$recordonbleu->get_subdefs() as $subdef) { + + if ($subdef->get_name() == 'document') { + continue; + } + + if ( ! self::$objectPresent) { + self::$objectPresent = $subdef; + continue; + } + if ( ! self::$objectNotPresent) { + self::$objectNotPresent = $subdef; + continue; + } + } + + self::$objectNotPresent->remove_file(); + } + + /** + * @covers media_subdef::is_physically_present + */ + public function testIs_physically_present() + { + $this->assertTrue(self::$objectPresent->is_physically_present()); + $this->assertFalse(self::$objectNotPresent->is_physically_present()); + } + + /** + * @covers media_subdef::get_record + */ + public function testGet_record() + { + $this->assertEquals(self::$recordonbleu->get_record_id(), self::$objectNotPresent->get_record()->get_record_id()); + $this->assertEquals(self::$recordonbleu->get_record_id(), self::$objectPresent->get_record()->get_record_id()); + $this->assertEquals(self::$recordonbleu->get_sbas_id(), self::$objectNotPresent->get_record()->get_sbas_id()); + $this->assertEquals(self::$recordonbleu->get_sbas_id(), self::$objectPresent->get_record()->get_sbas_id()); +} + + /** + * @covers media_subdef::get_permalink + */ + public function testGet_permalink() + { + $this->assertInstanceOf('\\media_Permalink_adapter', self::$objectNotPresent->get_permalink()); + $this->assertInstanceOf('\\media_Permalink_adapter', self::$objectPresent->get_permalink()); + } + + /** + * @covers media_subdef::get_record_id + */ + public function testGet_record_id() + { + $this->assertEquals(self::$recordonbleu->get_record_id(), self::$objectNotPresent->get_record()->get_record_id()); + $this->assertEquals(self::$recordonbleu->get_record_id(), self::$objectPresent->get_record()->get_record_id()); + } + + /** + * @covers media_subdef::getEtag + */ + public function testGetEtag() + { + $this->assertNull(self::$objectNotPresent->getEtag()); + $this->assertInternalType('string', self::$objectPresent->getEtag()); + $this->assertRegExp('/[a-zA-Z0-9]{32}/', self::$objectPresent->getEtag()); + } + + /** + * @covers media_subdef::setEtag + */ + public function testSetEtag() + { + $etag = md5('random'); + self::$objectNotPresent->setEtag($etag); + $this->assertEquals($etag, self::$objectNotPresent->getEtag()); + } + + /** + * @covers media_subdef::get_sbas_id + */ + public function testGet_sbas_id() + { + $this->assertEquals(self::$recordonbleu->get_sbas_id(), self::$objectNotPresent->get_record()->get_sbas_id()); + $this->assertEquals(self::$recordonbleu->get_sbas_id(), self::$objectPresent->get_record()->get_sbas_id()); + } + + /** + * @covers media_subdef::get_type + */ + public function testGet_type() + { + $this->assertEquals(\media_subdef::TYPE_IMAGE, self::$objectPresent->get_type()); + } + + /** + * @covers media_subdef::get_mime + */ + public function testGet_mime() + { + $this->assertEquals('image/jpeg', self::$objectPresent->get_mime()); + $this->assertEquals('image/png', self::$objectNotPresent->get_mime()); + } + + /** + * @covers media_subdef::get_path + */ + public function testGet_path() + { + $this->assertEquals(dirname(self::$objectPresent->get_pathfile()) . DIRECTORY_SEPARATOR, self::$objectPresent->get_path()); + $this->assertEquals(dirname(self::$objectNotPresent->get_pathfile()) . DIRECTORY_SEPARATOR, self::$objectNotPresent->get_path()); + } + + /** + * @covers media_subdef::get_baseurl + */ + public function testGet_baseurl() + { + $this->assertInternalType('string', self::$objectPresent->get_baseurl()); + $this->assertInternalType('string', self::$objectNotPresent->get_baseurl()); + } + + /** + * @covers media_subdef::get_file + */ + public function testGet_file() + { + $this->assertEquals(basename(self::$objectPresent->get_pathfile()), self::$objectPresent->get_file()); + $this->assertEquals(basename(self::$objectNotPresent->get_pathfile()), self::$objectNotPresent->get_file()); + } + + /** + * @covers media_subdef::get_size + */ + public function testGet_size() + { + $this->assertTrue(self::$objectPresent->get_size() > 0); + $this->assertTrue(self::$objectNotPresent->get_size() > 0); + } + + /** + * @covers media_subdef::get_name + */ + public function testGet_name() + { + $this->assertTrue(in_array(self::$objectPresent->get_name(), array('thumbnail', 'preview'))); + $this->assertTrue(in_array(self::$objectNotPresent->get_name(), array('thumbnail', 'preview'))); + } + + /** + * @covers media_subdef::get_subdef_id + */ + public function testGet_subdef_id() + { + $this->assertInternalType('int', self::$objectPresent->get_subdef_id()); + $this->assertInternalType('int', self::$objectNotPresent->get_subdef_id()); + $this->assertTrue(self::$objectPresent->get_size() > 0); + $this->assertTrue(self::$objectNotPresent->get_size() > 0); + } + + /** + * @covers media_subdef::is_substituted + */ + public function testIs_substituted() + { + $this->assertFalse(self::$objectPresent->is_substituted()); + $this->assertTrue(self::$objectNotPresent->is_substituted()); + } + + /** + * @covers media_subdef::get_pathfile + */ + public function testGet_pathfile() + { + $this->assertEquals(self::$objectPresent->get_path() . self::$objectPresent->get_file(), self::$objectPresent->get_pathfile()); + $this->assertEquals(self::$objectNotPresent->get_path() . self::$objectNotPresent->get_file(), self::$objectNotPresent->get_pathfile()); + $this->assertTrue(file_exists(self::$objectPresent->get_pathfile())); + $this->assertTrue(file_exists(self::$objectNotPresent->get_pathfile())); + $this->assertTrue(is_readable(self::$objectPresent->get_pathfile())); + $this->assertTrue(is_readable(self::$objectNotPresent->get_pathfile())); + $this->assertTrue(is_writable(self::$objectPresent->get_pathfile())); + $this->assertTrue(is_writable(self::$objectNotPresent->get_pathfile())); + } + + /** + * @covers media_subdef::get_modification_date + */ + public function testGet_modification_date() + { + $this->assertInstanceOf('\\DateTime', self::$objectPresent->get_modification_date()); + $this->assertInstanceOf('\\DateTime', self::$objectNotPresent->get_modification_date()); + } + + /** + * @covers media_subdef::get_creation_date + */ + public function testGet_creation_date() + { + $this->assertInstanceOf('\\DateTime', self::$objectPresent->get_creation_date()); + $this->assertInstanceOf('\\DateTime', self::$objectNotPresent->get_creation_date()); + } + + /** + * @covers media_subdef::renew_url + */ + public function testRenew_url() + { + $this->assertInternalType('string', self::$objectPresent->renew_url()); + $this->assertInternalType('string', self::$objectNotPresent->renew_url()); + } + + /** + * @covers media_subdef::getDataboxSubdef + */ + public function testGetDataboxSubdef() + { + $this->assertInstanceOf('\\databox_subdef', self::$objectPresent->getDataboxSubdef()); + $this->assertInstanceOf('\\databox_subdef', self::$objectNotPresent->getDataboxSubdef()); + } + + /** + * @covers media_subdef::rotate + */ + public function testRotate() + { + $width_before = self::$objectPresent->get_width(); + $height_before = self::$objectPresent->get_height(); + + self::$objectPresent->rotate(90); + + $this->assertEquals($width_before, self::$objectPresent->get_height()); + $this->assertEquals($height_before, self::$objectPresent->get_width()); + } + + /** + * @covers media_subdef::rotate + * @expectedException \Alchemy\Phrasea\Exception\RuntimeException + * @covers \Alchemy\Phrasea\Exception\RuntimeException + */ + public function testRotateOnSubstitution() + { + self::$objectNotPresent->rotate(90); + } +}