Skip to content

Commit

Permalink
new Tag class
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosh committed Jan 18, 2015
1 parent bed035f commit 32e101e
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 134 deletions.
211 changes: 120 additions & 91 deletions src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,126 @@

namespace Marcosh\EventGraph;

use PhpOrient\Protocols\Binary\Data\Record;

class Tag
{
// /**
// * @var string
// */
// private $id;

// /**
// * @var string
// */
// private $name;

// /**
// * @var Event
// */
// private $first;

// /**
// * @var Event
// */
// private $last;

// /**
// * @param string
// * @return Tag
// */
// public function setId($id)
// {
// $this->id = $id;
// return $this;
// }

// /**
// * @return mixed null|string
// */
// public function getId()
// {
// return $this->id;
// }

// /**
// * @param string
// * @return Tag
// */
// public function setName($name)
// {
// $this->name = $name;
// return $this;
// }

// /**
// * @return string
// */
// public function getName()
// {
// return $this->name;
// }

// /**
// * @param string id of the event
// * @return Tag
// */
// public function setFirstEvent($event)
// {
// $this->first = $event;
// return $this;
// }

// /**
// * @return string id of the event
// */
// public function getFirstEvent()
// {
// return $this->first;
// }

// /**
// * @param string id of the event
// * @return Tag
// */
// public function setLastEvent($event)
// {
// $this->last = $event;
// return $this;
// }

// /**
// * @return string id of the event
// */
// public function getLastEvent()
// {
// return $this->last;
// }
/**
* @var string
*/
private $name;

/**
* @var array
*/
private $history;

/**
* underlying Orient database record
*/
private $record;

public function __construct()
{
$this->record = $this->createTagRecord();
}

/**
* @param string
* @return Tag
*/
public function setName($name)
{
$this->name = $name;
$this->saveToRecord();
return $this;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @param array of Event ids
* @return Tag
*/
public function setHistory(array $history)
{
$this->history = $history;
$this->saveToRecord();
return $this;
}

/**
* @param string Event id //TODO: are we sure we are using strings?
* @return Tag
*/
public function addEvent($eventId)
{
$this->history[] = $eventId;
$this->saveToRecord();
return $this;
}

/**
* @return array of Event ids
*/
public function getHistory()
{
return $this->history;
}

/**
* @param Record
* @return Tag
*/
public function setRecord(Record $record)
{
if ($record->getOClass() != 'Tag') {
throw new \Exception('A Tag object can have only a "Tag" record');
}

$this->record = $record;
$data = $record->getOData();

if (isset($data['name'])) {
$this->name = $data['name'];
}

if (isset($data['history'])) {
$this->history = $data['history'];
}
return $this;
}

/**
* @return Record
*/
public function getRecord()
{
return $this->record;
}

/**
* @return Record
*/
private function createTagRecord()
{
$record = new Record();
$record->setOClass('Tag');
return $record;
}

private function saveToRecord()
{
$data = [
'name' => $this->name,
'history' => $this->history
];
$this->record->setOData($data);
}
}
130 changes: 87 additions & 43 deletions test/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,93 @@

namespace Marcosh\EventGraph;

use PhpOrient\Protocols\Binary\Data\Record;

class TagTest extends \PHPUnit_Framework_TestCase
{
// public function testId()
// {
// $id = '#0:0';
// $tag = new Tag();
// $tag->setId($id);
// $this->assertEquals($id, $tag->getId());
// }

// public function testName()
// {
// $name = 'tag';
// $tag = new Tag();
// $tag->setName($name);
// $this->assertEquals($name, $tag->getName());
// }

// public function testFirstEvent()
// {
// $event = '#0:0';
// $tag = new Tag();
// $tag->setFirstEvent($event);
// $this->assertEquals($event, $tag->getFirstEvent());
// }

// public function testLastEvent()
// {
// $event = '#0:1';
// $tag = new Tag();
// $tag->setLastEvent($event);
// $this->assertEquals($event, $tag->getLastEvent());
// }

// public function testTagWithNoHistoryHasNullFirstEvent()
// {
// $tag = new Tag();
// $this->assertNull($tag->getFirstEvent());
// }

// public function testTagWithNoHistoryHasNullLastEvent()
// {
// $tag = new Tag();
// $this->assertNull($tag->getLastEvent());
// }
public function testCreationCreatesTagRecord()
{
$tag = new Tag();
$record = $tag->getRecord();
$this->assertEquals('Tag', $record->getOClass());
}

public function testSetName()
{
$name = 'name';
$tag = new Tag();
$tag->setName($name);
$record = $tag->getRecord();
$this->assertEquals($name, $record->getOData()['name']);
}

public function testGetName()
{
$name = 'tag';
$tag = new Tag();
$tag->setName($name);
$this->assertEquals($name, $tag->getName());
}

public function testSetHistory()
{
$history = array('#0:0', '#0:1');
$tag = new Tag();
$tag->setHistory($history);
$record = $tag->getRecord();
$this->assertEquals($history, $record->getOData()['history']);
}

public function testGetHistory()
{
$history = array('#0:0', '#0:1');
$tag = new Tag();
$tag->setHistory($history);
$this->assertEquals($history, $tag->getHistory());
}

public function testAddEvent()
{
$event = '#0:0';
$tag = new Tag();
$tag->addEvent($event);
$record = $tag->getRecord();
$this->assertEquals(array($event), $record->getOData()['history']);
}

public function testAddEventFromEmptyHistory()
{
$event = '#0:0';
$tag = new Tag();
$tag->addEvent($event);
$this->assertEquals(array($event), $tag->getHistory());
}

public function testAddEventToPoupulatedHistory()
{
$history = array('#0:0');
$event = '#0:1';
$tag = new Tag();
$tag->setHistory($history);
$tag->addEvent($event);
array_push($history, $event);
$this->assertEquals($history, $tag->getHistory());
}

public function testNewTagHasEmptyHistory()
{
$tag = new Tag();
$this->assertEmpty($tag->getHistory());
}

/**
* @expectedException Exception
*/
public function testSetRecordThrowsIfWrongClass()
{
$record = new Record();
$record->setOClass('NotATag');
$tag = new Tag();
$tag->setRecord($record);
}
}

0 comments on commit 32e101e

Please sign in to comment.