Skip to content

Commit

Permalink
Renamed Zend\Authentication exceptions to match SPL in full
Browse files Browse the repository at this point in the history
Altered ZendTest\Authentication to test for new exceptions and messages
  • Loading branch information
Ralph Schindler committed Sep 8, 2010
1 parent 702dd17 commit 09adaa0
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 60 deletions.
10 changes: 5 additions & 5 deletions library/Zend/Authentication/Adapter/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ protected function _setDbAdapter(AbstractDBAdapter $zendDb = null)
if(null === $this->_zendDb) {
$this->_zendDb = AbstractTable::getDefaultAdapter();
if (null === $this->_zendDb) {
throw new MissingDependencyException(
'A null adapter was provided but there is no default adatper '
. 'registered with Zend\Db\Table to utilize.'
throw new RuntimeException(
'Null was provided for the adapter but there is no default'
. ' adatper registered with Zend\Db\Table to utilize.'
);
}
}
Expand Down Expand Up @@ -412,7 +412,7 @@ protected function _authenticateSetup()
}

if (null !== $exception) {
throw new Exception($exception);
throw new RuntimeException($exception);
}

$this->_authenticateResultInfo = array(
Expand Down Expand Up @@ -479,7 +479,7 @@ protected function _authenticateQuerySelect(DBSelect $dbSelect)
unset($origDbFetchMode);
}
} catch (\Exception $e) {
throw new Exception('The supplied parameters to Zend\\Authentication\\Adapter\\DbTable failed to '
throw new RuntimeException('The supplied parameters to Zend\Authentication\Adapter\DbTable failed to '
. 'produce a valid sql statement, please check table and column names '
. 'for validity.', 0, $e);
}
Expand Down
4 changes: 2 additions & 2 deletions library/Zend/Authentication/Adapter/Digest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ public function setPassword($password)
/**
* Defined by Zend_Auth_Adapter_Interface
*
* @throws Zend\Authentication\Adapter\Exception
* @throws Zend\Authentication\Adapter\RuntimeException
* @return Zend\Authentication\Result
*/
public function authenticate()
{
$optionsRequired = array('filename', 'realm', 'username', 'password');
foreach ($optionsRequired as $optionRequired) {
if (null === $this->{"_$optionRequired"}) {
throw new MissingDependencyException("Option '$optionRequired' must be set before authentication");
throw new RuntimeException("Option '$optionRequired' must be set before authentication");
}
}

Expand Down
22 changes: 10 additions & 12 deletions library/Zend/Authentication/Adapter/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,13 @@ class Http implements AuthenticationAdapter
* 'use_opaque' => <bool> Whether to send the opaque value in the header
* 'alogrithm' => <string> See $_supportedAlgos. Default: MD5
* 'proxy_auth' => <bool> Whether to do authentication as a Proxy
* @throws Zend\Authentication\Adapter\MissingDependencyException
* @throws Zend\Authentication\Adapter\InvalidArgumentException
* @return void
*/
public function __construct(array $config)
{
if (!extension_loaded('hash')) {
throw new MissingDependencyException(__CLASS__ . ' requires the \'hash\' extension to be availabe in PHP');
throw new InvalidArgumentException(__CLASS__ . ' requires the \'hash\' extension to be availabe in PHP');
}

$this->_request = null;
Expand Down Expand Up @@ -340,15 +339,14 @@ public function getResponse()
/**
* Authenticate
*
* @throws Zend\Authentication\Adapter\MissingDependencyException
* @throws Zend\Authentication\Adapter\UnsupportedRequestException
* @throws Zend\Authentication\Adapter\RuntimeException
* @return Zend\Authentication\Result
*/
public function authenticate()
{
if (empty($this->_request) ||
empty($this->_response)) {
throw new MissingDependencyException('Request and Response objects must be set before calling '
throw new RuntimeException('Request and Response objects must be set before calling '
. 'authenticate()');
}

Expand Down Expand Up @@ -391,7 +389,7 @@ public function authenticate()
$result = $this->_digestAuth($authHeader);
break;
default:
throw new UnsupportedRequestException('Unsupported authentication scheme: ' . $clientScheme);
throw new RuntimeException('Unsupported authentication scheme: ' . $clientScheme);
}

return $result;
Expand Down Expand Up @@ -474,10 +472,10 @@ protected function _digestHeader()
protected function _basicAuth($header)
{
if (empty($header)) {
throw new UnexpectedValueException('The value of the client Authorization header is required');
throw new RuntimeException('The value of the client Authorization header is required');
}
if (empty($this->_basicResolver)) {
throw new MissingDependencyException(
throw new RuntimeException(
'A basicResolver object must be set before doing Basic '
. 'authentication');
}
Expand All @@ -486,7 +484,7 @@ protected function _basicAuth($header)
$auth = substr($header, strlen('Basic '));
$auth = base64_decode($auth);
if (!$auth) {
throw new UnexpectedValueException('Unable to base64_decode Authorization header value');
throw new RuntimeException('Unable to base64_decode Authorization header value');
}

// See ZF-1253. Validate the credentials the same way the digest
Expand Down Expand Up @@ -522,10 +520,10 @@ protected function _basicAuth($header)
protected function _digestAuth($header)
{
if (empty($header)) {
throw new UnexpectedValueException('The value of the client Authorization header is required');
throw new RuntimeException('The value of the client Authorization header is required');
}
if (empty($this->_digestResolver)) {
throw new MissingDependencyException('A digestResolver object must be set before doing Digest authentication');
throw new RuntimeException('A digestResolver object must be set before doing Digest authentication');
}

$data = $this->_parseDigestAuth($header);
Expand Down Expand Up @@ -580,7 +578,7 @@ protected function _digestAuth($header)
// Should be REQUEST_METHOD . ':' . uri . ':' . hash(entity-body),
// but this isn't supported yet, so fall through to default case
default:
throw new UnsupportedRequestException('Client requested an unsupported qop option');
throw new RuntimeException('Client requested an unsupported qop option');
}
// Using hash() should make parameterizing the hash algorithm
// easier
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Authentication/Adapter/Http/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Exception extends \Zend\Authentication\Exception
interface Exception extends \Zend\Authentication\Adapter\Exception
{}
12 changes: 6 additions & 6 deletions library/Zend/Authentication/Adapter/Http/FileResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct($path = '')
public function setFile($path)
{
if (empty($path) || !is_readable($path)) {
throw new Exception('Path not readable: ' . $path);
throw new InvalidArgumentException('Path not readable: ' . $path);
}
$this->_file = $path;

Expand Down Expand Up @@ -109,22 +109,22 @@ public function getFile()
public function resolve($username, $realm)
{
if (empty($username)) {
throw new Exception('Username is required');
throw new InvalidArgumentException('Username is required');
} else if (!ctype_print($username) || strpos($username, ':') !== false) {
throw new Exception('Username must consist only of printable characters, '
throw new InvalidArgumentException('Username must consist only of printable characters, '
. 'excluding the colon');
}
if (empty($realm)) {
throw new Exception('Realm is required');
throw new InvalidArgumentException('Realm is required');
} else if (!ctype_print($realm) || strpos($realm, ':') !== false) {
throw new Exception('Realm must consist only of printable characters, '
throw new InvalidArgumentException('Realm must consist only of printable characters, '
. 'excluding the colon.');
}

// Open file, read through looking for matching credentials
$fp = @fopen($this->_file, 'r');
if (!$fp) {
throw new Exception('Unable to open password file: ' . $this->_file);
throw new RuntimeException('Unable to open password file: ' . $this->_file);
}

// No real validation is done on the contents of the password file. The
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Zend\Authentication\Adapter\Http;

class InvalidArgumentException
extends \InvalidArgumentException
implements Exception
{
}
9 changes: 9 additions & 0 deletions library/Zend/Authentication/Adapter/Http/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Zend\Authentication\Adapter\Http;

class RuntimeException
extends \RuntimeException
implements Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Zend\Authentication\Adapter;

class MissingDependencyException
class RuntimeException
extends \RuntimeException
implements Exception
{
Expand Down

This file was deleted.

30 changes: 9 additions & 21 deletions tests/Zend/Authentication/Adapter/DbTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testAuthenticateFailureIdentityNotFound()
try {
$result = $this->_adapter->authenticate();
$this->assertEquals(Authentication\Result::FAILURE_IDENTITY_NOT_FOUND, $result->getCode());
} catch (Authentication\Exception $e) {
} catch (Adapter\RuntimeException $e) {
$this->fail('Exception should have been thrown');
}
}
Expand All @@ -150,7 +150,7 @@ public function testAuthenticateFailureIdentityAmbigious()
try {
$result = $this->_adapter->authenticate();
$this->assertEquals(Authentication\Result::FAILURE_IDENTITY_AMBIGUOUS, $result->getCode());
} catch (Authentication\Exception $e) {
} catch (Adapter\RuntimeException $e) {
$this->fail('Exception should have been thrown');
}
}
Expand Down Expand Up @@ -252,75 +252,63 @@ public function testAdapterReturnsASelectObjectWithoutAuthTimeModificationsAfter

/**
* Ensure that exceptions are caught
*
* @expectedException Zend\Authentication\Exception
*/
public function testCatchExceptionNoTable()
{
$this->setExpectedException('Zend\Authentication\Adapter\RuntimeException', 'A table must be supplied for');
$adapter = new Adapter\DbTable($this->_db);
$result = $adapter->authenticate();
// $this->assertEquals($e->getMessage(), 'A table must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
}

/**
* Ensure that exceptions are caught
*
* @expectedException Zend\Authentication\Exception
*/
public function testCatchExceptionNoIdentityColumn()
{
$this->setExpectedException('Zend\Authentication\Adapter\RuntimeException', 'An identity column must be supplied for the');
$adapter = new Adapter\DbTable($this->_db, 'users');
$result = $adapter->authenticate();
// $this->assertEquals($e->getMessage(), 'An identity column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
}

/**
* Ensure that exceptions are caught
*
* @expectedException Zend\Authentication\Exception
*/
public function testCatchExceptionNoCredentialColumn()
{
$this->setExpectedException('Zend\Authentication\Adapter\RuntimeException', 'A credential column must be supplied');
$adapter = new Adapter\DbTable($this->_db, 'users', 'username');
$result = $adapter->authenticate();
// $this->assertEquals($e->getMessage(), 'A credential column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
}

/**
* Ensure that exceptions are caught
*
* @expectedException Zend\Authentication\Exception
*/
public function testCatchExceptionNoIdentity()
{
$this->setExpectedException('Zend\Authentication\Adapter\RuntimeException', 'A value for the identity was not provided prior');
$result = $this->_adapter->authenticate();
// $this->assertEquals($e->getMessage(), 'A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.');
}

/**
* Ensure that exceptions are caught
*
* @expectedException Zend\Authentication\Exception
*/
public function testCatchExceptionNoCredential()
{
$this->setExpectedException('Zend\Authentication\Adapter\RuntimeException', 'A credential value was not provided prior');
$this->_adapter->setIdentity('my_username');
$result = $this->_adapter->authenticate();
// $this->assertEquals($e->getMessage(), 'A credential value was not provided prior to authentication with Zend_Auth_Adapter_DbTable.');
}

/**
* Ensure that exceptions are caught
*
* @expectedException Zend\Authentication\Exception
*/
public function testCatchExceptionBadSql()
{
$this->setExpectedException('Zend\Authentication\Adapter\RuntimeException', 'The supplied parameters to');
$this->_adapter->setTableName('bad_table_name');
$this->_adapter->setIdentity('value');
$this->_adapter->setCredential('value');
$result = $this->_adapter->authenticate();
// $this->assertEquals($e->getMessage(), 'The supplied parameters to Zend_Auth_Adapter_DbTable failed to produce a valid sql statement, please check table and column names for validity.');
}

/**
Expand All @@ -347,7 +335,7 @@ public function testDbTableAdapterUsesCaseFolding()
*/
public function testAuthenticateWithDefaultDbAdapterNoAdapterException()
{
$this->setExpectedException('Zend\Authentication\Adapter\Exception', "No database adapter present");
$this->setExpectedException('Zend\Authentication\Adapter\RuntimeException', 'Null was provided');

// make sure that no default adapter exists
\Zend\Db\Table\AbstractTable::setDefaultAdapter(null);
Expand Down
2 changes: 1 addition & 1 deletion tests/Zend/Authentication/Adapter/Ldap/OfflineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testGetSetLdap()
$this->markTestSkipped('LDAP is not enabled');
}
$this->_adapter->setLdap(new \Zend\Ldap\Ldap());
$this->assertType('Zend_Ldap', $this->_adapter->getLdap());
$this->assertType('Zend\Ldap\Ldap', $this->_adapter->getLdap());
}

public function testUsernameIsNullIfNotSet()
Expand Down
2 changes: 1 addition & 1 deletion tests/Zend/Authentication/AuthenticationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @group Zend_Auth
*/
class AuthTest extends \PHPUnit_Framework_TestCase
class AuthenticationServiceTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
Expand Down

0 comments on commit 09adaa0

Please sign in to comment.