public hydrate ( |
||
$raw_object |
public function hydrate(RawObject $raw_object)
{
$commit = new Commit();
$commit->setSha($raw_object->getSha());
list($meta, $message) = explode("\n\n", $raw_object->getData());
$commit->setMessage($message);
foreach (explode("\n", $meta) as $meta_line) {
sscanf($meta_line, "%s ", $attribute);
$attribute_value = substr($meta_line, strlen($attribute) + 1);
switch ($attribute) {
case 'tree':
$commit->setTree(new TreeProxy($this->repo, $attribute_value));
break;
case 'parent':
$commit->addParent(new CommitProxy($this->repo, $attribute_value));
break;
case 'author':
preg_match('/(.*?) <(.*?)> ([0-9]*)( (.+))?/', $attribute_value, $matches);
$commit->setAuthor(new User($matches[1], $matches[2]));
$commit->setAuthorTime(\DateTime::createFromFormat('U O', $matches[3] . ' ' . $matches[5]));
break;
case 'committer':
preg_match('/(.*?) <(.*?)> ([0-9]*)( (.+))?/', $attribute_value, $matches);
$commit->setCommitter(new User($matches[1], $matches[2]));
$commit->setCommitTime(\DateTime::createFromFormat('U O', $matches[3] . ' ' . $matches[5]));
break;
}
}
return $commit;
}
public function testCommitHydratorHappyPath() { $sha = "c3c8d149d63338c1215e0b06c839768cdf1933d5"; $data = <<<EOF tree b4ac469697c1e0f5fbb5702befc59fd7a90970a0 parent b3bf978c063bc6491919a97d1f6d6c9a6c074a2d parent deadbeefcafec0ffee19a97d1f6d6c9a6c074a2d parent cafebabe063bc6491919a97d1f6d6c9a6c074a2d author Magnus Nordlander <*****@*****.**> 1316430078 +0200 committer Magnus Nordlander <*****@*****.**> 1316430078 +0200 Updated to version 2.1.2 EOF; $raw_object = M::mock('Gittern\\Transport\\RawObject', array('getSha' => $sha, 'getData' => $data)); $hydrator = new CommitHydrator(M::mock('Gittern\\Repository')); $commit = $hydrator->hydrate($raw_object); $this->assertEquals($sha, $commit->getSha()); $this->assertEquals("b4ac469697c1e0f5fbb5702befc59fd7a90970a0", $commit->getTree()->getSha()); $parents = $commit->getParents(); $this->assertEquals("b3bf978c063bc6491919a97d1f6d6c9a6c074a2d", $parents[0]->getSha()); $user = new GitObject\User('Magnus Nordlander', "*****@*****.**"); $this->assertEquals($user, $commit->getAuthor()); $this->assertEquals($user, $commit->getCommitter()); $timestamp = \DateTime::createFromFormat('U O', '1316430078 +0200'); $this->assertEquals($timestamp, $commit->getAuthorTime()); $this->assertEquals($timestamp, $commit->getCommitTime()); }