From 794d1b23296996dfb98c5c5bbae918416ddcf2d8 Mon Sep 17 00:00:00 2001 From: henk Date: Tue, 16 Aug 2016 11:56:35 +0200 Subject: [PATCH] #3 PSR-2 - ran PHP Coding Standards Fixer (https://github.com/FriendsOfPhp/PHP-CS-Fixer) --- doc/changelog.txt | 3 + .../Connection/PDOConnection.php | 2 +- .../Connection/PDOException.php | 3 +- .../TresholdsGovernor/Gateway/DbalGateway.php | 3 +- .../TresholdsGovernor/Gateway/RdbGateway.php | 37 ++++---- .../TresholdsGovernor/Manager/RdbManager.php | 19 ++-- .../Manager/ReleasesManagerInterface.php | 3 +- .../Manager/RequestCountsManagerInterface.php | 3 +- .../Manager/StatisticsManagerInterface.php | 6 +- .../Result/IpAddressBlocked.php | 1 - .../TresholdsGovernor/Result/Rejection.php | 1 - .../Result/UsernameBlocked.php | 2 - .../Result/UsernameBlockedForCookie.php | 2 - .../Result/UsernameBlockedForIpAddress.php | 4 +- .../Service/TresholdsGovernor.php | 57 ++++++------ .../Tests/Connection/PDOConnectionTest.php | 22 ++--- .../Tests/Gateway/DbalGatewayTest.php | 7 +- .../Tests/Gateway/PdoGatewayTest.php | 3 +- .../Tests/Gateway/RdbGatewayTest.php | 92 ++++++++----------- .../Tests/Mock/MockGateway.php | 3 +- .../Tests/Mock/RecordingWrapper.php | 7 +- .../Tests/Service/FunctionalTest.php | 92 +++++++++---------- .../Tests/Service/TresholdsGovernorTest.php | 18 ++-- 23 files changed, 176 insertions(+), 214 deletions(-) diff --git a/doc/changelog.txt b/doc/changelog.txt index 7be39cb..d9add81 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -176,4 +176,7 @@ AuthenticationGuardBundle Issue 9: cross framework library refactoring - FunctionalTest ::testCheckAuthenticationWithUserReleasedOnIpAddressAndCookie made compatible with PHP 5.3 ------------------------------------ +#3 PSR-2 +- ran PHP Coding Standards Fixer (https://github.com/FriendsOfPhp/PHP-CS-Fixer) +-------------------------------------- #2DO: tag v0.3 (when finished solving issues) diff --git a/src/Metaclass/TresholdsGovernor/Connection/PDOConnection.php b/src/Metaclass/TresholdsGovernor/Connection/PDOConnection.php index 98ca77d..44a2d12 100644 --- a/src/Metaclass/TresholdsGovernor/Connection/PDOConnection.php +++ b/src/Metaclass/TresholdsGovernor/Connection/PDOConnection.php @@ -77,4 +77,4 @@ public function executeQuery($query, array $params = array()) } // transactions may be needed for packing data when reducing granularity is added (NYI) -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Connection/PDOException.php b/src/Metaclass/TresholdsGovernor/Connection/PDOException.php index 3f483f4..f4ea942 100644 --- a/src/Metaclass/TresholdsGovernor/Connection/PDOException.php +++ b/src/Metaclass/TresholdsGovernor/Connection/PDOException.php @@ -14,5 +14,4 @@ public function __construct($errorInfo) $this->code = $sqlState; $this->errorInfo = $errorInfo; } - -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Gateway/DbalGateway.php b/src/Metaclass/TresholdsGovernor/Gateway/DbalGateway.php index 5da4306..87871e9 100644 --- a/src/Metaclass/TresholdsGovernor/Gateway/DbalGateway.php +++ b/src/Metaclass/TresholdsGovernor/Gateway/DbalGateway.php @@ -8,5 +8,4 @@ */ class DbalGateway extends RdbGateway { - -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Gateway/RdbGateway.php b/src/Metaclass/TresholdsGovernor/Gateway/RdbGateway.php index 03ea7ae..706fac7 100644 --- a/src/Metaclass/TresholdsGovernor/Gateway/RdbGateway.php +++ b/src/Metaclass/TresholdsGovernor/Gateway/RdbGateway.php @@ -1,32 +1,34 @@ connection = $dbalConnection; } /** - * @return PDOConnection|Doctrine\DBAL\Connection + * @return PDOConnection|\Doctrine\DBAL\Connection */ - protected function getConnection() + protected function getConnection() { return $this->connection; } //----------------------------- RequestCountsGatewayInterface ------------------------------------ - + /** * @return int Total of $counterColumn counted for $ipAddress with dtFrom after $timeLimit * AND as far as specified username, ipAddress and cookieToken equal to specified. @@ -89,7 +91,6 @@ public function insertOrIncrementCount(\DateTime $dateTime, $username, $ipAddres } else { $this->createRequestCountsWith($dateTime, $ipAddress, $username, $cookieToken, $counter, $blockedCounterName); } - } /** @@ -100,7 +101,8 @@ public function insertOrIncrementCount(\DateTime $dateTime, $username, $ipAddres * @param string $ipAddress * @param string $cookieToken */ - protected function getCountsIdWhereDateAndUsernameAndIpAddressAndCookie(\DateTime $dateTime, $username, $ipAddress, $cookieToken) { + protected function getCountsIdWhereDateAndUsernameAndIpAddressAndCookie(\DateTime $dateTime, $username, $ipAddress, $cookieToken) + { $sql = 'SELECT r.id FROM secu_requests r WHERE (r.username = :username) AND (r.ipAddress = :ipAddress) @@ -184,9 +186,10 @@ protected function createRequestCountsWith($datetime, $ipAddress, $username, $co * @param string $cookieToken * @throws BadFunctionCallException */ - public function updateCountsColumnWhereColumnNullAfterSupplied($columnToUpdate, \DateTime $value, \DateTime $dtLimit, $username, $ipAddress, $cookieToken) { + public function updateCountsColumnWhereColumnNullAfterSupplied($columnToUpdate, \DateTime $value, \DateTime $dtLimit, $username, $ipAddress, $cookieToken) + { if ($username === null && $ipAddress == null) { - throw new \BadFunctionCallException ('At least one of username and ip address must be supplied'); + throw new \BadFunctionCallException('At least one of username and ip address must be supplied'); } $sql = "UPDATE secu_requests SET $columnToUpdate = :value @@ -215,7 +218,7 @@ public function updateCountsColumnWhereColumnNullAfterSupplied($columnToUpdate, * Delete all RequestCounts with dtFrom before $dtLimit * @param \DateTime $dtLimit */ - public function deleteCountsUntil(\DateTime $dtLimit) + public function deleteCountsUntil(\DateTime $dtLimit) { $sql = 'DELETE FROM secu_requests WHERE dtFrom < :dtLimit'; $params = array('dtLimit' => $dtLimit->format('Y-m-d H:i:s')); @@ -337,7 +340,7 @@ public function countAddressesBlocked(\DateTime $timeLimit, $failureLimit) "; $conn = $this->getConnection(); - $found = $conn->fetchAll( $sql, array($timeLimit->format('Y-m-d H:i:s'), $failureLimit) ); + $found = $conn->fetchAll($sql, array($timeLimit->format('Y-m-d H:i:s'), $failureLimit)); return isset($found['blocked']) ? $found['blocked'] : 0; } @@ -376,7 +379,7 @@ public function insertOrUpdateRelease(\DateTime $datetime, $username, $ipAddress * @param string $cookieToken * @return int id of the Release */ - protected function getReleasesIdWhereDateAndUsernameAndIpAddressAndCookie($username, $ipAddress, $cookieToken) + protected function getReleasesIdWhereDateAndUsernameAndIpAddressAndCookie($username, $ipAddress, $cookieToken) { $params = array( 'username' => $username, @@ -459,7 +462,7 @@ public function isUserReleasedByCookieFrom($username, $cookieToken, \DateTime $r * Delete all releases dated before $dtLimit * @param DateTime $dtLimit */ - public function deleteReleasesUntil(\DateTime $dtLimit) + public function deleteReleasesUntil(\DateTime $dtLimit) { $sql = 'DELETE FROM secu_releases WHERE releasedAt < :dtLimit'; $params = array('dtLimit' => $dtLimit->format('Y-m-d H:i:s')); @@ -467,5 +470,3 @@ public function deleteReleasesUntil(\DateTime $dtLimit) $this->getConnection()->executeQuery($sql, $params); } } - -?> \ No newline at end of file diff --git a/src/Metaclass/TresholdsGovernor/Manager/RdbManager.php b/src/Metaclass/TresholdsGovernor/Manager/RdbManager.php index 1bba1c1..fd3318f 100644 --- a/src/Metaclass/TresholdsGovernor/Manager/RdbManager.php +++ b/src/Metaclass/TresholdsGovernor/Manager/RdbManager.php @@ -34,13 +34,13 @@ public function countLoginsFailedForIpAddres($ipAddress, \DateTime $timeLimit) /** {@inheritdoc} */ public function countLoginsFailedForUserName($username, \DateTime $timeLimit) - { + { return $this->gateway->countWhereSpecifiedAfter('loginsFailed', $username, null, null, $timeLimit, 'userReleasedAt'); } /** {@inheritdoc} */ public function countLoginsFailedForUserOnAddress($username, $ipAddress, \DateTime $timeLimit) - { + { return $this->gateway->countWhereSpecifiedAfter('loginsFailed', $username, $ipAddress, null, $timeLimit, 'userReleasedForAddressAndCookieAt'); } @@ -53,7 +53,7 @@ public function countLoginsFailedForUserByCookie($username, $cookieToken, \DateT /** {@inheritdoc} */ public function insertOrIncrementSuccessCount(\DateTime $dateTime, $username, $ipAddress, $cookieToken) { - $this->gateway->insertOrIncrementCount($dateTime, $username, $ipAddress, $cookieToken, true); + $this->gateway->insertOrIncrementCount($dateTime, $username, $ipAddress, $cookieToken, true); } /** {@inheritdoc} */ @@ -100,12 +100,12 @@ public function deleteCountsUntil(\DateTime $limit) //StatisticsManagerInterface /** {@inheritdoc} */ - public function countLoginsFailed( \DateTime $timeLimit) + public function countLoginsFailed(\DateTime $timeLimit) { return $this->gateway->countWhereSpecifiedAfter('loginsFailed', null, null, null, $timeLimit); } /** {@inheritdoc} */ - public function countLoginsSucceeded( \DateTime $timeLimit) + public function countLoginsSucceeded(\DateTime $timeLimit) { return $this->gateway->countWhereSpecifiedAfter('loginsSucceeded', null, null, null, $timeLimit); } @@ -151,9 +151,9 @@ public function isUserReleasedOnAddressFrom($username, $ipAddress, $timeLimit) /** {@inheritdoc} */ public function isUserReleasedByCookieFrom($username, $cookieToken, $timeLimit) - { - return $this->gateway->isUserReleasedByCookieFrom($username, $cookieToken, $timeLimit); - } + { + return $this->gateway->isUserReleasedByCookieFrom($username, $cookieToken, $timeLimit); + } /** {@inheritdoc} */ public function insertOrUpdateRelease($dateTime, $username, $ipAddress, $cookieToken) @@ -166,7 +166,4 @@ public function deleteReleasesUntil($limit) { $this->gateway->deleteReleasesUntil($limit); } - } - -?> \ No newline at end of file diff --git a/src/Metaclass/TresholdsGovernor/Manager/ReleasesManagerInterface.php b/src/Metaclass/TresholdsGovernor/Manager/ReleasesManagerInterface.php index 3ffd0ce..e0a79d7 100644 --- a/src/Metaclass/TresholdsGovernor/Manager/ReleasesManagerInterface.php +++ b/src/Metaclass/TresholdsGovernor/Manager/ReleasesManagerInterface.php @@ -41,5 +41,4 @@ public function insertOrUpdateRelease($dateTime, $username, $ipAddress, $cookieT * @param \DateTime $limit */ public function deleteReleasesUntil($limit); - -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Manager/RequestCountsManagerInterface.php b/src/Metaclass/TresholdsGovernor/Manager/RequestCountsManagerInterface.php index 8d52dd9..e99dcfb 100644 --- a/src/Metaclass/TresholdsGovernor/Manager/RequestCountsManagerInterface.php +++ b/src/Metaclass/TresholdsGovernor/Manager/RequestCountsManagerInterface.php @@ -103,5 +103,4 @@ public function releaseCountsForUserNameAndCookie($username, $cookieToken, \Date * @param \DateTime $limit */ public function deleteCountsUntil(\DateTime $limit); - -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Manager/StatisticsManagerInterface.php b/src/Metaclass/TresholdsGovernor/Manager/StatisticsManagerInterface.php index e8416bf..f9727e6 100644 --- a/src/Metaclass/TresholdsGovernor/Manager/StatisticsManagerInterface.php +++ b/src/Metaclass/TresholdsGovernor/Manager/StatisticsManagerInterface.php @@ -16,13 +16,13 @@ interface StatisticsManagerInterface * @param \DateTime $timeLimit * @return int Total of `loginsFailed` counted with `dtFrom` after $timeLimit */ - public function countLoginsFailed( \DateTime $timeLimit); + public function countLoginsFailed(\DateTime $timeLimit); /** * @param \DateTime $timeLimit * @return int Total of `loginsSucceeded` counted with `dtFrom` after $timeLimit */ - public function countLoginsSucceeded( \DateTime $timeLimit); + public function countLoginsSucceeded(\DateTime $timeLimit); /** * @param $username @@ -74,4 +74,4 @@ public function countsByUsernameBetween($username, \DateTime $limitFrom, \DateTi * @return array of arrays, each with values for all column of `secu_requests` */ public function countsByAddressBetween($ipAddress, \DateTime $limitFrom, \DateTime $limitUntil); -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Result/IpAddressBlocked.php b/src/Metaclass/TresholdsGovernor/Result/IpAddressBlocked.php index 33a6b05..61773ab 100644 --- a/src/Metaclass/TresholdsGovernor/Result/IpAddressBlocked.php +++ b/src/Metaclass/TresholdsGovernor/Result/IpAddressBlocked.php @@ -15,4 +15,3 @@ public function getCounterName() return 'ipAddressBlocked'; } } -?> \ No newline at end of file diff --git a/src/Metaclass/TresholdsGovernor/Result/Rejection.php b/src/Metaclass/TresholdsGovernor/Result/Rejection.php index 39190d9..ad22d14 100644 --- a/src/Metaclass/TresholdsGovernor/Result/Rejection.php +++ b/src/Metaclass/TresholdsGovernor/Result/Rejection.php @@ -27,4 +27,3 @@ public function __construct($message, $parameters=array()) * */ abstract public function getCounterName(); } -?> \ No newline at end of file diff --git a/src/Metaclass/TresholdsGovernor/Result/UsernameBlocked.php b/src/Metaclass/TresholdsGovernor/Result/UsernameBlocked.php index 8d04165..e9a1028 100644 --- a/src/Metaclass/TresholdsGovernor/Result/UsernameBlocked.php +++ b/src/Metaclass/TresholdsGovernor/Result/UsernameBlocked.php @@ -14,6 +14,4 @@ public function getCounterName() { return 'usernameBlocked'; } - } -?> \ No newline at end of file diff --git a/src/Metaclass/TresholdsGovernor/Result/UsernameBlockedForCookie.php b/src/Metaclass/TresholdsGovernor/Result/UsernameBlockedForCookie.php index 04e80dd..918c5a0 100644 --- a/src/Metaclass/TresholdsGovernor/Result/UsernameBlockedForCookie.php +++ b/src/Metaclass/TresholdsGovernor/Result/UsernameBlockedForCookie.php @@ -14,6 +14,4 @@ public function getCounterName() { return 'usernameBlockedForCookie'; } - } -?> \ No newline at end of file diff --git a/src/Metaclass/TresholdsGovernor/Result/UsernameBlockedForIpAddress.php b/src/Metaclass/TresholdsGovernor/Result/UsernameBlockedForIpAddress.php index 533552f..7f88b3a 100644 --- a/src/Metaclass/TresholdsGovernor/Result/UsernameBlockedForIpAddress.php +++ b/src/Metaclass/TresholdsGovernor/Result/UsernameBlockedForIpAddress.php @@ -9,9 +9,9 @@ * @copyright MetaClass Groningen 2014 */ class UsernameBlockedForIpAddress extends Rejection -{ + { public function getCounterName() { return 'usernameBlockedForIpAddress'; } -} \ No newline at end of file + } diff --git a/src/Metaclass/TresholdsGovernor/Service/TresholdsGovernor.php b/src/Metaclass/TresholdsGovernor/Service/TresholdsGovernor.php index fb72439..d11d4e2 100644 --- a/src/Metaclass/TresholdsGovernor/Service/TresholdsGovernor.php +++ b/src/Metaclass/TresholdsGovernor/Service/TresholdsGovernor.php @@ -17,9 +17,10 @@ * @author Henk Verhoeven * @copyright MetaClass Groningen 2013 - 2014 */ -class TresholdsGovernor { +class TresholdsGovernor +{ -//dependencies + //dependencies /** @var \Metaclass\TresholdsGovernor\Manager\RequestCountsManagerInterface $requestCountsManager does the actual storage and summation of RequestCounts */ public $requestCountsManager; @@ -27,16 +28,16 @@ class TresholdsGovernor { public $releasesManager; /** @var string $dtString holding the current date and time in format Y-m-d H:i:s */ - public $dtString; + public $dtString; //config with defaults /** var int $counterDurationInSeconds how many seconds each counter counts. */ - public $counterDurationInSeconds = 180; + public $counterDurationInSeconds = 180; /** @var string $blockUsernamesFor The duration for which failed login counters are summed per username. Format as DateTime offset */ public $blockUsernamesFor = '25 minutes'; - /** @var int The number of failed login attempts that are allowed per username within the $blockUsernamesFor duration. */ + /** @var int The number of failed login attempts that are allowed per username within the $blockUsernamesFor duration. */ public $limitPerUserName = 3; /** @var string $blockIpAddressesFor The duration for which failed login counters are summed per ip addess. Format as DateTime offset */ @@ -44,15 +45,15 @@ class TresholdsGovernor { /** @var int $limitBasePerIpAddress The number of failed login attempts that are allowed per IP address within the $blockIpAddressesFor duration. */ public $limitBasePerIpAddress = 10; //limit may be higher, depending on successfull logins and requests (NYI) - + /** @var string $allowReleasedUserOnAddressFor For how long a username will remain released per IP address. * Format as DateTime offset. If empty feature is switched off. */ - public $allowReleasedUserOnAddressFor = '30 days'; + public $allowReleasedUserOnAddressFor = '30 days'; /** @var string $allowReleasedUserByCookieFor For how long a username will remain released per IP address. * Format as DateTime offset. If empty feature is switched off. * Currently AuthenticationGuard does not provide cookietokens. */ - public $allowReleasedUserByCookieFor = ''; + public $allowReleasedUserByCookieFor = ''; /** @var boolean $releaseUserOnLoginSuccess Wheather each time the user logs in sucessfully, the username is released for all ip addresses and user agents. */ public $releaseUserOnLoginSuccess = false; @@ -72,7 +73,7 @@ class TresholdsGovernor { /** @var float microtime of init */ protected $initMicrotime; - /** @var string $ipAddress IP Address sending the request that is being processed */ + /** @var string $ipAddress IP Address sending the request that is being processed */ protected $ipAddress; /** @var string $username username from the request that is being processed */ @@ -106,7 +107,8 @@ class TresholdsGovernor { * This parameter should be left null if separate RequestCountsManager and ReleasesManager will be set to the corresponding public properties. * @throws \ReflectionException if property with the name of a key does not exist or is not public */ - public function __construct($params, $dataManager=null) { + public function __construct($params, $dataManager=null) + { $this->requestCountsManager = $dataManager; $this->releasesManager = $dataManager; $this->dtString = date('Y-m-d H:i:s'); @@ -119,8 +121,7 @@ public function __construct($params, $dataManager=null) { protected function setPropertiesFromParams($params) { $rClass = new \ReflectionClass($this); - forEach($params as $key => $value) - { + foreach ($params as $key => $value) { $rProp = $rClass->getProperty($key); if (!$rProp->isPublic()) { throw new \ReflectionException("Property must be public: '$key'"); @@ -137,14 +138,14 @@ protected function setPropertiesFromParams($params) * @param string $password not used * @param string $cookieToken token from the cookie from the request that is being processed */ - public function initFor($ipAddress, $username, $password, $cookieToken) + public function initFor($ipAddress, $username, $password, $cookieToken) { $this->initMicrotime = microtime(true); //cast to string because null is used for control in some Gateway functions $this->ipAddress = (string) $ipAddress; $this->username = (string) $username; - $this->cookieToken = (string) $cookieToken; + $this->cookieToken = (string) $cookieToken; //$this->password = (string) $password; $timeLimit = new \DateTime("$this->dtString - $this->blockIpAddressesFor"); @@ -175,7 +176,7 @@ public function initFor($ipAddress, $username, $password, $cookieToken) * @return \Metaclass\TresholdsGovernor\Result\Rejection or null if the governor does not require the login to be blocked. * (Blocking may still take place for reasons external to this governor) */ - public function checkAuthentication($justFailed=false) + public function checkAuthentication($justFailed=false) { if ($justFailed) { // failure, but not yet registered, add it here $this->failureCountForUserName++; @@ -186,10 +187,10 @@ public function checkAuthentication($justFailed=false) } $result = $this->decide(); - if ($justFailed || $result) { - $this->registerAuthenticationFailure($result); - } - return $result; + if ($justFailed || $result) { + $this->registerAuthenticationFailure($result); + } + return $result; } /** @@ -243,13 +244,13 @@ public function registerAuthenticationSuccess() { //? should we releaseUserNameForIpAddress? And shouldn't that have a shorter effect then release from e-mail? //? should we register (some) other failures in the session and release those here? - + $dateTime = $this->getRequestCountsDt($this->dtString); $this->requestCountsManager->insertOrIncrementSuccessCount($dateTime, $this->username, $this->ipAddress, $this->cookieToken); if ($this->releaseUserOnLoginSuccess) { $this->releaseUserName(); - } + } $this->releaseUserNameForIpAddressAndCookie(); } @@ -264,14 +265,14 @@ public function registerAuthenticationFailure(Rejection $rejection=null) /** Release the username from the current request. * Meant only to be combined with new password */ - public function releaseUserName() + public function releaseUserName() { $dateTime = new \DateTime($this->dtString); $timeLimit = new \DateTime("$this->dtString - $this->blockUsernamesFor"); $this->requestCountsManager->releaseCountsForUserName($this->username, $dateTime, $timeLimit); } - /** Release the username from the request for the IP address sending the request and the token form the cookie that was sent with the request. */ + /** Release the username from the request for the IP address sending the request and the token form the cookie that was sent with the request. */ public function releaseUserNameForIpAddressAndCookie() { $dateTime = new \DateTime($this->dtString); @@ -293,20 +294,20 @@ public function adminReleaseIpAddress() } /** Delete RequestCounts and Releases that will no longer be used according to the current blocking resp release durations. - * Packing RequestCounts into ones with longer durations has not yet been implemented. */ - public function packData() + * Packing RequestCounts into ones with longer durations has not yet been implemented. */ + public function packData() { $limit = new \DateTime("$this->dtString - $this->keepCountsFor"); $this->requestCountsManager->deleteCountsUntil($limit); $result["requestcounts_deleted_until"] = $limit; //idea pack RequestCounts to lower granularity for period between both limits - + if ($this->allowReleasedUserOnAddressFor) { $limit = min($limit, new \DateTime("$this->dtString - $this->allowReleasedUserOnAddressFor")); } if ($this->allowReleasedUserByCookieFor) { $limit = min($limit, new \DateTime("$this->dtString - $this->allowReleasedUserByCookieFor")); - } + } $this->releasesManager->deleteReleasesUntil($limit); $result["releases_deleted_until"] = $limit; @@ -371,5 +372,3 @@ public function sleepUntilFixedExecutionTime() $this->sleepUntilSinceInit($this->fixedExecutionSeconds); } } - -?> \ No newline at end of file diff --git a/src/Metaclass/TresholdsGovernor/Tests/Connection/PDOConnectionTest.php b/src/Metaclass/TresholdsGovernor/Tests/Connection/PDOConnectionTest.php index 870e0e1..02c4bdc 100644 --- a/src/Metaclass/TresholdsGovernor/Tests/Connection/PDOConnectionTest.php +++ b/src/Metaclass/TresholdsGovernor/Tests/Connection/PDOConnectionTest.php @@ -6,19 +6,18 @@ use \PDO; use Metaclass\TresholdsGovernor\Connection\PDOConnection; - class PDOConnectionTest extends \PHPUnit_Framework_TestCase { - STATIC $connection; + public static $connection; - function setup() + public function setup() { - if (!isSet(self::$connection)) { + if (!isset(self::$connection)) { $this->makeConnection(); } } - function testCreateTable() + public function testCreateTable() { self::$connection->executeQuery(' CREATE TABLE `testtable` ( @@ -28,7 +27,7 @@ function testCreateTable() '); } - function testInsertRetrieve() + public function testInsertRetrieve() { // Insert using parameterized query $datetime = '2012-01-21 21:50'; @@ -44,13 +43,13 @@ function testInsertRetrieve() $this->assertEquals($datetime, $found[0]['dtFrom']); } - function makeConnection() + public function makeConnection() { $pdo = new PDO('sqlite::memory:'); self::$connection = new PDOConnection($pdo); } - function testErrorHandlingQuery() + public function testErrorHandlingQuery() { $exception = null; try { @@ -69,7 +68,7 @@ function testErrorHandlingQuery() print 'testErrorHandlingQuery '. $exception->getLine(). ' '. $exception->getMessage(). "\n"; } - function testErrorHandlingPrepare() + public function testErrorHandlingPrepare() { $exception = null; try { @@ -84,7 +83,7 @@ function testErrorHandlingPrepare() print 'testErrorHandlingPrepare '. $exception->getLine(). ' '. $exception->getMessage(). "\n"; } - function testErrorHandlingExecute() + public function testErrorHandlingExecute() { $exception = null; try { @@ -100,5 +99,4 @@ function testErrorHandlingExecute() print 'testErrorHandlingExecute '. $exception->getLine(). ' '. $exception->getMessage(). "\n"; } - -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Tests/Gateway/DbalGatewayTest.php b/src/Metaclass/TresholdsGovernor/Tests/Gateway/DbalGatewayTest.php index 40f0ec9..f427f63 100644 --- a/src/Metaclass/TresholdsGovernor/Tests/Gateway/DbalGatewayTest.php +++ b/src/Metaclass/TresholdsGovernor/Tests/Gateway/DbalGatewayTest.php @@ -9,7 +9,8 @@ /** * For testing RdbGateway with Doctrine DBAL */ -class DbalGatewayTest extends RdbGatewayTest { +class DbalGatewayTest extends RdbGatewayTest +{ protected function makeConnection() { @@ -18,6 +19,4 @@ protected function makeConnection() 'driver' => 'pdo_sqlite', )); } - - -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Tests/Gateway/PdoGatewayTest.php b/src/Metaclass/TresholdsGovernor/Tests/Gateway/PdoGatewayTest.php index 4680685..ded22c5 100644 --- a/src/Metaclass/TresholdsGovernor/Tests/Gateway/PdoGatewayTest.php +++ b/src/Metaclass/TresholdsGovernor/Tests/Gateway/PdoGatewayTest.php @@ -15,5 +15,4 @@ protected function makeConnection() $pdo = new PDO('sqlite::memory:'); self::$connection = new PDOConnection($pdo); } - -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Tests/Gateway/RdbGatewayTest.php b/src/Metaclass/TresholdsGovernor/Tests/Gateway/RdbGatewayTest.php index 3a4c28b..56e531c 100644 --- a/src/Metaclass/TresholdsGovernor/Tests/Gateway/RdbGatewayTest.php +++ b/src/Metaclass/TresholdsGovernor/Tests/Gateway/RdbGatewayTest.php @@ -8,13 +8,13 @@ class RdbGatewayTest extends \PHPUnit_Framework_TestCase { - STATIC $connection; + public static $connection; protected $wrapper, $gateway, $requestData1; - function setup() + public function setup() { - if (!isSet(self::$connection)) { + if (!isset(self::$connection)) { $this->makeConnection(); self::createTables(); } @@ -30,7 +30,7 @@ function setup() $this->dtLimit = '2001-08-12 15:33:07'; } - static function createTables() + public static function createTables() { $sql = " CREATE TABLE `secu_requests` ( @@ -73,7 +73,7 @@ static function createTables() self::$connection ->executeQuery("CREATE INDEX `byCookie` ON secu_releases(`username`,`cookieToken`)"); } - function test_createRequestCountsWith() + public function test_createRequestCountsWith() { $loginSucceeded = false; $blockedCounterName = 'usernameBlockedForCookie'; @@ -88,8 +88,7 @@ function test_createRequestCountsWith() AND (r.cookieToken = :token) AND (addressReleasedAt IS NULL) AND (userReleasedAt IS NULL) - AND (userReleasedForAddressAndCookieAt IS NULL)" - , $callParams[0]); + AND (userReleasedForAddressAndCookieAt IS NULL)", $callParams[0]); $this->assertEquals($this->requestData1, $callParams[1], 'parameters'); $updateCall = $this->wrapper->calls[1]; @@ -124,14 +123,13 @@ function test_createRequestCountsWith() $this->assertNull($result[0]['userReleasedForAddressAndCookieAt'], 'userReleasedForAddressAndCookieAt'); } - function test_countWhereLoginsSucceededSpecifiedAfter() + public function test_countWhereLoginsSucceededSpecifiedAfter() { $result = $this->gateway->countWhereSpecifiedAfter('loginsSucceeded', $this->requestData1['username'], $this->requestData1['ipAddress'], $this->requestData1['token'], new \DateTime($this->dtLimit), 'userReleasedAt'); $this->assertEquals('executeQuery', $this->wrapper->calls[0][0], 'first call'); $callParams = $this->wrapper->calls[0][1]; - $this->assertEquals("SELECT sum(r.loginsSucceeded) FROM secu_requests r WHERE (r.dtFrom > :dtLimit) AND (r.username = :username) AND (r.ipAddress = :ipAddress) AND (r.cookieToken = :token) AND (userReleasedAt IS NULL)" - , $callParams[0]); + $this->assertEquals("SELECT sum(r.loginsSucceeded) FROM secu_requests r WHERE (r.dtFrom > :dtLimit) AND (r.username = :username) AND (r.ipAddress = :ipAddress) AND (r.cookieToken = :token) AND (userReleasedAt IS NULL)", $callParams[0]); $expectedParams = $this->requestData1; unset($expectedParams['dtFrom']); @@ -142,7 +140,7 @@ function test_countWhereLoginsSucceededSpecifiedAfter() } - function test_incrementCount() + public function test_incrementCount() { $this->gateway->insertOrIncrementCount(new \DateTime($this->requestData1['dtFrom']), $this->requestData1['username'], $this->requestData1['ipAddress'], $this->requestData1['token'], false, 'ipAddressBlocked'); @@ -156,8 +154,7 @@ function test_incrementCount() AND (r.cookieToken = :token) AND (addressReleasedAt IS NULL) AND (userReleasedAt IS NULL) - AND (userReleasedForAddressAndCookieAt IS NULL)" - , $callParams[0]); + AND (userReleasedForAddressAndCookieAt IS NULL)", $callParams[0]); $this->assertEquals($this->requestData1, $callParams[1], 'parameters'); $id = self::$connection->executeQuery($callParams[0], $callParams[1])->fetchColumn(); @@ -173,15 +170,14 @@ function test_incrementCount() $this->assertEquals($id, $updateCall[1][1]['id'], 'call 1 param 1 [id]'); } - function test_countWhereLoginsFailedSpecifiedAfter() + public function test_countWhereLoginsFailedSpecifiedAfter() { $result = $this->gateway->countWhereSpecifiedAfter('loginsFailed', null, null, null, new \DateTime($this->dtLimit), null); $this->assertEquals('executeQuery', $this->wrapper->calls[0][0], 'first call'); $callParams = $this->wrapper->calls[0][1]; $this->assertEquals( - "SELECT sum(r.loginsFailed) FROM secu_requests r WHERE (r.dtFrom > :dtLimit)" - , + "SELECT sum(r.loginsFailed) FROM secu_requests r WHERE (r.dtFrom > :dtLimit)", $callParams[0] ); $expectedParams['dtLimit'] = $this->dtLimit; @@ -191,12 +187,12 @@ function test_countWhereLoginsFailedSpecifiedAfter() } /** @expectedException \BadFunctionCallException */ - function testException_updateCountsColumnWhereColumnNullAfterSupplied() + public function testException_updateCountsColumnWhereColumnNullAfterSupplied() { $this->gateway->updateCountsColumnWhereColumnNullAfterSupplied('userReleasedAt', new \DateTime($this->requestData1['dtFrom']), new \DateTime($this->dtLimit), null, null, null); } - function test_updateCountsColumnWhereColumnNullAfterSupplied() + public function test_updateCountsColumnWhereColumnNullAfterSupplied() { $this->gateway->updateCountsColumnWhereColumnNullAfterSupplied('userReleasedForAddressAndCookieAt', new \DateTime($this->requestData1['dtFrom']), new \DateTime($this->dtLimit), $this->requestData1['username'], $this->requestData1['ipAddress'], $this->requestData1['token']); @@ -206,8 +202,7 @@ function test_updateCountsColumnWhereColumnNullAfterSupplied() "UPDATE secu_requests SET userReleasedForAddressAndCookieAt = :value WHERE (userReleasedForAddressAndCookieAt IS NULL) - AND (dtFrom > :dtLimit) AND (username = :username) AND (ipAddress = :ipAddress) AND (cookieToken = :token)" - , $callParams[0] + AND (dtFrom > :dtLimit) AND (username = :username) AND (ipAddress = :ipAddress) AND (cookieToken = :token)", $callParams[0] ); $expectedParams = $this->requestData1; $expectedParams['value'] = $this->requestData1['dtFrom']; @@ -226,7 +221,7 @@ function test_updateCountsColumnWhereColumnNullAfterSupplied() $this->assertEquals($expectedParams['value'], $result[0]['userReleasedForAddressAndCookieAt'], 'userReleasedForAddressAndCookieAt'); } - function testException_deleteCountsUntil() + public function testException_deleteCountsUntil() { $exCls = version_compare(PHP_VERSION, '7.0.0', '>=') ? '\TypeError' @@ -236,7 +231,7 @@ function testException_deleteCountsUntil() $this->gateway->deleteCountsUntil(null); } - function test_deleteCountsUntil() + public function test_deleteCountsUntil() { $now = new \DateTime(); $this->gateway->deleteCountsUntil($now) ; @@ -244,25 +239,23 @@ function test_deleteCountsUntil() $this->assertEquals('executeQuery', $this->wrapper->calls[0][0], 'first call'); $callParams = $this->wrapper->calls[0][1]; $this->assertEquals( - "DELETE FROM secu_requests WHERE dtFrom < :dtLimit" - , $callParams[0] + "DELETE FROM secu_requests WHERE dtFrom < :dtLimit", $callParams[0] ); $expectedParams = array('dtLimit' => $now->format('Y-m-d H:i:s')); $this->assertEquals($expectedParams, $callParams[1], 'parameters'); $result = self::$connection->executeQuery("SELECT * FROM secu_requests")->fetchAll(); $this->assertEquals(0, count($result), '0 rows'); - } - function test_restoreCounter() + public function test_restoreCounter() { $loginSucceeded = false; $blockedCounterName = null; $this->gateway->insertOrIncrementCount(new \DateTime($this->requestData1['dtFrom']), $this->requestData1['username'], $this->requestData1['ipAddress'], $this->requestData1['token'], $loginSucceeded, $blockedCounterName); } - function test_countsGroupedByIpAddress() + public function test_countsGroupedByIpAddress() { $now = new \DateTime(); $result = $this->gateway->countsGroupedByIpAddress(new \DateTime($this->dtLimit), $now, $this->requestData1['username']); @@ -282,8 +275,7 @@ function test_countsGroupedByIpAddress() WHERE (r.dtFrom >= ?) AND (r.addressReleasedAt IS NULL) AND (r.dtFrom < ?) AND (r.username = ?) GROUP BY r.ipAddress ORDER BY r.ipAddress - LIMIT 200" - , $callParams[0] + LIMIT 200", $callParams[0] ); $expectedParams = array($this->dtLimit, $now->format('Y-m-d H:i:s'), $this->requestData1['username']); $this->assertEquals($expectedParams, $callParams[1], 'parameters'); @@ -301,7 +293,7 @@ function test_countsGroupedByIpAddress() $this->assertEquals(0, $result[0]['usernameBlockedForCookie'], 'usernameBlockedForCookie'); } - function test_countsBetween() + public function test_countsBetween() { $now = new \DateTime(); $result = $this->gateway->countsBetween(new \DateTime($this->dtLimit), $now, $this->requestData1['username'], $this->requestData1['ipAddress']); @@ -312,8 +304,7 @@ function test_countsBetween() "SELECT * FROM secu_requests r WHERE (r.dtFrom >= ?) AND (r.dtFrom < ?) AND (r.username = ?) AND (r.ipAddress = ?) ORDER BY r.dtFrom DESC - LIMIT 500" - , $callParams[0] + LIMIT 500", $callParams[0] ); $expectedParams = array($this->dtLimit, $now->format('Y-m-d H:i:s'), $this->requestData1['username'], $this->requestData1['ipAddress'] ); $this->assertEquals($expectedParams, $callParams[1], 'parameters'); @@ -337,15 +328,14 @@ function test_countsBetween() $this->assertNull($result[0]['userReleasedForAddressAndCookieAt'], 'userReleasedForAddressAndCookieAt'); } - function test_insertRelease() + public function test_insertRelease() { $this->gateway->insertOrUpdateRelease(new \DateTime($this->requestData1['dtFrom']), $this->requestData1['username'], $this->requestData1['ipAddress'], $this->requestData1['token']); $this->assertEquals('executeQuery', $this->wrapper->calls[0][0], 'connection method called'); $callParams = $this->wrapper->calls[0][1]; $this->assertEquals( - "SELECT id from secu_releases WHERE username = :username AND ipAddress = :ipAddress AND cookieToken = :cookieToken" - , $callParams[0] + "SELECT id from secu_releases WHERE username = :username AND ipAddress = :ipAddress AND cookieToken = :cookieToken", $callParams[0] ); $expectedParams = array( 'ipAddress' => $this->requestData1['ipAddress'], @@ -357,8 +347,7 @@ function test_insertRelease() $this->assertEquals('executeQuery', $this->wrapper->calls[1][0], 'connection method called'); $callParams = $this->wrapper->calls[1][1]; $this->assertEquals( - "INSERT INTO secu_releases (releasedAt, username, ipAddress, cookieToken) VALUES (:releasedAt, :username, :ipAddress, :cookieToken)" - , $callParams[0] + "INSERT INTO secu_releases (releasedAt, username, ipAddress, cookieToken) VALUES (:releasedAt, :username, :ipAddress, :cookieToken)", $callParams[0] ); $expectedParams = array_merge($expectedParams, array('releasedAt' => $this->requestData1['dtFrom'])); $this->assertEquals($expectedParams, $callParams[1], 'parameters'); @@ -371,7 +360,7 @@ function test_insertRelease() $this->assertEquals($this->requestData1['token'], $result[0]['cookieToken'], 'cookieToken'); } - function test_updateRelease() + public function test_updateRelease() { $now = new \DateTime(); $this->gateway->insertOrUpdateRelease($now, $this->requestData1['username'], $this->requestData1['ipAddress'], $this->requestData1['token']); @@ -379,8 +368,7 @@ function test_updateRelease() $this->assertEquals('executeQuery', $this->wrapper->calls[0][0], 'first connection method called'); $callParams = $this->wrapper->calls[0][1]; $this->assertEquals( - "SELECT id from secu_releases WHERE username = :username AND ipAddress = :ipAddress AND cookieToken = :cookieToken" - , $callParams[0] + "SELECT id from secu_releases WHERE username = :username AND ipAddress = :ipAddress AND cookieToken = :cookieToken", $callParams[0] ); $expectedParams = array( 'ipAddress' => $this->requestData1['ipAddress'], @@ -394,8 +382,7 @@ function test_updateRelease() $this->assertEquals('executeQuery', $this->wrapper->calls[1][0], 'second connection method called'); $callParams = $this->wrapper->calls[1][1]; $this->assertEquals( - "UPDATE secu_releases SET releasedAt = :releasedAt WHERE id = :id" - , $callParams[0] + "UPDATE secu_releases SET releasedAt = :releasedAt WHERE id = :id", $callParams[0] ); $expectedParams = array( 'releasedAt' => $now->format('Y-m-d H:i:s'), @@ -411,7 +398,7 @@ function test_updateRelease() $this->gateway->insertOrUpdateRelease(new \DateTime($this->requestData1['dtFrom']), $this->requestData1['username'], $this->requestData1['ipAddress'], $this->requestData1['token']); } - function test_isUserReleasedOnAddressFrom() + public function test_isUserReleasedOnAddressFrom() { $result = $this->gateway->isUserReleasedOnAddressFrom($this->requestData1['username'], $this->requestData1['ipAddress'], new \DateTime($this->dtLimit)); @@ -421,8 +408,7 @@ function test_isUserReleasedOnAddressFrom() "SELECT max(r.releasedAt) FROM secu_releases r WHERE r.releasedAt >= ? - AND r.username = ? AND r.ipAddress = ? " - , $callParams[0] + AND r.username = ? AND r.ipAddress = ? ", $callParams[0] ); $expectedParams = array( $this->dtLimit, @@ -434,7 +420,7 @@ function test_isUserReleasedOnAddressFrom() $this->assertTrue($result, 'result'); } - function test_isUserReleasedByCookieFrom() + public function test_isUserReleasedByCookieFrom() { $result = $this->gateway->isUserReleasedByCookieFrom($this->requestData1['username'], $this->requestData1['token'], new \DateTime($this->dtLimit)); @@ -444,8 +430,7 @@ function test_isUserReleasedByCookieFrom() "SELECT max(r.releasedAt) FROM secu_releases r WHERE r.releasedAt >= ? - AND r.username = ? AND r.cookieToken = ? " - , $callParams[0] + AND r.username = ? AND r.cookieToken = ? ", $callParams[0] ); $expectedParams = array( $this->dtLimit, @@ -457,7 +442,7 @@ function test_isUserReleasedByCookieFrom() $this->assertTrue($result, 'result'); } - function testException_deleteReleasesUntil() + public function testException_deleteReleasesUntil() { $exCls = version_compare(PHP_VERSION, '7.0.0', '>=') ? '\TypeError' @@ -467,14 +452,13 @@ function testException_deleteReleasesUntil() $this->gateway->deleteReleasesUntil(null); } - function test_deleteReleasesUntil() + public function test_deleteReleasesUntil() { $this->gateway->deleteReleasesUntil(new \DateTime($this->dtLimit)); // Before releasedAt $this->assertEquals('executeQuery', $this->wrapper->calls[0][0]); $callParams = $this->wrapper->calls[0][1]; - $this->assertEquals("DELETE FROM secu_releases WHERE releasedAt < :dtLimit" - , $callParams[0]); + $this->assertEquals("DELETE FROM secu_releases WHERE releasedAt < :dtLimit", $callParams[0]); $this->assertEquals( array('dtLimit' => $this->dtLimit), $callParams[1], @@ -488,9 +472,9 @@ function test_deleteReleasesUntil() $this->assertEquals(0, count($result), '1 row'); } - function testCleanupDb() + public function testCleanupDb() { self::$connection->executeQuery("DELETE FROM secu_releases"); self::$connection->executeQuery("DELETE FROM secu_requests"); } -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Tests/Mock/MockGateway.php b/src/Metaclass/TresholdsGovernor/Tests/Mock/MockGateway.php index 2f7fe10..e0e244f 100644 --- a/src/Metaclass/TresholdsGovernor/Tests/Mock/MockGateway.php +++ b/src/Metaclass/TresholdsGovernor/Tests/Mock/MockGateway.php @@ -1,7 +1,7 @@ deleteCountsLimit = $dtLimit; } } -?> \ No newline at end of file diff --git a/src/Metaclass/TresholdsGovernor/Tests/Mock/RecordingWrapper.php b/src/Metaclass/TresholdsGovernor/Tests/Mock/RecordingWrapper.php index 0a9acbd..630d7a1 100644 --- a/src/Metaclass/TresholdsGovernor/Tests/Mock/RecordingWrapper.php +++ b/src/Metaclass/TresholdsGovernor/Tests/Mock/RecordingWrapper.php @@ -3,8 +3,8 @@ namespace Metaclass\TresholdsGovernor\Tests\Mock; - -class RecordingWrapper { +class RecordingWrapper +{ protected $wrapped; public $calls; @@ -21,5 +21,4 @@ public function __call($method, $arguments) $this->calls[] = array($method, $arguments, $result); return $result; } - -} \ No newline at end of file +} diff --git a/src/Metaclass/TresholdsGovernor/Tests/Service/FunctionalTest.php b/src/Metaclass/TresholdsGovernor/Tests/Service/FunctionalTest.php index effe36e..7b937c0 100644 --- a/src/Metaclass/TresholdsGovernor/Tests/Service/FunctionalTest.php +++ b/src/Metaclass/TresholdsGovernor/Tests/Service/FunctionalTest.php @@ -18,7 +18,7 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase { - STATIC $connection; + public static $connection; /** * @var TresholdsGovernor @@ -30,9 +30,9 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase */ protected $statisticsManager; - function setup() + public function setup() { - if (!isSet(self::$connection)) { + if (!isset(self::$connection)) { $pdo = new PDO('sqlite::memory:'); self::$connection = new PDOConnection($pdo); @@ -46,9 +46,9 @@ function setup() $this->governor->dtString = '1980-07-01 00:00:00'; $this->governor->counterDurationInSeconds = 300; //5 minutes - $this->governor->blockUsernamesFor = '30 days'; + $this->governor->blockUsernamesFor = '30 days'; $this->governor->blockIpAddressesFor = '30 days'; //not very realistic, but should still work - $this->governor->allowReleasedUserOnAddressFor = '30 days'; + $this->governor->allowReleasedUserOnAddressFor = '30 days'; $this->governor->allowReleasedUserByCookieFor = '10 days'; } @@ -61,14 +61,14 @@ protected function get($propName) } - function testSetup() + public function testSetup() { $dt = new \DateTime($this->governor->dtString); $this->assertEquals('1980-07-01 00:00:00', $dt->format('Y-m-d H:i:s'), 'DateTime is properly constructed'); } /** test that the request counts dtFrom will be set floored to 5 minutes, as setup has configured $this->governor */ - function testGetRequestCountsDt() + public function testGetRequestCountsDt() { $this->assertEquals('1980-07-01 00:00:00', $this->governor->getRequestCountsDt('1980-07-01 00:00:00')->format('Y-m-d H:i:s')); $this->assertEquals('1980-07-01 00:00:00', $this->governor->getRequestCountsDt('1980-07-01 00:00:01')->format('Y-m-d H:i:s')); @@ -76,7 +76,7 @@ function testGetRequestCountsDt() $this->assertEquals('1980-07-01 00:05:00', $this->governor->getRequestCountsDt('1980-07-01 00:05:00')->format('Y-m-d H:i:s')); } - function testInitFor() + public function testInitFor() { $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); $this->assertEquals(0, $this->get('failureCountForIpAddress'), 'failure count for ip address'); @@ -85,7 +85,7 @@ function testInitFor() $this->assertEquals(0, $this->get('failureCountForUserByCookie'), 'failure count for username by cookie'); } - function testRegisterAuthenticationFailure() + public function testRegisterAuthenticationFailure() { $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); $this->governor->registerAuthenticationFailure(); @@ -113,7 +113,7 @@ function testRegisterAuthenticationFailure() $this->assertEquals(1, $this->get('failureCountForIpAddress'), 'failure count by ip address'); $this->assertEquals(1, $this->get('failureCountForUserName'), 'failure count by username'); $this->assertEquals(1, $this->get('failureCountForUserOnAddress'), 'failure count for username on address'); - $this->assertEquals(0, $this->get('failureCountForUserByCookie'), 'failure count for username by other cookie'); + $this->assertEquals(0, $this->get('failureCountForUserByCookie'), 'failure count for username by other cookie'); $this->assertFalse($this->get('isUserReleasedOnAddress'), 'is user released on address'); $this->assertFalse($this->get('isUserReleasedByCookie'), 'is user released on other cookie'); @@ -131,7 +131,7 @@ function testRegisterAuthenticationFailure() } // Feature Not used - function checkAuthenticationJustFailed() + public function checkAuthenticationJustFailed() { $this->governor->limitPerUserName = 3; $this->governor->limitBasePerIpAddress = 3; @@ -141,19 +141,19 @@ function checkAuthenticationJustFailed() $this->assertEquals(1, $this->get('failureCountForUserOnAddress'), 'failure count for username on address'); $this->assertEquals(1, $this->get('failureCountForUserByCookie'), 'failure count for username by cookie'); - $this->assertNull($this->governor->checkAuthentication(true)); + $this->assertNull($this->governor->checkAuthentication(true)); $this->assertEquals(2, $this->get('failureCountForIpAddress'), 'failure count for ip address'); $this->assertEquals(2, $this->get('failureCountForUserName'), 'failure count for username'); $this->assertEquals(2, $this->get('failureCountForUserOnAddress'), 'failure count for username on address'); $this->assertEquals(2, $this->get('failureCountForUserByCookie'), 'failure count for username by cookie'); } - function testCheckAuthenticationUnreleased() + public function testCheckAuthenticationUnreleased() { $this->governor->limitPerUserName = 3; $this->governor->limitBasePerIpAddress = 2; $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); - $this->assertNull($this->governor->checkAuthentication()); + $this->assertNull($this->governor->checkAuthentication()); $this->governor->limitBasePerIpAddress = 1; $result = $this->governor->checkAuthentication(); //registers authentication failure, but that only shows up when $this->governor->initFor @@ -164,7 +164,7 @@ function testCheckAuthenticationUnreleased() $this->governor->limitPerUserName = 2; $this->governor->limitBasePerIpAddress = 3; - $this->assertNull($this->governor->checkAuthentication(), 'result'); + $this->assertNull($this->governor->checkAuthentication(), 'result'); $this->governor->limitPerUserName = 1; @@ -185,7 +185,7 @@ function testCheckAuthenticationUnreleased() $this->assertEquals(0, $counts[0]['usernameBlockedForCookie'], '192.168.255.255 usernameBlockedForCookie'); } - function testRegisterAuthenticationSuccess() + public function testRegisterAuthenticationSuccess() { $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); $this->governor->registerAuthenticationSuccess(); @@ -218,7 +218,7 @@ function testRegisterAuthenticationSuccess() $this->assertEquals(3, $this->get('failureCountForIpAddress'), 'failure count by ip address'); $this->assertEquals(3, $this->get('failureCountForUserName'), 'failure count by username, other cookieToken'); $this->assertEquals(0, $this->get('failureCountForUserOnAddress'), 'failure count for username on address, other cookieToken'); - $this->assertEquals(0, $this->get('failureCountForUserByCookie'), 'failure count for username by other cookie'); + $this->assertEquals(0, $this->get('failureCountForUserByCookie'), 'failure count for username by other cookie'); $this->assertTrue($this->get('isUserReleasedOnAddress'), 'is user released on address'); $this->assertFalse($this->get('isUserReleasedByCookie'), 'is user released on by cookie'); @@ -226,12 +226,12 @@ function testRegisterAuthenticationSuccess() $this->assertEquals(0, $this->get('failureCountForIpAddress'), 'failure count by other ip address'); $this->assertEquals(3, $this->get('failureCountForUserName'), 'failure count by username, other addres and other cookieToken'); $this->assertEquals(0, $this->get('failureCountForUserOnAddress'), 'failure count for username on other address, other cookieToken'); - $this->assertEquals(0, $this->get('failureCountForUserByCookie'), 'failure count for username by other cookie, other address'); + $this->assertEquals(0, $this->get('failureCountForUserByCookie'), 'failure count for username by other cookie, other address'); $this->assertFalse($this->get('isUserReleasedOnAddress'), 'is user released on other address'); $this->assertFalse($this->get('isUserReleasedByCookie'), 'is user released on by cookie'); } - function testRegisterAuthenticationFailureAfterSuccess() + public function testRegisterAuthenticationFailureAfterSuccess() { $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); $this->governor->registerAuthenticationFailure(); @@ -256,15 +256,15 @@ function testRegisterAuthenticationFailureAfterSuccess() $this->assertEquals(4, $this->get('failureCountForIpAddress'), 'failure count by ip address'); $this->assertEquals(4, $this->get('failureCountForUserName'), 'failure count by username'); $this->assertEquals(1, $this->get('failureCountForUserOnAddress'), 'failure count for username on address'); - $this->assertEquals(0, $this->get('failureCountForUserByCookie'), 'failure count for username by other cookie'); + $this->assertEquals(0, $this->get('failureCountForUserByCookie'), 'failure count for username by other cookie'); $this->assertTrue($this->get('isUserReleasedOnAddress'), 'is user released on address'); $this->assertFalse($this->get('isUserReleasedByCookie'), 'is user released by other cookie'); } - function testCheckAuthenticationWithUserReleasedOnIpAddressAndCookie() + public function testCheckAuthenticationWithUserReleasedOnIpAddressAndCookie() { $this->governor->dtString = '1980-07-01 00:05:00'; //5 minutes later - + $this->governor->limitPerUserName = 2; $this->governor->limitBasePerIpAddress = 5; $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); @@ -275,14 +275,14 @@ function testCheckAuthenticationWithUserReleasedOnIpAddressAndCookie() $this->governor->initFor('192.168.255.254', 'testuser1', 'whattheheck', 'cookieToken1'); $this->assertNull($this->governor->checkAuthentication()); //assert no Rejection because of cookieToken released - + $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken2'); $this->assertNull($this->governor->checkAuthentication()); //assert no Rejection because of ip address released - + $this->governor->limitPerUserName = 5; $this->governor->initFor('192.168.255.254', 'testuser1', 'whattheheck', 'cookieToken2'); $this->assertNull($this->governor->checkAuthentication()); //assert no Rejection on other ip address - + $this->governor->limitBasePerIpAddress = 4; $this->governor->limitPerUserName = 2; $this->governor->initFor('192.168.255.255', 'testuser2', 'whattheheck', 'cookieToken1'); @@ -294,10 +294,10 @@ function testCheckAuthenticationWithUserReleasedOnIpAddressAndCookie() $this->governor->initFor('192.168.255.254', 'testuser1', 'whattheheck', 'cookieToken1'); $this->assertNull($this->governor->checkAuthentication()); //assert no Rejection because of cookieToken released - + $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken2'); $this->assertNull($this->governor->checkAuthentication()); //assert no Rejection because of ip address released - + $this->governor->limitPerUserName = 0; $result = $this->governor->checkAuthentication(); //registers authentication failure on cookieToken2 and ip 255 $this->assertNotNull($result, 'result'); @@ -322,20 +322,20 @@ function testCheckAuthenticationWithUserReleasedOnIpAddressAndCookie() $counts = $this->statisticsManager->countsByAddressBetween('192.168.255.255', $from, $until); $this->assertEquals(2, count($counts), '192.168.255.255 counter2'); - $countsTestuser1 = array_filter($counts, function($e) { + $countsTestuser1 = array_filter($counts, function ($e) { return $e['username'] == 'testuser1'; }); - $this->assertEquals(1, count($countsTestuser1) , '192.168.255.255 testuser1'); + $this->assertEquals(1, count($countsTestuser1), '192.168.255.255 testuser1'); $testuser1 = current($countsTestuser1); $this->assertEquals(0, $testuser1['ipAddressBlocked'], '192.168.255.255 ipAddressBlocked'); $this->assertEquals(0, $testuser1['usernameBlocked'], '192.168.255.255 usernameBlocked'); $this->assertEquals(1, $testuser1['usernameBlockedForIpAddress'], '192.168.255.255 usernameBlockedForIpAddress'); $this->assertEquals(0, $testuser1['usernameBlockedForCookie'], '192.168.255.255 usernameBlockedForCookie'); - $countsTestuser2 = array_filter($counts, function($e) { + $countsTestuser2 = array_filter($counts, function ($e) { return $e['username'] == 'testuser2'; }); - $this->assertEquals(1, count($countsTestuser2) , '192.168.255.255 testuser2'); + $this->assertEquals(1, count($countsTestuser2), '192.168.255.255 testuser2'); $testuser2 = current($countsTestuser2); $this->assertEquals(1, $testuser2['ipAddressBlocked'], '192.168.255.255 ipAddressBlocked'); $this->assertEquals(0, $testuser2['usernameBlocked'], '192.168.255.255 usernameBlocked'); @@ -343,7 +343,7 @@ function testCheckAuthenticationWithUserReleasedOnIpAddressAndCookie() $this->assertEquals(0, $testuser2['usernameBlockedForCookie'], '192.168.255.255 usernameBlockedForCookie'); } - function testBlockingDurations() + public function testBlockingDurations() { $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); $this->assertEquals(6, $this->get('failureCountForIpAddress'), 'failure count by ip address'); @@ -354,7 +354,7 @@ function testBlockingDurations() $this->assertTrue($this->get('isUserReleasedByCookie'), 'is user released by cookie'); $this->governor->dtString = '1980-07-10 23:59:59'; //just less then 10 days after first request - + $this->governor->blockUsernamesFor = '10 days'; $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); @@ -381,13 +381,12 @@ function testBlockingDurations() $this->governor->blockIpAddressesFor = '863995 seconds'; //5 seconds less then 10 days $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); $this->assertEquals(2, $this->get('failureCountForIpAddress'), 'failure count by ip address'); - } - function testReleaseDurations() + public function testReleaseDurations() { $this->governor->dtString = '1980-07-11 00:00:00'; //10 days after first request and releases - + $this->governor->allowReleasedUserOnAddressFor = '10 days'; $this->governor->allowReleasedUserByCookieFor = '10 days'; $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); @@ -410,10 +409,9 @@ function testReleaseDurations() //should not be influenced: $this->assertEquals(2, $this->get('failureCountForUserOnAddress'), 'failure count for username on address'); $this->assertEquals(2, $this->get('failureCountForUserByCookie'), 'failure count for username by cookie'); - } - function testDeleteData1() + public function testDeleteData1() { $this->get('requestCountsManager')->deleteCountsUntil(new \DateTime('1981-01-01')); $this->get('releasesManager')->deleteReleasesUntil(new \DateTime('1981-01-01')); @@ -428,7 +426,8 @@ function testDeleteData1() $this->assertFalse($this->get('isUserReleasedByCookie'), 'is user released by cookie'); } - function testRegisterAuthenticationSuccessReleasingUser() { + public function testRegisterAuthenticationSuccessReleasingUser() + { $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken1'); $this->governor->releaseUserOnLoginSuccess = true; $this->governor->registerAuthenticationFailure(); @@ -473,9 +472,9 @@ function testRegisterAuthenticationSuccessReleasingUser() { $this->assertEquals(0, $this->get('failureCountForUserByCookie'), 'failure count for username by other cookie, other address'); $this->assertFalse($this->get('isUserReleasedOnAddress'), 'is user released on other address'); $this->assertFalse($this->get('isUserReleasedByCookie'), 'is user released by other cookie'); - } + } - function testCheckAuthenticationWithUserReleased() + public function testCheckAuthenticationWithUserReleased() { $this->governor->limitPerUserName = 1; $this->governor->limitBasePerIpAddress = 1; @@ -487,15 +486,15 @@ function testCheckAuthenticationWithUserReleased() $this->governor->initFor('192.168.255.254', 'testuser1', 'whattheheck', 'cookieToken1'); $this->assertNull($this->governor->checkAuthentication()); //assert no Rejection because of cookieToken released - + $this->governor->initFor('192.168.255.255', 'testuser1', 'whattheheck', 'cookieToken2'); $this->assertNull($this->governor->checkAuthentication()); //assert no Rejection because of ip address released - + $this->governor->limitBasePerIpAddress = 1; $this->governor->limitPerUserName = 1; $this->governor->initFor('192.168.255.254', 'testuser1', 'whattheheck', 'cookieToken2'); $this->assertNull($this->governor->checkAuthentication()); //assert no Rejection because user released - + $this->governor->limitBasePerIpAddress = 0; $this->governor->initFor('192.168.255.255', 'testuser2', 'whattheheck', 'cookieToken1'); $result = $this->governor->checkAuthentication(); //registers authentication failure for testuser2 @@ -505,7 +504,7 @@ function testCheckAuthenticationWithUserReleased() $this->assertEquals(array('%ipAddress%' => '192.168.255.255'), $result->parameters); } - function testDeleteData2() + public function testDeleteData2() { $this->get('requestCountsManager')->deleteCountsUntil(new \DateTime('1981-01-01')); $this->get('releasesManager')->deleteReleasesUntil(new \DateTime('1981-01-01')); @@ -519,7 +518,4 @@ function testDeleteData2() $this->assertFalse($this->get('isUserReleasedOnAddress'), 'is user released on address'); $this->assertFalse($this->get('isUserReleasedByCookie'), 'is user released by cookie'); } - - } -?> \ No newline at end of file diff --git a/src/Metaclass/TresholdsGovernor/Tests/Service/TresholdsGovernorTest.php b/src/Metaclass/TresholdsGovernor/Tests/Service/TresholdsGovernorTest.php index ae2da06..26d4f98 100644 --- a/src/Metaclass/TresholdsGovernor/Tests/Service/TresholdsGovernorTest.php +++ b/src/Metaclass/TresholdsGovernor/Tests/Service/TresholdsGovernorTest.php @@ -17,9 +17,9 @@ class TresholdsGovernorTest extends \PHPUnit_Framework_TestCase { public $governor; - function setup() + public function setup() { - $this->governor = new TresholdsGovernor(array()); + $this->governor = new TresholdsGovernor(array()); $this->governor->dtString = '1980-07-01 00:00:00'; $this->governor->counterDurationInSeconds = 300; //5 minutes @@ -27,7 +27,6 @@ function setup() $this->governor->blockIpAddressesFor = '30 days'; //not very realistic, but should still work $this->governor->allowReleasedUserOnAddressFor = '30 days'; $this->governor->allowReleasedUserByCookieFor = '10 days'; - } protected function get($propName) @@ -38,7 +37,7 @@ protected function get($propName) return $rProp->getValue($this->governor); } - function testPackData() + public function testPackData() { $this->governor->requestCountsManager = new RdbManager(new MockGateway()); $this->governor->releasesManager = new RdbManager(new MockGateway()); @@ -47,7 +46,7 @@ function testPackData() $this->governor->blockIpAddressesFor = '5 days'; // $this->governor->allowReleasedUserOnAddressFor = '30 days'; // $this->governor->allowReleasedUserByCookieFor = '10 days'; - + $this->assertNull($this->governor->requestCountsManager->gateway->deleteReleasesLimit, "releasesLimit on requestCountsGateway"); $this->assertNull($this->governor->releasesManager->gateway->deleteCountsLimit, "deleteCountsLimit on releasesGateway"); @@ -87,13 +86,13 @@ function testPackData() $this->assertEquals(new \DateTime('1980-06-26 00:00:00'), $this->governor->releasesManager->gateway->deleteReleasesLimit, "deleteReleasesLimit on releasesGateway"); } - function assertNoException($value, $message = '') + public function assertNoException($value, $message = '') { //assertNotNull crashes on exception. // workaround for ugly $this->assertThat($result, $this->logicalNot(new \PHPUnit_Framework_Constraint_Exception('Exception')) ); - if ($value instanceOf \Exception) { + if ($value instanceof \Exception) { $this->assertTrue(true); // replaces self::$count += count($constraint); wich does not work because $count is private :-( - + $failureDescription = "Failed asserting no Exception: \n" . get_class($value) . " with message '". $value->getMessage(); $failureDescription .= "' in ". $value->getFile(). ':'. $value->getLine(); @@ -103,6 +102,5 @@ function assertNoException($value, $message = '') } throw new \PHPUnit_Framework_ExpectationFailedException($failureDescription, null); } - } + } } -?> \ No newline at end of file