Skip to content

Commit

Permalink
Merge pull request #3 from tomphp/master
Browse files Browse the repository at this point in the history
Add optional  parameter to getEntity
  • Loading branch information
Tom Oram committed Jul 26, 2013
2 parents 14950df + e504cdc commit 4e63360
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 23 deletions.
Expand Up @@ -3,6 +3,7 @@
namespace SclZfSingleEntityController\Controller;

use SclZfSingleEntityController\Exception\InvalidArgumentException;
use SclZfSingleEntityController\Exception\NoEntityException;
use SclZfSingleEntityController\Exception\NoMapperException;
use SclZfUtilities\Mapper\GenericMapperInterface;
use Zend\Mvc\Controller\AbstractActionController as ZendActionController;
Expand Down Expand Up @@ -147,10 +148,21 @@ public function setEntity($entity)
/**
* Return the current entity being worked on.
*
* @param bool $expected If set to true this method will throw an
* exception if the entity is not set.
* @return object
* @throws NoEntityException If entity is not set and $expected is true.
*/
public function getEntity()
public function getEntity($expected = false)
{
if (!$expected) {
return $this->entity;
}

if (!is_object($this->entity)) {
throw new NoEntityException(__METHOD__);
}

return $this->entity;
}

Expand Down
Expand Up @@ -3,7 +3,6 @@
namespace SclZfSingleEntityController\Controller;

use SclZfUtilities\Mapper\GenericMapperInterface;
use SclZfSingleEntityController\Exception\NoEntityException;

/**
* A generic controller which provides add, edit, delete & view actions for a
Expand Down Expand Up @@ -117,15 +116,10 @@ public function addAction()
* Display a form to edit an entity.
*
* @return array
* @throws NoEntityException When no entity is loaded.
*/
public function editAction()
{
$entity = $this->getEntity();

if (null == $entity) {
throw new NoEntityException(__METHOD__);
}
$entity = $this->getEntity(true);

$form = $this->getFormBuilder()->createForm($entity, 'Edit');

Expand All @@ -143,15 +137,10 @@ public function editAction()
* Display an entity.
*
* @return array
* @throws NoEntityException When no entity is loaded.
*/
public function viewAction()
{
$entity = $this->getEntity();

if (null == $entity) {
throw new NoEntityException(__METHOD__);
}
$entity = $this->getEntity(true);

return array($this->viewVariable => $entity);
}
Expand All @@ -160,16 +149,11 @@ public function viewAction()
* Prompt the user to find if they want to delete an entity.
*
* @return array
* @throws NoEntityException When no entity is loaded.
* @todo Add checks that delete requests are valid.
*/
public function deleteAction()
{
$entity = $this->getEntity();

if (null == $entity) {
throw new NoEntityException(__METHOD__);
}
$entity = $this->getEntity(true);

$request = $this->getRequest();

Expand Down
Expand Up @@ -117,6 +117,7 @@ public function testSetEntityWithScalar()
/**
* When setEntity() is with a scalar an exception should be thrown.
*
* @depends testGetSetMapper
* @covers SclZfSingleEntityController\Controller\SingleEntityController::setMapper
* @expectedException SclZfSingleEntityController\Exception\InvalidArgumentException
*
Expand All @@ -131,14 +132,15 @@ public function testSetEntityWithIncorrectEntityType()
->method('getEntityName')
->will($this->returnValue('TheEntityClass'));

$this->controller->setEntity('x');
$this->controller->setEntity(new \stdClass);
}

/**
* Test the getEntity & setEntity methods.
*
* @covers SclZfSingleEntityController\Controller\SingleEntityController::setEntity
* @covers SclZfSingleEntityController\Controller\SingleEntityController::getEntity
* @depends testGetSetMapper
* @covers SclZfSingleEntityController\Controller\SingleEntityController::setEntity
* @covers SclZfSingleEntityController\Controller\SingleEntityController::getEntity
*
* @return void
*/
Expand Down Expand Up @@ -169,4 +171,65 @@ public function testSetGetEntity()
'The entity return was not the entity set.'
);
}

/**
* Test getEntity with no entity set return null.
*
* @covers SclZfSingleEntityController\Controller\SingleEntityController::getEntity
*
* @return void
*/
public function testGetEntityWithNoEntityReturnsNull()
{
$this->assertNull($this->controller->getEntity());
}

/**
* Test getEntity with $expected set to true and no entity should throw.
*
* @covers SclZfSingleEntityController\Controller\SingleEntityController::getEntity
* @expectedException SclZfSingleEntityController\Exception\NoEntityException
*
* @return void
*/
public function testGetEntityWithExpectedAndNoEntity()
{
$result = $this->controller->getEntity(true);
}

/**
* Test getEntity with $expected set to true.
*
* @depends testGetSetMapper
* @covers SclZfSingleEntityController\Controller\SingleEntityController::getEntity
*
* @return void
*/
public function testGetEntityWithExpected()
{
$entity = new \stdClass();

$this->controller->setMapper($this->mapper);

$this->mapper
->expects($this->atLeastOnce())
->method('getEntityName')
->will($this->returnValue('stdClass'));

$result = $this->controller->setEntity($entity);

$this->assertSame(
$this->controller,
$result,
'setEntity() did not return $this.'
);

$result = $this->controller->getEntity(true);

$this->assertSame(
$entity,
$result,
'The entity return was not the entity set.'
);
}
}

0 comments on commit 4e63360

Please sign in to comment.