Skip to content

Commit

Permalink
test variable processor
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Dec 14, 2014
1 parent c85f947 commit 673a964
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
20 changes: 0 additions & 20 deletions src/Processor/VariableProcessor.php
Expand Up @@ -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;
Expand Down Expand Up @@ -113,7 +94,6 @@ public function isPassed()
return true;
}


public function equals($value)
{
$this->conditionList[self::CONDITION_EQUALS] = $value;
Expand Down
35 changes: 30 additions & 5 deletions tests/DetectorTest.php
Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit 673a964

Please sign in to comment.