Skip to content

Commit 1606bd5

Browse files
authored
Merge pull request #21 from UseMuffin/trash-field
Don't introspect schmema when behavior instance is created.
2 parents 96c8154 + 5a58ded commit 1606bd5

2 files changed

Lines changed: 66 additions & 21 deletions

File tree

src/Model/Behavior/TrashBehavior.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,13 @@ class TrashBehavior extends Behavior
4040
];
4141

4242
/**
43-
* Constructor
43+
* Initialize the behavior.
4444
*
45-
* Merges config with the default and store in the config property
46-
*
47-
* @param \Cake\ORM\Table $table The table this behavior is attached to.
4845
* @param array $config The config for this behavior.
46+
* @return void
4947
*/
50-
public function __construct(Table $table, array $config = [])
48+
public function initialize(array $config)
5149
{
52-
$columns = $table->schema()->columns();
53-
foreach (['deleted', 'trashed'] as $name) {
54-
if (in_array($name, $columns, true)) {
55-
$this->_defaultConfig['field'] = $name;
56-
break;
57-
}
58-
}
59-
60-
if (empty($this->_defaultConfig['field']) &&
61-
$field = Configure::read('Muffin/Trash.field')
62-
) {
63-
$this->_defaultConfig['field'] = $field;
64-
}
65-
66-
parent::__construct($table, $config);
67-
6850
if (!empty($config['events'])) {
6951
$this->config('events', $config['events'], false);
7052
}
@@ -306,6 +288,26 @@ public function getTrashField($aliased = true)
306288
{
307289
$field = $this->config('field');
308290

291+
if (empty($field)) {
292+
$columns = $this->_table->schema()->columns();
293+
foreach (['deleted', 'trashed'] as $name) {
294+
if (in_array($name, $columns, true)) {
295+
$field = $name;
296+
break;
297+
}
298+
}
299+
300+
if (empty($field)) {
301+
$field = Configure::read('Muffin/Trash.field');
302+
}
303+
304+
if (empty($field)) {
305+
throw new RuntimeException('TrashBehavior: "field" config needs to be provided.');
306+
}
307+
308+
$this->config('field', $field);
309+
}
310+
309311
if ($aliased) {
310312
return $this->_table->aliasField($field);
311313
}

tests/TestCase/Model/Behavior/TrashBehaviorTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Muffin\Trash\Test\TestCase\Model\Behavior;
33

4+
use Cake\Core\Configure;
45
use Cake\ORM\Entity;
56
use Cake\ORM\TableRegistry;
67
use Cake\TestSuite\TestCase;
@@ -346,6 +347,48 @@ public function testCascadingUntrash()
346347
$this->assertNotInstanceOf('Cake\I18n\Time', $article->comments[0]->trashed);
347348
}
348349

350+
/**
351+
* Test that getTrashField() throws exception if "field" is not specified
352+
* and cannot be introspected.
353+
*
354+
* @expectedException RuntimeException
355+
* @return void
356+
*/
357+
public function testGetTrashFieldException()
358+
{
359+
$trash = new TrashBehavior($this->Users);
360+
$trash->getTrashField();
361+
}
362+
363+
/**
364+
* Test that getTrashField() uses configured value
365+
*
366+
* @return void
367+
*/
368+
public function testGetTrashFieldUsesConfiguredValue()
369+
{
370+
$trash = new TrashBehavior($this->Users, ['field' => 'deleted']);
371+
$this->assertEquals('Users.deleted', $trash->getTrashField());
372+
373+
Configure::write('Muffin/Trash.field', 'trashed');
374+
$trash = new TrashBehavior($this->Users);
375+
$this->assertEquals('Users.trashed', $trash->getTrashField());
376+
}
377+
378+
/**
379+
* Test that getTrashField() defaults to deleted or trashed
380+
* when found in schema and not specified
381+
*
382+
* @return void
383+
*/
384+
public function testGetTrashFieldSchemaIntrospection()
385+
{
386+
$this->assertEquals(
387+
'Articles.trashed',
388+
$this->Articles->behaviors()->get('Trash')->getTrashField()
389+
);
390+
}
391+
349392
/**
350393
* Test the implementedEvents method.
351394
*

0 commit comments

Comments
 (0)