From 673a964aedfe5382278911b92cacc0db8451b14b Mon Sep 17 00:00:00 2001 From: Dmytro Sokil Date: Sun, 14 Dec 2014 20:19:49 +0200 Subject: [PATCH] test variable processor --- src/Processor/VariableProcessor.php | 20 ----------------- tests/DetectorTest.php | 35 ++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/Processor/VariableProcessor.php b/src/Processor/VariableProcessor.php index ab78692..e1029b9 100644 --- a/src/Processor/VariableProcessor.php +++ b/src/Processor/VariableProcessor.php @@ -57,30 +57,11 @@ protected function getValue() } } - protected function isValueExists() - { - switch($this->variableType) { - case 'GLOBALS' : return array_key_exists($this->name, $GLOBALS); - case '_GET' : return array_key_exists($this->name, $_GET); - case '_POST' : return array_key_exists($this->name, $_POST); - case '_SERVER' : return array_key_exists($this->name, $_SERVER); - case '_REQUEST' : return array_key_exists($this->name, $_REQUEST); - case '_SESSION' : return array_key_exists($this->name, $_SESSION); - case '_COOKIE' : return array_key_exists($this->name, $_COOKIE); - case '_FILES' : return array_key_exists($this->name, $_FILES); - case '_ENV' : return array_key_exists($this->name, $_ENV); - } - } - public function isPassed() { foreach($this->conditionList as $condition => $conditionValue) { switch($condition) { - case self::CONDITION_EXISTS: - $passed = $this->isValueExists(); - break; - case self::CONDITION_EQUALS: $passed = ($this->getValue() === $conditionValue); break; @@ -113,7 +94,6 @@ public function isPassed() return true; } - public function equals($value) { $this->conditionList[self::CONDITION_EQUALS] = $value; diff --git a/tests/DetectorTest.php b/tests/DetectorTest.php index b3d6a76..7f4e6c5 100644 --- a/tests/DetectorTest.php +++ b/tests/DetectorTest.php @@ -150,7 +150,25 @@ public function testCheck_RequestRate_PdoMysqlCollector($pdoErrorMode) $pdo->query('DROP TABLE IF EXISTS test_collector'); } - public function testCheck_Variable() + public function getVarTypeList() + { + return array( + array('GLOBALS'), + array('_GET'), + array('_POST'), + array('_SERVER'), + array('_REQUEST'), + array('_SESSION'), + array('_COOKIE'), + array('_FILES'), + array('_ENV'), + ); + } + + /** + * @dataProvider getVarTypeList + */ + public function testCheck_Variable($varType) { $detector = new Detector(); @@ -160,9 +178,14 @@ public function testCheck_Variable() $detector ->setKey('someKey') - ->declareProcessor('variable', function(\Sokil\FraudDetector\Processor\VariableProcessor $processor) { + ->declareProcessor('variable', function($processor) use($varType) { /* @var $processor \Sokil\FraudDetector\Processor\VariableProcessor */ - $processor->setName('globalVariable')->equals(42); + $processor + ->setName('globalVariable', $varType) + ->equals(42) + ->notEquals(500) + ->greater(41) + ->lower(43); }) ->onCheckPassed(function() use($status) { $status->ok = true; @@ -171,11 +194,13 @@ public function testCheck_Variable() $status->ok = false; }); - $GLOBALS['globalVariable'] = 42; + // valid var + $GLOBALS[$varType]['globalVariable'] = 42; $detector->check(); $this->assertTrue($status->ok); - $GLOBALS['globalVariable'] = 43; + // invalid var + $GLOBALS[$varType]['globalVariable'] = 43; $detector->check(); $this->assertFalse($status->ok); }