diff --git a/src/ORM/Table.php b/src/ORM/Table.php index 86a179c8161..6d6329fd2ee 100644 --- a/src/ORM/Table.php +++ b/src/ORM/Table.php @@ -230,6 +230,7 @@ public function __construct(array $config = []) { $this->initialize($config); $this->_eventManager->attach($this); + $this->dispatchEvent('Model.initialize'); } /** diff --git a/tests/TestCase/ORM/TableTest.php b/tests/TestCase/ORM/TableTest.php index 0f0971d1ffd..5945dbe1343 100644 --- a/tests/TestCase/ORM/TableTest.php +++ b/tests/TestCase/ORM/TableTest.php @@ -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; @@ -81,6 +82,11 @@ public function setUp() { ]); } +/** + * teardown method + * + * @return void + */ public function tearDown() { parent::tearDown(); TableRegistry::clear(); @@ -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'), @@ -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'), @@ -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(); @@ -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(); @@ -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'); + } + }