diff --git a/tests/TestCase/Controller/Component/AuthComponentTest.php b/tests/TestCase/Controller/Component/AuthComponentTest.php index 029bdb5c252..f7e82ba88d4 100644 --- a/tests/TestCase/Controller/Component/AuthComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthComponentTest.php @@ -1227,8 +1227,8 @@ public function testAfterIdentifyForStatelessAuthentication() ]); $this->Auth->config('storage', 'Memory'); - EventManager::instance()->on('Auth.afterIdentify', function ($event) { - $user = $event->data[0]; + EventManager::instance()->on('Auth.afterIdentify', function (Event $event) { + $user = $event->data(0); $user['from_callback'] = true; return $user; diff --git a/tests/TestCase/Controller/ControllerTest.php b/tests/TestCase/Controller/ControllerTest.php index 9919ed18a20..52000c01359 100644 --- a/tests/TestCase/Controller/ControllerTest.php +++ b/tests/TestCase/Controller/ControllerTest.php @@ -412,7 +412,7 @@ public function testBeforeRenderCallbackChangingViewClass() { $Controller = new Controller(new Request, new Response()); - $Controller->eventManager()->on('Controller.beforeRender', function ($event) { + $Controller->eventManager()->on('Controller.beforeRender', function (Event $event) { $controller = $event->subject(); $controller->viewClass = 'Json'; }); @@ -437,9 +437,9 @@ public function testBeforeRenderEventCancelsRender() { $Controller = new Controller(new Request, new Response()); - $Controller->eventManager()->attach(function ($event) { + $Controller->eventManager()->on('Controller.beforeRender', function (Event $event) { return false; - }, 'Controller.beforeRender'); + }); $result = $Controller->render('index'); $this->assertInstanceOf('Cake\Network\Response', $result); @@ -489,9 +489,9 @@ public function testRedirectBeforeRedirectModifyingUrl() { $Controller = new Controller(null, new Response()); - $Controller->eventManager()->attach(function ($event, $url, $response) { + $Controller->eventManager()->on('Controller.beforeRedirect', function (Event $event, $url, Response $response) { $response->location('http://book.cakephp.org'); - }, 'Controller.beforeRedirect'); + }); $response = $Controller->redirect('http://cakephp.org', 301); $this->assertEquals('http://book.cakephp.org', $response->header()['Location']); @@ -510,9 +510,9 @@ public function testRedirectBeforeRedirectModifyingStatusCode() ->getMock(); $Controller = new Controller(null, $Response); - $Controller->eventManager()->attach(function ($event, $url, $response) { + $Controller->eventManager()->on('Controller.beforeRedirect', function (Event $event, $url, Response $response) { $response->statusCode(302); - }, 'Controller.beforeRedirect'); + }); $response = $Controller->redirect('http://cakephp.org', 301); @@ -528,7 +528,7 @@ public function testRedirectBeforeRedirectListenerReturnResponse() $Controller = new Controller(null, $Response); $newResponse = new Response; - $Controller->eventManager()->on('Controller.beforeRedirect', function ($event, $url, $response) use ($newResponse) { + $Controller->eventManager()->on('Controller.beforeRedirect', function (Event $event, $url, Response $response) use ($newResponse) { return $newResponse; }); diff --git a/tests/TestCase/Error/ExceptionRendererTest.php b/tests/TestCase/Error/ExceptionRendererTest.php index 3fd3b532013..71235c07861 100644 --- a/tests/TestCase/Error/ExceptionRendererTest.php +++ b/tests/TestCase/Error/ExceptionRendererTest.php @@ -859,7 +859,7 @@ public function testRenderWithNoRequest() public function testRenderShutdownEvents() { $fired = []; - $listener = function ($event) use (&$fired) { + $listener = function (Event $event) use (&$fired) { $fired[] = $event->name(); }; $events = EventManager::instance(); @@ -882,7 +882,7 @@ public function testRenderShutdownEvents() public function testSubclassTriggerShutdownEvents() { $fired = []; - $listener = function ($event) use (&$fired) { + $listener = function (Event $event) use (&$fired) { $fired[] = $event->name(); }; $events = EventManager::instance(); diff --git a/tests/TestCase/Http/ActionDispatcherTest.php b/tests/TestCase/Http/ActionDispatcherTest.php index 80ba212e45b..bbe385f4010 100644 --- a/tests/TestCase/Http/ActionDispatcherTest.php +++ b/tests/TestCase/Http/ActionDispatcherTest.php @@ -15,6 +15,7 @@ namespace Cake\Test\TestCase\Http; use Cake\Core\Configure; +use Cake\Event\Event; use Cake\Http\ActionDispatcher; use Cake\Network\Request; use Cake\Network\Response; @@ -141,8 +142,8 @@ public function testDispatchAfterDispatchEventModifyResponse() ->getMock(); $filter->expects($this->once()) ->method('afterDispatch') - ->will($this->returnCallback(function ($event) { - $event->data['response']->body('Filter body'); + ->will($this->returnCallback(function (Event $event) { + $event->data('response')->body('Filter body'); })); $req = new Request([ diff --git a/tests/TestCase/Http/ServerTest.php b/tests/TestCase/Http/ServerTest.php index b2c76026128..8c445e7aca3 100644 --- a/tests/TestCase/Http/ServerTest.php +++ b/tests/TestCase/Http/ServerTest.php @@ -14,6 +14,7 @@ */ namespace Cake\Test\TestCase; +use Cake\Event\Event; use Cake\Http\Server; use Cake\TestSuite\TestCase; use TestApp\Http\BadResponseApplication; @@ -183,7 +184,7 @@ public function testBuildMiddlewareEvent() $server = new Server($app); $this->called = false; - $server->eventManager()->on('Server.buildMiddleware', function ($event, $middleware) { + $server->eventManager()->on('Server.buildMiddleware', function (Event $event, $middleware) { $this->assertInstanceOf('Cake\Http\MiddlewareQueue', $middleware); $middleware->add(function ($req, $res, $next) { $this->called = true; diff --git a/tests/TestCase/ORM/Association/BelongsToManyTest.php b/tests/TestCase/ORM/Association/BelongsToManyTest.php index 73654e118bf..00133842dd0 100644 --- a/tests/TestCase/ORM/Association/BelongsToManyTest.php +++ b/tests/TestCase/ORM/Association/BelongsToManyTest.php @@ -690,7 +690,7 @@ public function testReplaceLinkFailingDomainRules() { $articles = TableRegistry::get('Articles'); $tags = TableRegistry::get('Tags'); - $tags->eventManager()->on('Model.buildRules', function ($event, $rules) { + $tags->eventManager()->on('Model.buildRules', function (Event $event, $rules) { $rules->add(function () { return false; }, 'rule', ['errorField' => 'name', 'message' => 'Bad data']); diff --git a/tests/TestCase/ORM/MarshallerTest.php b/tests/TestCase/ORM/MarshallerTest.php index 35427c0c3b4..738f1174af0 100644 --- a/tests/TestCase/ORM/MarshallerTest.php +++ b/tests/TestCase/ORM/MarshallerTest.php @@ -15,6 +15,7 @@ namespace Cake\Test\TestCase\ORM; use Cake\Database\Expression\IdentifierExpression; +use Cake\Event\Event; use Cake\I18n\Time; use Cake\ORM\Entity; use Cake\ORM\Exception\MissingAssociationException; @@ -1729,7 +1730,7 @@ public function testMergeBelongsToManyFromArrayWithConditions() ]); $this->articles->Tags->eventManager() - ->on('Model.beforeFind', function ($event, $query) use (&$called) { + ->on('Model.beforeFind', function (Event $event, $query) use (&$called) { $called = true; return $query->where(['Tags.id >=' => 1]); @@ -2328,7 +2329,7 @@ public function testMergeManyExistingQueryAliases() ['id' => 1, 'comment' => 'Changed 1', 'user_id' => 1], ['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2], ]; - $this->comments->eventManager()->on('Model.beforeFind', function ($event, $query) { + $this->comments->eventManager()->on('Model.beforeFind', function (Event $event, $query) { return $query->contain(['Articles']); }); $marshall = new Marshaller($this->comments); diff --git a/tests/TestCase/ORM/QueryRegressionTest.php b/tests/TestCase/ORM/QueryRegressionTest.php index de65b4a64ed..a7862f822ea 100644 --- a/tests/TestCase/ORM/QueryRegressionTest.php +++ b/tests/TestCase/ORM/QueryRegressionTest.php @@ -15,6 +15,7 @@ namespace Cake\Test\TestCase\ORM; use Cake\Core\Plugin; +use Cake\Event\Event; use Cake\I18n\Time; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; @@ -327,9 +328,9 @@ public function testSaveWithCallbacks() $articles = TableRegistry::get('Articles'); $articles->belongsTo('Authors'); - $articles->eventManager()->attach(function ($event, $query) { + $articles->eventManager()->on('Model.beforeFind', function (Event $event, $query) { return $query->contain('Authors'); - }, 'Model.beforeFind'); + }); $article = $articles->newEntity(); $article->title = 'Foo'; diff --git a/tests/TestCase/ORM/QueryTest.php b/tests/TestCase/ORM/QueryTest.php index f877ba9c105..0fe5e990712 100644 --- a/tests/TestCase/ORM/QueryTest.php +++ b/tests/TestCase/ORM/QueryTest.php @@ -20,6 +20,7 @@ use Cake\Database\TypeMap; use Cake\Database\ValueBinder; use Cake\Datasource\ConnectionManager; +use Cake\Event\Event; use Cake\I18n\Time; use Cake\ORM\Query; use Cake\ORM\ResultSet; @@ -1309,11 +1310,11 @@ public function testFormatResultsBelongsToMany() $articlesTags ->eventManager() - ->attach(function ($event, $query) { + ->on('Model.beforeFind', function (Event $event, $query) { $query->formatResults(function ($results) { return $results; }); - }, 'Model.beforeFind'); + }); $query = new Query($this->connection, $table); @@ -1601,11 +1602,11 @@ public function testCountBeforeFind() $table = TableRegistry::get('Articles'); $table->hasMany('Comments'); $table->eventManager() - ->attach(function ($event, $query) { + ->on('Model.beforeFind', function (Event $event, $query) { $query ->limit(1) ->order(['Articles.title' => 'DESC']); - }, 'Model.beforeFind'); + }); $query = $table->find(); $result = $query->count(); @@ -1622,11 +1623,11 @@ public function testBeforeFindCalledOnce() $callCount = 0; $table = TableRegistry::get('Articles'); $table->eventManager() - ->attach(function ($event, $query) use (&$callCount) { + ->on('Model.beforeFind', function (Event $event, $query) use (&$callCount) { $valueBinder = new ValueBinder(); $query->sql($valueBinder); $callCount++; - }, 'Model.beforeFind'); + }); $query = $table->find(); $valueBinder = new ValueBinder(); @@ -2741,11 +2742,11 @@ public function testCleanCopyBeforeFind() $table = TableRegistry::get('Articles'); $table->hasMany('Comments'); $table->eventManager() - ->attach(function ($event, $query) { + ->on('Model.beforeFind', function (Event $event, $query) { $query ->limit(5) ->order(['Articles.title' => 'DESC']); - }, 'Model.beforeFind'); + }); $query = $table->find(); $query->offset(10) diff --git a/tests/TestCase/ORM/RulesCheckerIntegrationTest.php b/tests/TestCase/ORM/RulesCheckerIntegrationTest.php index 581d5324240..c9baa597d25 100644 --- a/tests/TestCase/ORM/RulesCheckerIntegrationTest.php +++ b/tests/TestCase/ORM/RulesCheckerIntegrationTest.php @@ -14,6 +14,7 @@ */ namespace Cake\Test\TestCase\ORM; +use Cake\Event\Event; use Cake\ORM\Entity; use Cake\ORM\RulesChecker; use Cake\ORM\TableRegistry; @@ -689,8 +690,9 @@ public function testUseBeforeRules() $rules = $table->rulesChecker(); $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope')); - $table->eventManager()->attach( - function ($event, Entity $entity, \ArrayObject $options, $operation) { + $table->eventManager()->on( + 'Model.beforeRules', + function (Event $event, Entity $entity, \ArrayObject $options, $operation) { $this->assertEquals( [ 'atomic' => true, @@ -705,9 +707,7 @@ function ($event, Entity $entity, \ArrayObject $options, $operation) { $event->stopPropagation(); return true; - }, - 'Model.beforeRules' - ); + }); $this->assertSame($entity, $table->save($entity)); } @@ -729,8 +729,9 @@ public function testUseAfterRules() $rules = $table->rulesChecker(); $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope')); - $table->eventManager()->attach( - function ($event, Entity $entity, \ArrayObject $options, $result, $operation) { + $table->eventManager()->on( + 'Model.afterRules', + function (Event $event, Entity $entity, \ArrayObject $options, $result, $operation) { $this->assertEquals( [ 'atomic' => true, @@ -746,9 +747,7 @@ function ($event, Entity $entity, \ArrayObject $options, $result, $operation) { $event->stopPropagation(); return true; - }, - 'Model.afterRules' - ); + }); $this->assertSame($entity, $table->save($entity)); } @@ -767,9 +766,9 @@ public function testUseBuildRulesEvent() ]); $table = TableRegistry::get('Articles'); - $table->eventManager()->attach(function ($event, $rules) { + $table->eventManager()->on('Model.buildRules', function (Event $event, RulesChecker $rules) { $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope')); - }, 'Model.buildRules'); + }); $this->assertFalse($table->save($entity)); } @@ -812,7 +811,7 @@ public function testIsUniqueAliasPrefix() $rules = $table->rulesChecker(); $rules->add($rules->isUnique(['author_id'])); - $table->Authors->eventManager()->on('Model.beforeFind', function ($event, $query) { + $table->Authors->eventManager()->on('Model.beforeFind', function (Event $event, $query) { $query->leftJoin(['a2' => 'authors']); }); @@ -858,7 +857,7 @@ public function testExistsInAliasPrefix() $rules = $table->rulesChecker(); $rules->add($rules->existsIn('author_id', 'Authors')); - $table->Authors->eventManager()->on('Model.beforeFind', function ($event, $query) { + $table->Authors->eventManager()->on('Model.beforeFind', function (Event $event, $query) { $query->leftJoin(['a2' => 'authors']); }); @@ -869,7 +868,7 @@ public function testExistsInAliasPrefix() /** * Tests that using an array in existsIn() sets the error message correctly * - * @return + * @return void */ public function testExistsInErrorWithArrayField() { diff --git a/tests/TestCase/ORM/TableTest.php b/tests/TestCase/ORM/TableTest.php index 3226a6628e1..0f23e89aeaf 100644 --- a/tests/TestCase/ORM/TableTest.php +++ b/tests/TestCase/ORM/TableTest.php @@ -26,9 +26,9 @@ use Cake\Event\Event; use Cake\Event\EventManager; use Cake\I18n\Time; -use Cake\ORM\AssociationCollection; use Cake\ORM\Association\BelongsToMany; use Cake\ORM\Association\HasMany; +use Cake\ORM\AssociationCollection; use Cake\ORM\Entity; use Cake\ORM\Query; use Cake\ORM\RulesChecker; @@ -475,7 +475,7 @@ public function testFindBeforeFindEventMutateQuery() ]); $table->eventManager()->on( 'Model.beforeFind', - function ($event, $query, $options) { + function (Event $event, $query, $options) { $query->limit(1); } ); @@ -499,7 +499,7 @@ public function testFindBeforeFindEventOverrideReturn() $expected = ['One', 'Two', 'Three']; $table->eventManager()->on( 'Model.beforeFind', - function ($event, $query, $options) use ($expected) { + function (Event $event, $query, $options) use ($expected) { $query->setResult($expected); $event->stopPropagation(); } @@ -2661,7 +2661,7 @@ public function testBeforeSaveGetsCorrectPersistance() ]); $table = TableRegistry::get('users'); $called = false; - $listener = function ($event, $entity) use (&$called) { + $listener = function (Event $event, $entity) use (&$called) { $this->assertFalse($entity->isNew()); $called = true; }; @@ -3154,7 +3154,7 @@ public function testDeleteBeforeDeleteAbort() $mock = $this->getMockBuilder('Cake\Event\EventManager')->getMock(); $mock->expects($this->at(2)) ->method('dispatch') - ->will($this->returnCallback(function ($event) { + ->will($this->returnCallback(function (Event $event) { $event->stopPropagation(); })); @@ -3177,9 +3177,9 @@ public function testDeleteBeforeDeleteReturnResult() $mock = $this->getMockBuilder('Cake\Event\EventManager')->getMock(); $mock->expects($this->at(2)) ->method('dispatch') - ->will($this->returnCallback(function ($event) { + ->will($this->returnCallback(function (Event $event) { $event->stopPropagation(); - $event->result = 'got stopped'; + $event->setResult('got stopped'); })); $table = TableRegistry::get('users', ['eventManager' => $mock]); @@ -5649,14 +5649,14 @@ public function testFindOrCreateNoTransaction() public function testInitializeEvent() { $count = 0; - $cb = function ($event) use (&$count) { + $cb = function (Event $event) use (&$count) { $count++; }; EventManager::instance()->on('Model.initialize', $cb); $articles = TableRegistry::get('Articles'); $this->assertEquals(1, $count, 'Callback should be called'); - EventManager::instance()->detach($cb, 'Model.initialize'); + EventManager::instance()->off('Model.initialize', $cb); } /** @@ -5682,7 +5682,7 @@ public function testHasFinder() public function testBuildValidatorEvent() { $count = 0; - $cb = function ($event) use (&$count) { + $cb = function (Event $event) use (&$count) { $count++; }; EventManager::instance()->on('Model.buildValidator', $cb); @@ -5949,7 +5949,7 @@ public function testSaveHasManyNoWasteSave() $counter = 0; $userTable->Comments ->eventManager() - ->on('Model.afterSave', function ($event, $entity) use (&$counter) { + ->on('Model.afterSave', function (Event $event, $entity) use (&$counter) { if ($entity->dirty()) { $counter++; } @@ -5986,7 +5986,7 @@ public function testSaveBelongsToManyNoWasteSave() $counter = 0; $table->Tags->junction() ->eventManager() - ->on('Model.afterSave', function ($event, $entity) use (&$counter) { + ->on('Model.afterSave', function (Event $event, $entity) use (&$counter) { if ($entity->dirty()) { $counter++; } diff --git a/tests/TestCase/View/ViewTest.php b/tests/TestCase/View/ViewTest.php index 206054b8c39..fd7dbd6ecd2 100644 --- a/tests/TestCase/View/ViewTest.php +++ b/tests/TestCase/View/ViewTest.php @@ -923,7 +923,7 @@ public function testElementInexistent3() public function testElementCallbacks() { $count = 0; - $callback = function ($event, $file) use (&$count) { + $callback = function (Event $event, $file) use (&$count) { $count++; }; $events = $this->View->eventManager();