Skip to content

Commit

Permalink
Remove use of TestCase::getMock() from "Controller" namepace tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jun 4, 2016
1 parent 8dcf70d commit d00d7fd
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 171 deletions.
167 changes: 72 additions & 95 deletions tests/TestCase/Controller/Component/AuthComponentTest.php
Expand Up @@ -20,7 +20,6 @@
use Cake\Event\EventManager;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Network\Session;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -66,7 +65,9 @@ public function setUp()
});

$request = new Request();
$response = $this->getMock('Cake\Network\Response', ['stop']);
$response = $this->getMockBuilder('Cake\Network\Response')
->setMethods(['stop'])
->getMock();

$this->Controller = new AuthTestController($request, $response);
$this->Auth = new TestAuthComponent($this->Controller->components());
Expand Down Expand Up @@ -123,13 +124,10 @@ public function testIsErrorOrTests()
*/
public function testIdentify()
{
$AuthLoginFormAuthenticate = $this->getMock(
'Cake\Controller\Component\Auth\FormAuthenticate',
['authenticate'],
[],
'',
false
);
$AuthLoginFormAuthenticate = $this->getMockBuilder('Cake\Controller\Component\Auth\FormAuthenticate')
->setMethods(['authenticate'])
->disableOriginalConstructor()
->getMock();
$this->Auth->authenticate = [
'AuthLoginForm' => [
'userModel' => 'AuthUsers'
Expand Down Expand Up @@ -231,27 +229,18 @@ public function testIsAuthorizedMissingFile()
*/
public function testIsAuthorizedDelegation()
{
$AuthMockOneAuthorize = $this->getMock(
'Cake\Controller\Component\BaseAuthorize',
['authorize'],
[],
'',
false
);
$AuthMockTwoAuthorize = $this->getMock(
'Cake\Controller\Component\Auth\BaseAuthorize',
['authorize'],
[],
'',
false
);
$AuthMockThreeAuthorize = $this->getMock(
'Cake\Controller\Component\Auth\BaseAuthorize',
['authorize'],
[],
'',
false
);
$AuthMockOneAuthorize = $this->getMockBuilder('Cake\Controller\Component\BaseAuthorize')
->setMethods(['authorize'])
->disableOriginalConstructor()
->getMock();
$AuthMockTwoAuthorize = $this->getMockBuilder('Cake\Controller\Component\BaseAuthorize')
->setMethods(['authorize'])
->disableOriginalConstructor()
->getMock();
$AuthMockThreeAuthorize = $this->getMockBuilder('Cake\Controller\Component\BaseAuthorize')
->setMethods(['authorize'])
->disableOriginalConstructor()
->getMock();

$this->Auth->setAuthorizeObject(0, $AuthMockOneAuthorize);
$this->Auth->setAuthorizeObject(1, $AuthMockTwoAuthorize);
Expand Down Expand Up @@ -282,13 +271,10 @@ public function testIsAuthorizedDelegation()
*/
public function testIsAuthorizedUsingUserInSession()
{
$AuthMockFourAuthorize = $this->getMock(
'Cake\Controller\Component\Auth\BaseAuthorize',
['authorize'],
[],
'',
false
);
$AuthMockFourAuthorize = $this->getMockBuilder('Cake\Controller\Component\BaseAuthorize')
->setMethods(['authorize'])
->disableOriginalConstructor()
->getMock();
$this->Auth->config('authorize', ['AuthMockFour']);
$this->Auth->setAuthorizeObject(0, $AuthMockFourAuthorize);

Expand Down Expand Up @@ -676,13 +662,11 @@ public function testNoLoginRedirectForAuthenticatedUser()

$this->Auth->session->write('Auth.User.id', '1');
$this->Auth->config('authenticate', ['Form']);
$this->getMock(
'Cake\Controller\Component\Auth\BaseAuthorize',
['authorize'],
[],
'NoLoginRedirectMockAuthorize',
false
);
$this->getMockBuilder('Cake\Controller\Component\BaseAuthorize')
->setMethods(['authorize'])
->disableOriginalConstructor()
->setMockClassName('NoLoginRedirectMockAuthorize')
->getMock();
$this->Auth->config('authorize', ['NoLoginRedirectMockAuthorize']);
$this->Auth->config('loginAction', ['controller' => 'auth_test', 'action' => 'login']);

Expand Down Expand Up @@ -718,11 +702,10 @@ public function testDefaultToLoginRedirect()
]);

$response = new Response();
$Controller = $this->getMock(
'Cake\Controller\Controller',
['on', 'redirect'],
[$request, $response]
);
$Controller = $this->getMockBuilder('Cake\Controller\Controller')
->setMethods(['on', 'redirect'])
->setConstructorArgs([$request, $response])
->getMock();
$event = new Event('Controller.startup', $Controller);

// Should not contain basedir when redirect is called.
Expand All @@ -742,11 +725,10 @@ public function testDefaultToLoginRedirect()
public function testRedirectToUnauthorizedRedirect()
{
$url = '/party/on';
$this->Auth->Flash = $this->getMock(
'Cake\Controller\Component\FlashComponent',
['set'],
[$this->Controller->components()]
);
$this->Auth->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['set'])
->setConstructorArgs([$this->Controller->components()])
->getMock();
$this->Auth->request = $request = new Request([
'url' => $url,
'session' => $this->Auth->session
Expand All @@ -759,11 +741,10 @@ public function testRedirectToUnauthorizedRedirect()
$this->Auth->config('unauthorizedRedirect', $expected);

$response = new Response();
$Controller = $this->getMock(
'Cake\Controller\Controller',
['on', 'redirect'],
[$request, $response]
);
$Controller = $this->getMockBuilder('Cake\Controller\Controller')
->setMethods(['on', 'redirect'])
->setConstructorArgs([$request, $response])
->getMock();

$Controller->expects($this->once())
->method('redirect')
Expand All @@ -785,11 +766,10 @@ public function testRedirectToUnauthorizedRedirect()
public function testRedirectToUnauthorizedRedirectLoginAction()
{
$url = '/party/on';
$this->Auth->Flash = $this->getMock(
'Cake\Controller\Component\FlashComponent',
['set'],
[$this->Controller->components()]
);
$this->Auth->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setMethods(['set'])
->setConstructorArgs([$this->Controller->components()])
->getMock();
$this->Auth->request = $request = new Request([
'url' => $url,
'session' => $this->Auth->session
Expand All @@ -802,11 +782,10 @@ public function testRedirectToUnauthorizedRedirectLoginAction()
$this->Auth->config('loginAction', '/users/login');

$response = new Response();
$Controller = $this->getMock(
'Cake\Controller\Controller',
['on', 'redirect'],
[$request, $response]
);
$Controller = $this->getMockBuilder('Cake\Controller\Controller')
->setMethods(['on', 'redirect'])
->setConstructorArgs([$request, $response])
->getMock();

// Uses referrer instead of loginAction.
$Controller->expects($this->once())
Expand All @@ -826,10 +805,9 @@ public function testRedirectToUnauthorizedRedirectLoginAction()
public function testRedirectToUnauthorizedRedirectSuppressedAuthError()
{
$url = '/party/on';
$this->Auth->session = $this->getMock(
'Cake\Network\Session',
['flash']
);
$this->Auth->session = $this->getMockBuilder('Cake\Network\Session')
->setMethods(['flash'])
->getMock();
$this->Auth->request = $Request = new Request($url);
$this->Auth->request->addParams(Router::parse($url));
$this->Auth->config('authorize', ['Controller']);
Expand All @@ -839,11 +817,10 @@ public function testRedirectToUnauthorizedRedirectSuppressedAuthError()
$this->Auth->config('authError', false);

$Response = new Response();
$Controller = $this->getMock(
'Cake\Controller\Controller',
['on', 'redirect'],
[$Request, $Response]
);
$Controller = $this->getMockBuilder('Cake\Controller\Controller')
->setMethods(['on', 'redirect'])
->setConstructorArgs([$Request, $Response])
->getMock();

$Controller->expects($this->once())
->method('redirect')
Expand Down Expand Up @@ -875,11 +852,10 @@ public function testForbiddenException()
$this->Auth->setUser(['username' => 'baker', 'password' => 'cake']);

$response = new Response();
$Controller = $this->getMock(
'Cake\Controller\Controller',
['on', 'redirect'],
[$request, $response]
);
$Controller = $this->getMockBuilder('Cake\Controller\Controller')
->setMethods(['on', 'redirect'])
->setConstructorArgs([$request, $response])
->getMock();

$event = new Event('Controller.startup', $Controller);
$this->Auth->startup($event);
Expand All @@ -894,7 +870,9 @@ public function testForbiddenException()
public function testNoRedirectOnLoginAction()
{
$event = new Event('Controller.startup', $this->Controller);
$controller = $this->getMock('Cake\Controller\Controller', ['redirect']);
$controller = $this->getMockBuilder('Cake\Controller\Controller')
->setMethods(['redirect'])
->getMock();
$controller->methods = ['login'];

$url = '/AuthTest/login';
Expand Down Expand Up @@ -1199,11 +1177,10 @@ public function testAfterIdentifyForStatelessAuthentication()
*/
public function testSetUser()
{
$storage = $this->getMock(
'Cake\Auth\Storage\SessionStorage',
['write'],
[$this->Auth->request, $this->Auth->response]
);
$storage = $this->getMockBuilder('Cake\Auth\Storage\SessionStorage')
->setMethods(['write'])
->setConstructorArgs([$this->Auth->request, $this->Auth->response])
->getMock();
$this->Auth->storage($storage);

$user = ['username' => 'mark', 'role' => 'admin'];
Expand Down Expand Up @@ -1243,11 +1220,9 @@ public function testGettingUserAfterSetUser()
*/
public function testFlashSettings()
{
$this->Auth->Flash = $this->getMock(
'Cake\Controller\Component\FlashComponent',
[],
[$this->Controller->components()]
);
$this->Auth->Flash = $this->getMockBuilder('Cake\Controller\Component\FlashComponent')
->setConstructorArgs([$this->Controller->components()])
->getMock();
$this->Controller->request->params['action'] = 'add';
$this->Auth->startup(new Event('Controller.startup', $this->Controller));

Expand Down Expand Up @@ -1450,7 +1425,9 @@ public function testStatelessAuthNoRedirect()
*/
public function testStatelessFollowedByStatefulAuth()
{
$this->Auth->response = $this->getMock('Cake\Network\Response', ['stop', 'statusCode', 'send']);
$this->Auth->response = $this->getMockBuilder('Cake\Network\Response')
->setMethods(['stop', 'statusCode', 'send'])
->getMock();
$event = new Event('Controller.startup', $this->Controller);
$this->Auth->authenticate = ['Basic', 'Form'];
$this->Controller->request['action'] = 'add';
Expand Down
9 changes: 4 additions & 5 deletions tests/TestCase/Controller/Component/CookieComponentTest.php
Expand Up @@ -36,11 +36,10 @@ class CookieComponentTest extends TestCase
public function setUp()
{
parent::setUp();
$controller = $this->getMock(
'Cake\Controller\Controller',
['redirect'],
[new Request(), new Response()]
);
$controller = $this->getMockBuilder('Cake\Controller\Controller')
->setMethods(['redirect'])
->setConstructorArgs([new Request(), new Response()])
->getMock();
$controller->loadComponent('Cookie');
$this->Controller = $controller;
$this->Cookie = $controller->Cookie;
Expand Down

0 comments on commit d00d7fd

Please sign in to comment.