From 9a7454fbb69977a27d2b94154a8dadfa14024196 Mon Sep 17 00:00:00 2001 From: kanellov Date: Wed, 11 Mar 2015 14:26:40 +0200 Subject: [PATCH 1/5] Fix php 5.3 incompatibility in tests --- test/Adapter/CallbackTest.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/test/Adapter/CallbackTest.php b/test/Adapter/CallbackTest.php index 4dfccc0..de6d503 100644 --- a/test/Adapter/CallbackTest.php +++ b/test/Adapter/CallbackTest.php @@ -19,9 +19,9 @@ class CallbackTest extends \PHPUnit_Framework_TestCase { /** - * Database table authentication adapter + * Callback authentication adapter * - * @var \Zend\Authentication\Adapter\DbTable + * @var \Zend\Authentication\Adapter\Callback */ protected $_adapter = null; @@ -89,15 +89,16 @@ public function testAuthenticateThrowsException() */ public function testAuthenticateProvidesCallbackWithIdentityAndCredentials() { - $adapter = $this->_adapter; $this->_adapter->setIdentity('testIdentity'); $this->_adapter->setCredential('testCredential'); - $callback = function ($identity, $credential) use ($adapter) { - $this->assertEquals($identity, $adapter->getIdentity()); - $this->assertEquals($credential, $adapter->getCredential()); + $that = $this; + $callback = function ($identity, $credential) use ($that) { + $adapter = $that->_adapter; + $that->assertEquals($identity, $adapter->getIdentity()); + $that->assertEquals($credential, $adapter->getCredential()); }; - $adapter->setCallback($callback); - $adapter->authenticate(); + $this->_adapter->setCallback($callback); + $this->_adapter->authenticate(); } /** @@ -122,17 +123,18 @@ public function testAuthenticateResultIfCallbackThrows() */ public function testAuthenticateResultIfCallbackReturnsFalsy() { - $adapter = $this->_adapter; + $that = $this; $falsyValues = array(false, null, '', '0', array(), 0, 0.0); - array_map(function ($falsy) use ($adapter) { + array_map(function ($falsy) use ($that) { $callback = function () use ($falsy) { return $falsy; }; + $adapter = $that->_adapter; $adapter->setCallback($callback); $result = $adapter->authenticate(); - $this->assertFalse($result->isValid()); - $this->assertEquals(Authentication\Result::FAILURE, $result->getCode()); - $this->assertEquals(array('Authentication failure'), $result->getMessages()); + $that->assertFalse($result->isValid()); + $that->assertEquals(Authentication\Result::FAILURE, $result->getCode()); + $that->assertEquals(array('Authentication failure'), $result->getMessages()); }, $falsyValues); } From 6bc88ccf93b4ed82a009f93a6b466e2cee820fc7 Mon Sep 17 00:00:00 2001 From: kanellov Date: Wed, 11 Mar 2015 14:27:34 +0200 Subject: [PATCH 2/5] Added docblock for protected variable --- src/Adapter/Callback.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Adapter/Callback.php b/src/Adapter/Callback.php index 8ccf4d4..87ed011 100644 --- a/src/Adapter/Callback.php +++ b/src/Adapter/Callback.php @@ -20,6 +20,9 @@ */ class Callback extends AbstractAdapter { + /** + * @var callable + */ protected $callback; /** From cfb51c72c62e65542c01a584c7012a6b3a088cc5 Mon Sep 17 00:00:00 2001 From: kanellov Date: Wed, 11 Mar 2015 14:31:25 +0200 Subject: [PATCH 3/5] Updated docblock --- src/Adapter/Callback.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Adapter/Callback.php b/src/Adapter/Callback.php index 87ed011..4ca52c3 100644 --- a/src/Adapter/Callback.php +++ b/src/Adapter/Callback.php @@ -40,6 +40,7 @@ public function __construct($callback = null) /** * Authenticate * @return \Zend\Authentication\Result the authentication result + * @throws RuntimeException */ public function authenticate() { From 81682ab2463761c69fa7d5dd1a851687a6f941b9 Mon Sep 17 00:00:00 2001 From: kanellov Date: Wed, 11 Mar 2015 14:37:30 +0200 Subject: [PATCH 4/5] Fix php 5.3 incompatibility in unit tests --- test/Adapter/CallbackTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/Adapter/CallbackTest.php b/test/Adapter/CallbackTest.php index de6d503..45d194d 100644 --- a/test/Adapter/CallbackTest.php +++ b/test/Adapter/CallbackTest.php @@ -89,11 +89,11 @@ public function testAuthenticateThrowsException() */ public function testAuthenticateProvidesCallbackWithIdentityAndCredentials() { - $this->_adapter->setIdentity('testIdentity'); - $this->_adapter->setCredential('testCredential'); + $adapter = $this->_adapter; + $adapter->setIdentity('testIdentity'); + $adapter->setCredential('testCredential'); $that = $this; - $callback = function ($identity, $credential) use ($that) { - $adapter = $that->_adapter; + $callback = function ($identity, $credential) use ($that, $adapter) { $that->assertEquals($identity, $adapter->getIdentity()); $that->assertEquals($credential, $adapter->getCredential()); }; @@ -123,13 +123,13 @@ public function testAuthenticateResultIfCallbackThrows() */ public function testAuthenticateResultIfCallbackReturnsFalsy() { - $that = $this; + $that = $this; + $adapter = $this->_adapter; $falsyValues = array(false, null, '', '0', array(), 0, 0.0); - array_map(function ($falsy) use ($that) { + array_map(function ($falsy) use ($that, $adapter) { $callback = function () use ($falsy) { return $falsy; }; - $adapter = $that->_adapter; $adapter->setCallback($callback); $result = $adapter->authenticate(); $that->assertFalse($result->isValid()); From 2bec51d7518804f446ddc4b5d3f83b5a8faf0050 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 17 Mar 2015 15:46:27 -0500 Subject: [PATCH 5/5] Review for consistency --- src/Adapter/Callback.php | 37 +++++++++++---------- test/Adapter/CallbackTest.php | 60 +++++++++++++++++------------------ 2 files changed, 47 insertions(+), 50 deletions(-) diff --git a/src/Adapter/Callback.php b/src/Adapter/Callback.php index 4ca52c3..dd39956 100644 --- a/src/Adapter/Callback.php +++ b/src/Adapter/Callback.php @@ -9,13 +9,15 @@ namespace Zend\Authentication\Adapter; +use Exception; use Zend\Authentication\Exception\InvalidArgumentException; use Zend\Authentication\Exception\RuntimeException; use Zend\Authentication\Result; /** * Authentication Adapter authenticates using callback function. - * The Callback function must return the identity on authentication success + * + * The Callback function must return an identity on authentication success, * and false on authentication failure. */ class Callback extends AbstractAdapter @@ -26,44 +28,45 @@ class Callback extends AbstractAdapter protected $callback; /** - * Class constructor - * - * @param callable $callback the autentication callback + * @param callable $callback The authentication callback */ public function __construct($callback = null) { - if (!is_null($callback)) { + if (null !== $callback) { $this->setCallback($callback); } } /** - * Authenticate - * @return \Zend\Authentication\Result the authentication result + * Authenticate using the provided callback + * + * @return Result The authentication result * @throws RuntimeException */ public function authenticate() { $callback = $this->getCallback(); - if (is_null($callback)) { + if (! $callback) { throw new RuntimeException('No callback provided'); } + try { $identity = call_user_func($callback, $this->getIdentity(), $this->getCredential()); - if (!$identity) { - return new Result(Result::FAILURE, null, array('Authentication failure')); - } - } catch (\Exception $e) { + } catch (Exception $e) { return new Result(Result::FAILURE_UNCATEGORIZED, null, array($e->getMessage())); } + if (! $identity) { + return new Result(Result::FAILURE, null, array('Authentication failure')); + } + return new Result(Result::SUCCESS, $identity, array('Authentication success')); } /** * Gets the value of callback. * - * @return mixed + * @return null|callable */ public function getCallback() { @@ -73,19 +76,15 @@ public function getCallback() /** * Sets the value of callback. * - * @param mixed $callback the callback + * @param callable $callback the callback * @throws InvalidArgumentException - * - * @return self */ public function setCallback($callback) { - if (!is_callable($callback)) { + if (! is_callable($callback)) { throw new InvalidArgumentException('Invalid callback provided'); } $this->callback = $callback; - - return $this; } } diff --git a/test/Adapter/CallbackTest.php b/test/Adapter/CallbackTest.php index 45d194d..a205dc0 100644 --- a/test/Adapter/CallbackTest.php +++ b/test/Adapter/CallbackTest.php @@ -5,37 +5,40 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Authentication */ namespace ZendTest\Authentication\Adapter; -use Zend\Authentication; -use Zend\Authentication\Adapter; +use Exception; +use PHPUnit_Framework_TestCase as TestCase; +use Zend\Authentication\Adapter\Callback; +use Zend\Authentication\Result; -/** - * @group Zend_Auth - */ -class CallbackTest extends \PHPUnit_Framework_TestCase +class CallbackTest extends TestCase { /** * Callback authentication adapter * - * @var \Zend\Authentication\Adapter\Callback + * @var Callback */ - protected $_adapter = null; + protected $adapter = null; /** * Set up test configuration */ public function setUp() { - $this->_setupAuthAdapter(); + $this->setupAuthAdapter(); } public function tearDown() { - $this->_adapter = null; + $this->adapter = null; + } + + protected function setupAuthAdapter() + { + $this->adapter = new Callback(); } /** @@ -47,7 +50,7 @@ public function testSetCallbackThrowsException() 'Zend\Authentication\Exception\InvalidArgumentException', 'Invalid callback provided' ); - $this->_adapter->setCallback('This is not a valid callback'); + $this->adapter->setCallback('This is not a valid callback'); } /** @@ -57,8 +60,8 @@ public function testCallbackSetGetMethods() { $callback = function () { }; - $this->_adapter->setCallback($callback); - $this->assertEquals($callback, $this->_adapter->getCallback()); + $this->adapter->setCallback($callback); + $this->assertEquals($callback, $this->adapter->getCallback()); } /** @@ -68,7 +71,7 @@ public function testClassConstructorSetCallback() { $callback = function () { }; - $adapter = new Adapter\Callback($callback); + $adapter = new Callback($callback); $this->assertEquals($callback, $adapter->getCallback()); } @@ -81,7 +84,7 @@ public function testAuthenticateThrowsException() 'Zend\Authentication\Exception\RuntimeException', 'No callback provided' ); - $this->_adapter->authenticate(); + $this->adapter->authenticate(); } /** @@ -89,7 +92,7 @@ public function testAuthenticateThrowsException() */ public function testAuthenticateProvidesCallbackWithIdentityAndCredentials() { - $adapter = $this->_adapter; + $adapter = $this->adapter; $adapter->setIdentity('testIdentity'); $adapter->setCredential('testCredential'); $that = $this; @@ -97,8 +100,8 @@ public function testAuthenticateProvidesCallbackWithIdentityAndCredentials() $that->assertEquals($identity, $adapter->getIdentity()); $that->assertEquals($credential, $adapter->getCredential()); }; - $this->_adapter->setCallback($callback); - $this->_adapter->authenticate(); + $this->adapter->setCallback($callback); + $this->adapter->authenticate(); } /** @@ -106,15 +109,15 @@ public function testAuthenticateProvidesCallbackWithIdentityAndCredentials() */ public function testAuthenticateResultIfCallbackThrows() { - $adapter = $this->_adapter; - $exception = new \Exception('Callback Exception'); + $adapter = $this->adapter; + $exception = new Exception('Callback Exception'); $callback = function () use ($exception) { throw $exception; }; $adapter->setCallback($callback); $result = $adapter->authenticate(); $this->assertFalse($result->isValid()); - $this->assertEquals(Authentication\Result::FAILURE_UNCATEGORIZED, $result->getCode()); + $this->assertEquals(Result::FAILURE_UNCATEGORIZED, $result->getCode()); $this->assertEquals(array($exception->getMessage()), $result->getMessages()); } @@ -124,7 +127,7 @@ public function testAuthenticateResultIfCallbackThrows() public function testAuthenticateResultIfCallbackReturnsFalsy() { $that = $this; - $adapter = $this->_adapter; + $adapter = $this->adapter; $falsyValues = array(false, null, '', '0', array(), 0, 0.0); array_map(function ($falsy) use ($that, $adapter) { $callback = function () use ($falsy) { @@ -133,7 +136,7 @@ public function testAuthenticateResultIfCallbackReturnsFalsy() $adapter->setCallback($callback); $result = $adapter->authenticate(); $that->assertFalse($result->isValid()); - $that->assertEquals(Authentication\Result::FAILURE, $result->getCode()); + $that->assertEquals(Result::FAILURE, $result->getCode()); $that->assertEquals(array('Authentication failure'), $result->getMessages()); }, $falsyValues); } @@ -143,20 +146,15 @@ public function testAuthenticateResultIfCallbackReturnsFalsy() */ public function testAuthenticateResultIfCallbackReturnsIdentity() { - $adapter = $this->_adapter; + $adapter = $this->adapter; $callback = function () { return 'identity'; }; $adapter->setCallback($callback); $result = $adapter->authenticate(); $this->assertTrue($result->isValid()); - $this->assertEquals(Authentication\Result::SUCCESS, $result->getCode()); + $this->assertEquals(Result::SUCCESS, $result->getCode()); $this->assertEquals('identity', $result->getIdentity()); $this->assertEquals(array('Authentication success'), $result->getMessages()); } - - protected function _setupAuthAdapter() - { - $this->_adapter = new Adapter\Callback(); - } }