Commit 45dc419f authored by ZZZ[GONE] frebault's avatar ZZZ[GONE] frebault
Browse files

Fields Test operational

parent 181bc6d8
......@@ -8,19 +8,21 @@
namespace GGUSHelpdesk\ExtendedFields;
class ArrayObjectFactory extends \ArrayObject implements IXFieldsFactory{
protected $data = array();
class ArrayObjectFactory implements IExtendedFieldsFactory{
function createInstanceFromFieldsList(array $fieldsList)
{
$this->data = $fieldsList;
return $this->serialize();
return new \ArrayObject($fieldsList,\ArrayObject::ARRAY_AS_PROPS);
}
function createInstanceFromString($string)
function createInstanceFromSerializedObject($serializedObject)
{
return $this->unserialize($string);
$arrayObject = new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS);
$arrayObject->unserialize($serializedObject);
return $arrayObject;
}
function serializeInstance($object){
return $object->serialize();
}
}
\ No newline at end of file
......@@ -9,9 +9,10 @@
namespace GGUSHelpdesk\ExtendedFields;
interface IXFieldsFactory {
interface IExtendedFieldsFactory {
function createInstanceFromFieldsList(array $fieldsList);
function createInstanceFromString($string);
function createInstanceFromSerializedObject($string);
function serializeInstance($object);
}
\ No newline at end of file
......@@ -2,8 +2,7 @@
namespace GGUSHelpdesk;
use GGUSHelpdesk\Exceptions\InvalidFieldException;
use GGUSHelpdesk\ExtendedFields\ArrayObjectFactory;
use GGUSHelpdesk\ExtendedFields\IExtendedFieldsFactory;
/**
* provide basic behaviour a GGUS Ticket's Fields
......@@ -13,51 +12,48 @@ class Fields extends ArrayFields
const LIST_TYPE_X = 1;
const MAP_TYPE_C = 2;
const FIELD_ID = 'XMLFields';
const FIELD_ID = 'EXTENDED_FIELD_INDEX';
private $cMap;
private $xmlKeyList;
private $extendedFieldsKeys;
private $extendedFieldsIndex;
private $extendedFieldsFactory;
public function __construct(array $cMap, array $xmlKeyList = array())
public function __construct(array $cMap, array $extendedFieldsKeys = array())
{
$this->cMap = $cMap;
$this->xmlKeyList = $xmlKeyList;
$this->extendedFieldsIndex = array();
$this->extendedFieldsKeys = $extendedFieldsKeys;
$this->extendedFieldsIndex = null;
$this->extendedFieldsFactory = null;
parent::__construct();
}
// public function init()
// {
// }
public function setExtendedFields($index = self::FIELD_ID)
public function initExtendedFields()
{
if (isset($this->cMap[$index])) {
$this->setExtendedFieldsFactory(new ArrayObjectFactory());
$this->extendedFieldsIndex = $this->cMap[$index];
$this->setXmlFieldsString($this->xmlKeyList);
if (isset($this->cMap[self::FIELD_ID])) {
if($this->extendedFieldsFactory == null) {
throw new \ErrorException("Please set en extended fields factory");
}
$this->extendedFieldsIndex = $this->cMap[self::FIELD_ID];
$this[$this->extendedFieldsIndex] = $this->extendedFieldsFactory->createInstanceFromFieldsList($this->extendedFieldsKeys);
} else {
throw new InvalidFieldException("Unable to set extended field at the given index : " . self::FIELD_ID);
}
}
/*
* $entriesHydrator = new EntriesHydrator();
$dataEntry = $entriesHydrator->hydrate($data);
*/
public function setXmlFieldsString($data)
public function setExtendedFieldsFromSerializedObject($data)
{
$this[$this->extendedFieldsIndex] = $this->extendedFieldsFactory->createInstanceFromFieldsList($data);
$this[$this->extendedFieldsIndex] = $this->extendedFieldsFactory->createInstanceFromSerializedObject($data);
}
public function getXmlField()
public function getExtendedField()
{
return $this->extendedFieldsFactory->creatInstanceFromString($this[$this->extendedFieldsIndex]);
return $this[$this->extendedFieldsIndex];
}
public function setExtendedFieldsFactory(\ArrayObject $factory){
public function setExtendedFieldsFactory(IExtendedFieldsFactory $factory)
{
$this->extendedFieldsFactory = $factory;
}
......@@ -67,7 +63,7 @@ class Fields extends ArrayFields
if (isset($this->cMap[$name])) {
return self::MAP_TYPE_C;
}
if (isset($this->xmlKeyList[$name])) {
if (isset($this->extendedFieldsKeys[$name])) {
return self::LIST_TYPE_X;
}
return null;
......@@ -105,7 +101,7 @@ class Fields extends ArrayFields
} else {
if ($map === self::MAP_TYPE_C) {
if (self::FIELD_ID === $name) {
return $this[$this->extendedFieldsIndex]->serialize();
return $this->extendedFieldsFactory->serializeInstance($this[$this->extendedFieldsIndex]);
} else {
if (isset($this[$this->cMap[$name]])) {
return $this[$this->cMap[$name]];
......@@ -114,7 +110,6 @@ class Fields extends ArrayFields
throw new InvalidFieldException("Undefined '$name' field");
} else return null;
}
}
} else {
if (isset($this[$this->extendedFieldsIndex][$name])) {
......@@ -135,7 +130,7 @@ class Fields extends ArrayFields
$output = $this->getData();
if (isset($this->extendedFieldsIndex)) {
$output[$this->extendedFieldsIndex] = $this[$this->extendedFieldsIndex]->serialize();
$output[$this->extendedFieldsIndex] = $this->extendedFieldsFactory->serializeInstance($this[$this->extendedFieldsIndex]);
}
return $output;
}
......@@ -147,12 +142,11 @@ class Fields extends ArrayFields
foreach ($this->cMap as $index => $value) {
if ($index === self::FIELD_ID) {
// make xmlFields as a flat array
foreach ($this->xmlKeyList as $xIdx => $path) {
foreach ($this->extendedFieldsKeys as $xIdx => $path) {
try {
if(isset($this[$this->extendedFieldsIndex][$xIdx])) {
if (isset($this[$this->extendedFieldsIndex][$xIdx])) {
$xValue = $this[$this->extendedFieldsIndex][$xIdx];
}
else {
} else {
$xValue = 'N.A.';
}
} catch (InvalidFieldException $e) {
......
<?php
/**
* Created by PhpStorm.
* User: Pierre FREBAULT
* Date: 17/10/2014
* Time: 16:12
*/
use GGUSHelpdesk\Fields;
class FieldsTest extends PHPUnit_Framework_TestCase {
public function setUp()
{
$this->map = array(
'Vo' => 'GHD_VO_Specific',
'AuthorEmail' => 'GHD_E-Mail',
'Login' => 'GHD_Last_Login',
'Modifier' => 'GHD_Last_Modifier',
'AuthorDn' => 'GHD_Cert_DN',
'Author' => 'GHD_Submitter',
'Subject' => 'GHD_Subject',
'Type' => 'GHD_TicketType',
'Status' => 'GHD_Status',
'ResponsibleUnit' => 'GHD_Responsible_Unit',
'Comment' => 'GHD_Public_Diary',
'Involve' => 'GHD_Involve',
'Site' => 'GHD_Affected_Site',
'CarbonCopy' => 'GHD_Carbon_Copy',
'EXTENDED_FIELD_INDEX' => 'GHD_Soap_Client_Data',
'Description' => 'GHD_Description',
'Priority' => 'GHD_Priority',
'ProblemType' => 'GHD_ProblemType',
'Id' => 'GHD_Request_ID',
'Solution' => 'GHD_Detailed_Solution',
'ResponsibleEmail' => 'GHD_Assigned_To',
'MetaStatus' => 'GHD_Meta_Status',
'ModificationDate' => 'GHD_Modified_Date',
'CreationDate' => 'GHD_Create_Date',
'NotificationStrategy' => 'GHD_User_Notification'
);
$this->xmlKeyList = array(
'Helpdesk' => '',
'Workflow' => '',
'Step' => '',
'WorkflowStepLabel' => '',
'Community' => '',
'SubCommunity' => '',
'EndDate' => '',
'GroupId' => '',
'GroupUrl' => ''
);
}
/**
* @expectedException ggushelpdesk\Exceptions\InvalidFieldException
*/
public function testInitExtendedFieldsNoIndex(){
$gf = new Fields(array(), $this->xmlKeyList);
$gf->initExtendedFields();
}
/**
* @expectedException \ErrorException
*/
public function testInitExtendedFactoryNotSet(){
$gf = new Fields($this->map, $this->xmlKeyList);
$gf->initExtendedFields();
}
public function testIniExtendedFields(){
$gf = new Fields($this->map, $this->xmlKeyList);
$gf->setExtendedFieldsFactory(new \GGUSHelpdesk\ExtendedFields\ArrayObjectFactory());
$gf->initExtendedFields();
// var_dump($gf->getArrayCopy());
}
public function testSetters()
{
$gf = new Fields($this->map, $this->xmlKeyList);
$gf->setExtendedFieldsFactory(new \GGUSHelpdesk\ExtendedFields\ArrayObjectFactory());
$gf->initExtendedFields();
// in xml
$gf->setValue('Helpdesk', 'ONE');
$this->assertEquals($gf->getValue('Helpdesk'), 'ONE');
// in classic
$gf->setValue('Description', 'This is a descr');
$this->assertEquals($gf->getValue('Description'), 'This is a descr');
// in classic
$gf->setValue('CarbonCopy', 'toto');
$this->assertEquals($gf->getValue('CarbonCopy'), 'toto');
return $gf;
}
/**
* @expectedException ggushelpdesk\Exceptions\InvalidFieldException
*/
public function testGetException()
{
// in xml
$gf = new Fields($this->map, $this->xmlKeyList);
$gf->setExtendedFieldsFactory(new \GGUSHelpdesk\ExtendedFields\ArrayObjectFactory());
$gf->initExtendedFields();
$gf->getValue('toto');
}
/**
* @expectedException ggushelpdesk\Exceptions\InvalidFieldException
*/
public function testSetException()
{
// in xml
$gf = new Fields($this->map, $this->xmlKeyList);
$gf->setExtendedFieldsFactory(new \GGUSHelpdesk\ExtendedFields\ArrayObjectFactory());
$gf->initExtendedFields();
$gf->setValue('toto', 0);
}
/**
* @depends testSetters
*/
public function testIndexedArray(Fields $gf)
{
$gf = new Fields($this->map, $this->xmlKeyList);
$gf->setExtendedFieldsFactory(new \GGUSHelpdesk\ExtendedFields\ArrayObjectFactory());
$gf->initExtendedFields();
$gf->setValue('Helpdesk', 'ONE');
$gf->setValue('Description', 'This is a descr');
$gf->setValue('CarbonCopy', 'toto');
$data = array(
'GHD_Description' => "This is a descr",
'GHD_Carbon_Copy' => 'toto',
'GHD_Soap_Client_Data' => 'x:i:2;a:9:{s:8:"Helpdesk";s:3:"ONE";s:8:"Workflow";s:0:"";s:4:"Step";s:0:"";s:17:"WorkflowStepLabel";s:0:"";s:9:"Community";s:0:"";s:12:"SubCommunity";s:0:"";s:7:"EndDate";s:0:"";s:7:"GroupId";s:0:"";s:8:"GroupUrl";s:0:"";};m:a:0:{}'
);
$res = $gf->toIndexedArray();
$this->assertEquals($res['GHD_Carbon_Copy'], $data['GHD_Carbon_Copy']);
$this->assertEquals($res['GHD_Description'], $data['GHD_Description']);
$this->assertEquals($res['GHD_Soap_Client_Data'], $data['GHD_Soap_Client_Data']);
$this->assertEquals($gf->getValue(Fields::FIELD_ID), $data['GHD_Soap_Client_Data']);
}
/**
* @expectedException ggushelpdesk\Exceptions\InvalidFieldException
*/
public function testSetXMLField()
{
$gf = new Fields($this->map, $this->xmlKeyList);
$gf->setExtendedFieldsFactory(new \GGUSHelpdesk\ExtendedFields\ArrayObjectFactory());
$gf->initExtendedFields();
$gf->setValue(Fields::FIELD_ID, null);
}
public function testMappedArray()
{
$gf = new Fields($this->map, $this->xmlKeyList);
$gf->setExtendedFieldsFactory(new \GGUSHelpdesk\ExtendedFields\ArrayObjectFactory());
$gf->initExtendedFields();
$data = array(
'Helpdesk' => '',
'Workflow' => '',
'Step' => '',
'WorkflowStepLabel' => '',
'Community' => '',
'SubCommunity' => '',
'EndDate' => '',
'GroupId' => '',
'GroupUrl' => '',
'CreationDate' => '2012-12-15'
);
$gf->setValue('CreationDate', '2012-12-15');
$this->assertEquals($gf->toMappedArray(), $data);
}
public function testSetExtendedFieldsFromSerializedObject(){
$gf = new Fields($this->map, $this->xmlKeyList);
$gf->setExtendedFieldsFactory(new \GGUSHelpdesk\ExtendedFields\ArrayObjectFactory());
$gf->initExtendedFields();
$data = 'x:i:2;a:9:{s:8:"Helpdesk";s:0:"";s:8:"Workflow";s:0:"";s:4:"Step";s:0:"";s:17:"WorkflowStepLabel";s:0:"";s:9:"Community";s:0:"";s:12:"SubCommunity";s:0:"";s:7:"EndDate";s:0:"";s:7:"GroupId";s:0:"";s:8:"GroupUrl";s:0:"";};m:a:0:{}';
$gf->setExtendedFieldsFromSerializedObject($data);
$this->assertEquals($this->xmlKeyList,$gf->getExtendedField()->getArrayCopy());
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment