Skip to content

Commit

Permalink
adds Event type hinting to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkingmedia committed Aug 30, 2016
1 parent 61fe5ce commit fc5e8a9
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 56 deletions.
4 changes: 2 additions & 2 deletions tests/TestCase/Controller/Component/AuthComponentTest.php
Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -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';
});
Expand All @@ -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);
Expand Down Expand Up @@ -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']);
Expand All @@ -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);

Expand All @@ -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;
});

Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Error/ExceptionRendererTest.php
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions tests/TestCase/Http/ActionDispatcherTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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([
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Http/ServerTest.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase;

use Cake\Event\Event;
use Cake\Http\Server;
use Cake\TestSuite\TestCase;
use TestApp\Http\BadResponseApplication;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -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']);
Expand Down
5 changes: 3 additions & 2 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down
17 changes: 9 additions & 8 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 14 additions & 15 deletions tests/TestCase/ORM/RulesCheckerIntegrationTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -705,9 +707,7 @@ function ($event, Entity $entity, \ArrayObject $options, $operation) {
$event->stopPropagation();

return true;
},
'Model.beforeRules'
);
});

$this->assertSame($entity, $table->save($entity));
}
Expand All @@ -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,
Expand All @@ -746,9 +747,7 @@ function ($event, Entity $entity, \ArrayObject $options, $result, $operation) {
$event->stopPropagation();

return true;
},
'Model.afterRules'
);
});

$this->assertSame($entity, $table->save($entity));
}
Expand All @@ -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));
}
Expand Down Expand Up @@ -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']);
});

Expand Down Expand Up @@ -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']);
});

Expand All @@ -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()
{
Expand Down

0 comments on commit fc5e8a9

Please sign in to comment.