2014-05-12 18:59:17 +02:00
|
|
|
<?php
|
|
|
|
class SafePostRetrieverTest extends AbstractTest
|
|
|
|
{
|
|
|
|
public function testRetrievingById()
|
|
|
|
{
|
|
|
|
$post = $this->preparePost();
|
|
|
|
$retriever = $this->prepareRetriever();
|
|
|
|
$retriever->getJob()->setArgument(JobArgs::ARG_POST_ID, $post->getId());
|
|
|
|
$this->assertIncorrectRetrieval($retriever);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRetrievingByName()
|
|
|
|
{
|
|
|
|
$post = $this->preparePost();
|
|
|
|
$retriever = $this->prepareRetriever();
|
|
|
|
$retriever->getJob()->setArgument(JobArgs::ARG_POST_NAME, $post->getName());
|
|
|
|
$this->assertCorrectRetrieval($retriever, $post);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRetrievingByEntity()
|
|
|
|
{
|
|
|
|
$post = $this->preparePost();
|
|
|
|
$retriever = $this->prepareRetriever();
|
|
|
|
$retriever->getJob()->setArgument(JobArgs::ARG_POST_ENTITY, $post);
|
|
|
|
$this->assertCorrectRetrieval($retriever, $post);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRetrievingByNonExistingName()
|
|
|
|
{
|
|
|
|
$retriever = $this->prepareRetriever();
|
|
|
|
$retriever->getJob()->setArgument(JobArgs::ARG_POST_NAME, 'nonsense');
|
|
|
|
$this->assert->throws(function() use ($retriever)
|
|
|
|
{
|
|
|
|
$retriever->tryRetrieve();
|
|
|
|
}, 'Invalid post name');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRetrievingNoArguments()
|
|
|
|
{
|
|
|
|
$retriever = $this->prepareRetriever();
|
|
|
|
$this->assertIncorrectRetrieval($retriever);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArgumentRequirements()
|
|
|
|
{
|
|
|
|
$retriever = $this->prepareRetriever();
|
|
|
|
$this->assert->areEquivalent(
|
|
|
|
JobArgs::Alternative(
|
|
|
|
JobArgs::ARG_POST_NAME,
|
|
|
|
JobArgs::ARG_POST_ENTITY),
|
|
|
|
$retriever->getRequiredArguments());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function assertIncorrectRetrieval($retriever)
|
|
|
|
{
|
|
|
|
$this->assert->doesNotThrow(function() use ($retriever)
|
|
|
|
{
|
|
|
|
$retriever->tryRetrieve();
|
|
|
|
});
|
|
|
|
$this->assert->isNull($retriever->tryRetrieve());
|
|
|
|
|
|
|
|
$this->assert->throws(function() use ($retriever)
|
|
|
|
{
|
|
|
|
$retriever->retrieve();
|
|
|
|
}, 'unsatisfied');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function assertCorrectRetrieval($retriever, $post)
|
|
|
|
{
|
|
|
|
$this->assert->doesNotThrow(function() use ($retriever)
|
|
|
|
{
|
|
|
|
$retriever->tryRetrieve();
|
|
|
|
});
|
|
|
|
$this->assert->isNotNull($retriever->tryRetrieve());
|
|
|
|
$this->assert->areEqual($post->getId(), $retriever->tryRetrieve()->getId());
|
|
|
|
|
|
|
|
$this->assert->doesNotThrow(function() use ($retriever)
|
|
|
|
{
|
|
|
|
$retriever->retrieve();
|
|
|
|
});
|
|
|
|
$this->assert->areEqual($post->getId(), $retriever->retrieve()->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function preparePost()
|
|
|
|
{
|
2014-05-13 21:08:07 +02:00
|
|
|
return $this->postMocker->mockSingle();
|
2014-05-12 18:59:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function prepareRetriever()
|
|
|
|
{
|
|
|
|
$job = new EditPostJob();
|
|
|
|
$postRetriever = new SafePostRetriever($job);
|
|
|
|
return $postRetriever;
|
|
|
|
}
|
|
|
|
}
|