Skip to content

Commit

Permalink
Merge pull request #21 from UseMuffin/trash-field
Browse files Browse the repository at this point in the history
Don't introspect schmema when behavior instance is created.
  • Loading branch information
ADmad authored Jun 20, 2017
2 parents 96c8154 + 5a58ded commit 1606bd5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/Model/Behavior/TrashBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,13 @@ class TrashBehavior extends Behavior
];

/**
* Constructor
* Initialize the behavior.
*
* Merges config with the default and store in the config property
*
* @param \Cake\ORM\Table $table The table this behavior is attached to.
* @param array $config The config for this behavior.
* @return void
*/
public function __construct(Table $table, array $config = [])
public function initialize(array $config)
{
$columns = $table->schema()->columns();
foreach (['deleted', 'trashed'] as $name) {
if (in_array($name, $columns, true)) {
$this->_defaultConfig['field'] = $name;
break;
}
}

if (empty($this->_defaultConfig['field']) &&
$field = Configure::read('Muffin/Trash.field')
) {
$this->_defaultConfig['field'] = $field;
}

parent::__construct($table, $config);

if (!empty($config['events'])) {
$this->config('events', $config['events'], false);
}
Expand Down Expand Up @@ -306,6 +288,26 @@ public function getTrashField($aliased = true)
{
$field = $this->config('field');

if (empty($field)) {
$columns = $this->_table->schema()->columns();
foreach (['deleted', 'trashed'] as $name) {
if (in_array($name, $columns, true)) {
$field = $name;
break;
}
}

if (empty($field)) {
$field = Configure::read('Muffin/Trash.field');
}

if (empty($field)) {
throw new RuntimeException('TrashBehavior: "field" config needs to be provided.');
}

$this->config('field', $field);
}

if ($aliased) {
return $this->_table->aliasField($field);
}
Expand Down
43 changes: 43 additions & 0 deletions tests/TestCase/Model/Behavior/TrashBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Muffin\Trash\Test\TestCase\Model\Behavior;

use Cake\Core\Configure;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -346,6 +347,48 @@ public function testCascadingUntrash()
$this->assertNotInstanceOf('Cake\I18n\Time', $article->comments[0]->trashed);
}

/**
* Test that getTrashField() throws exception if "field" is not specified
* and cannot be introspected.
*
* @expectedException RuntimeException
* @return void
*/
public function testGetTrashFieldException()
{
$trash = new TrashBehavior($this->Users);
$trash->getTrashField();
}

/**
* Test that getTrashField() uses configured value
*
* @return void
*/
public function testGetTrashFieldUsesConfiguredValue()
{
$trash = new TrashBehavior($this->Users, ['field' => 'deleted']);
$this->assertEquals('Users.deleted', $trash->getTrashField());

Configure::write('Muffin/Trash.field', 'trashed');
$trash = new TrashBehavior($this->Users);
$this->assertEquals('Users.trashed', $trash->getTrashField());
}

/**
* Test that getTrashField() defaults to deleted or trashed
* when found in schema and not specified
*
* @return void
*/
public function testGetTrashFieldSchemaIntrospection()
{
$this->assertEquals(
'Articles.trashed',
$this->Articles->behaviors()->get('Trash')->getTrashField()
);
}

/**
* Test the implementedEvents method.
*
Expand Down

0 comments on commit 1606bd5

Please sign in to comment.