Skip to content
Permalink
Browse files

adds Event type hinting to unit tests

  • Loading branch information...
thinkingmedia
thinkingmedia committed Aug 30, 2016
1 parent 61fe5ce commit fc5e8a93cbdfc9c487e8158d75328844a3a436f0
@@ -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;
@@ -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;
});
@@ -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();
@@ -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([
@@ -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;
@@ -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']);
@@ -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);
@@ -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';
@@ -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)
@@ -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()
{
Oops, something went wrong.

0 comments on commit fc5e8a9

Please sign in to comment.
You can’t perform that action at this time.