Skip to content

Commit

Permalink
Add Model.initialize event.
Browse files Browse the repository at this point in the history
This event follows the same pattern as Controller.initialize where it is
fired *after* the initialize() method and after other subscribers have
been attached. The initialize() method is *not* an event subscriber as
it is intended to be a post-constructor hook method like
Controller::initialize().

This will likely result in Component::initialize() being modified to be
conformant. There have been a number of reported issues in the past
around Component::initialize() not being called. It would be nice to put
those issues to rest and have consistency across the framework.
  • Loading branch information
markstory committed Oct 6, 2014
1 parent 8be7de7 commit c71f919
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/ORM/Table.php
Expand Up @@ -230,6 +230,7 @@ public function __construct(array $config = []) {

$this->initialize($config);
$this->_eventManager->attach($this);
$this->dispatchEvent('Model.initialize');
}

/**
Expand Down
32 changes: 29 additions & 3 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Database\Expression\QueryExpression;
use Cake\Database\TypeMap;
use Cake\Datasource\ConnectionManager;
use Cake\Event\EventManager;
use Cake\I18n\Time;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
Expand Down Expand Up @@ -81,6 +82,11 @@ public function setUp() {
]);
}

/**
* teardown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
TableRegistry::clear();
Expand Down Expand Up @@ -1902,6 +1908,9 @@ public function testDeleteCallbacks() {
->method('attach');

$mock->expects($this->at(1))
->method('dispatch');

$mock->expects($this->at(2))
->method('dispatch')
->with($this->logicalAnd(
$this->attributeEqualTo('_name', 'Model.beforeDelete'),
Expand All @@ -1911,7 +1920,7 @@ public function testDeleteCallbacks() {
)
));

$mock->expects($this->at(2))
$mock->expects($this->at(3))
->method('dispatch')
->with($this->logicalAnd(
$this->attributeEqualTo('_name', 'Model.afterDelete'),
Expand All @@ -1936,7 +1945,7 @@ public function testDeleteBeforeDeleteAbort() {
$options = new \ArrayObject(['atomic' => true, 'cascade' => true]);

$mock = $this->getMock('Cake\Event\EventManager');
$mock->expects($this->once())
$mock->expects($this->at(2))
->method('dispatch')
->will($this->returnCallback(function ($event) {
$event->stopPropagation();
Expand All @@ -1958,7 +1967,7 @@ public function testDeleteBeforeDeleteReturnResult() {
$options = new \ArrayObject(['atomic' => true, 'cascade' => true]);

$mock = $this->getMock('Cake\Event\EventManager');
$mock->expects($this->once())
$mock->expects($this->at(2))
->method('dispatch')
->will($this->returnCallback(function ($event) {
$event->stopPropagation();
Expand Down Expand Up @@ -3589,4 +3598,21 @@ function ($article) {
$this->assertEquals(2, $article->author_id);
}

/**
* Test that creating a table fires the initialize event.
*
* @return void
*/
public function testInitializeEvent() {
$count = 0;
$cb = function ($event) use (&$count){
$count++;
};
EventManager::instance()->attach($cb, 'Model.initialize');
$articles = TableRegistry::get('Articles');

$this->assertEquals(1, $count, 'Callback should be called');
EventManager::instance()->detach($cb, 'Model.initialize');
}

}

0 comments on commit c71f919

Please sign in to comment.