Skip to content

Commit

Permalink
[imp] new $entity->editorId() method for entities with editor
Browse files Browse the repository at this point in the history
  • Loading branch information
phproberto committed Aug 16, 2018
1 parent 5d5786a commit e77f8e1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
21 changes: 18 additions & 3 deletions extensions/libraries/joomla_entity/src/Users/Traits/HasEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,31 @@ public function editor($reload = false)
return $this->editor;
}

/**
* Retrieve the associated editor ID.
*
* @return integer
*
* @since __DEPLOY_VERSION__
*/
public function editorId()
{
if (!$this->has($this->columnAlias(Column::EDITOR)))
{
return 0;
}

return (int) $this->get($this->columnAlias(Column::EDITOR));
}

/**
* Check if this entity has an associated editor.
*
* @return boolean
*/
public function hasEditor()
{
$editorId = (int) $this->get($this->columnAlias(Column::EDITOR));

return !empty($editorId);
return 0 !== $this->editorId();
}

/**
Expand Down
55 changes: 53 additions & 2 deletions tests/Unit/Users/Traits/HasEditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,58 @@ class HasEditorTest extends \PHPUnit\Framework\TestCase
*/
const EDITOR_COLUMN = 'modified_by';

/**
* @test
*
* @return void
*/
public function editorIdReturnsZeroForMissingEditorColumn()
{
$class = $this->getMockBuilder(EntityWithAuthorAndEditor::class)
->disableOriginalConstructor()
->setMethods(array('columnAlias'))
->getMock();

$class->method('columnAlias')
->willReturn(static::EDITOR_COLUMN);

$this->assertSame(0, $class->editorId());
}

/**
* @test
*
* @return void
*/
public function editorIdReturnsCorrectId()
{
$entity = $this->getMockBuilder(EntityWithAuthorAndEditor::class)
->disableOriginalConstructor()
->setMethods(array('columnAlias'))
->getMock();

$entity->method('columnAlias')
->willReturn(static::EDITOR_COLUMN);

$entity->bind(
[
'id' => 99,
static::EDITOR_COLUMN => 34
]
);

$this->assertSame(34, $entity->editorId());

$entity->bind(
[
'id' => 99,
static::EDITOR_COLUMN => 333
]
);

$this->assertSame(333, $entity->editorId());
}

/**
* editor calls loadEditor.
*
Expand Down Expand Up @@ -106,8 +158,7 @@ public function testHasEditorReturnsCorrectValue()
->setMethods(array('columnAlias'))
->getMock();

$class->expects($this->once())
->method('columnAlias')
$class->method('columnAlias')
->willReturn(static::EDITOR_COLUMN);

$reflection = new \ReflectionClass($class);
Expand Down

0 comments on commit e77f8e1

Please sign in to comment.