Skip to content

Commit

Permalink
Added JRules mock; improved code coverage of JRules; fixed code
Browse files Browse the repository at this point in the history
formatting in JRule and JRules test files.
  • Loading branch information
Andrew Eddie committed Oct 21, 2011
1 parent a34b024 commit 2960cf1
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 40 deletions.
94 changes: 94 additions & 0 deletions tests/includes/mocks/JRulesMock.php
@@ -0,0 +1,94 @@
<?php
/**
* @package Joomla.UnitTest
* @copyright Copyright (C) 2005 - 2011 Open Source Matters. All rights reserved.
* @license GNU General Public License
*/

/**
* Mock class for JRules.
*
* @package Joomla.UnitTest
* @since 11.3
*/
class JRulesGlobalMock
{
/**
* Creates an instance of the mock JDatabase object.
*
* @param object $test A test object.
*
* @return object
*
* @since 11.3
*/
public static function create($test)
{
// Mock all the public methods.
$methods = array(
'allow',
);

// Create the mock.
$mockObject = $test->getMock(
'JRules',
$methods,
// Constructor arguments.
array(),
// Mock class name.
'',
// Call original constructor.
false
);

$test->assignMockCallbacks(
$mockObject,
array(
'allow' => array(get_called_class(), 'mockAllow'),
)
);

return $mockObject;
}

/**
* Mocking the allow method.
*
* @param string $action The action.
*
* @return mixed Boolean or null.
*
* @since 11.3
*/
public function mockAllow($action, $identity)
{
switch ($action)
{
case 'run':
if ($identity == 0)
{
return null;
}
else
{
// Odds return true, evens false.
return (boolean) ($identity % 2);
}
return false;

case 'walk':
if ($identity == 0)
{
return null;
}
else
{
// Odds return false, evens true.
return (boolean) (1 - ($identity % 2));
}

default:
return null;
}
}
}
68 changes: 42 additions & 26 deletions tests/suite/joomla/access/JRuleTest.php
Expand Up @@ -14,28 +14,26 @@
*/
class JRuleTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{

}

public function testConstructor()
/**
* Tests the JRule::__construct method.
*
* @return void
*
* @since 11.1
*/
public function test__construct()
{
$array = array(
-42 => 1,
2 => 1,
3 => 0
);




// Get the string representation.
$string = json_encode($array);

$string = json_encode($array);

// Test constructor with array.
$rule1 = new JRule($array);
$rule1 = new JRule($array);

// Check that import equals export.
$this->assertEquals(
Expand All @@ -48,22 +46,29 @@ public function testConstructor()

// Check that import equals export.

//**// Check that import equals not export.
//**// Check that import equals not export.

$array_A = array(
$array_A = array(
-44 => 1,
2 => 1,
3 => 0
);

$string_A = json_encode($array_A);
$rule_A = new JRule($string_A);
$string_A = json_encode($array_A);
$rule_A = new JRule($string_A);
$this->assertNotEquals(
$string,
(string) $rule_A
);
}

/**
* Tests the JRule::mergeIdentity method.
*
* @return void
*
* @since 11.1
*/
public function testMergeIdentity()
{
// Construct an rule with no identities.
Expand Down Expand Up @@ -91,6 +96,13 @@ public function testMergeIdentity()
);
}

/**
* Tests the JRule::mergeIdentities method.
*
* @return void
*
* @since 11.1
*/
public function testMergeIdentities()
{
$array = array(
Expand All @@ -108,25 +120,22 @@ public function testMergeIdentities()
(string) $rule
);

// Check that import equals export.

// Check that import equals export.

//**Test testMergeIdentities with object
// Test testMergeIdentities with object

$rule_A = new JRule($array);
$rule->mergeIdentities($rule_A);
$this->assertEquals(
$rule_A = new JRule($array);
$rule->mergeIdentities($rule_A);
$this->assertEquals(
json_encode($array),
(string) $rule
);

$this->assertEquals(
$this->assertEquals(
(string) $rule_A,
(string) $rule
);



// Merge a new set, flipping some bits.
$array = array(
-42 => 0,
Expand All @@ -149,6 +158,13 @@ public function testMergeIdentities()
);
}

/**
* Tests the JRule::allow method.
*
* @return void
*
* @since 11.1
*/
public function testAllow()
{
// Simple allow and deny test.
Expand All @@ -163,7 +179,7 @@ public function testAllow()
$rule->allow(-42)
);

$this->assertEquals(Null,$rule->allow(Null));
$this->assertEquals(Null,$rule->allow(Null));

// This one should be allowed.
$this->assertTrue(
Expand Down
59 changes: 45 additions & 14 deletions tests/suite/joomla/access/JRulesTest.php
Expand Up @@ -14,19 +14,18 @@
*/
class JRulesTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{

}

/**
* This method tests both the contructor and the __toString magic method.
*
* The input for this class could come from a posted form, or from a JSON string
* stored in the database. We need to ensure that the resulting JSON is the same
* as the input.
*
* @return void
*
* @since 11.1
*/
public function testConstructor()
public function test__construct()
{
$array = array(
'edit' => array(
Expand All @@ -38,22 +37,38 @@ public function testConstructor()

$string = json_encode($array);

$object = (object) $array;

// Test input as string.
$rules = new JRules($string);
$rules = new JRules($string);
$this->assertThat(
(string) $rules,
$this->equalTo($string)
$this->equalTo($string),
'Checks input as an string.'
);

// Test input as array.
$rules = new JRules($array);
$rules = new JRules($array);
$this->assertThat(
(string) $rules,
$this->equalTo($string)
$this->equalTo($string),
'Checks input as an array.'
);

$rules = new JRules($object);
$this->assertThat(
(string) $rules,
$this->equalTo($string),
'Checks input as an object.'
);
}

/**
* Tests the JRules::mergeRule method.
*
* @return void
*
* @since 11.1
*/
public function testMergeRule()
{
$identities = array(
Expand Down Expand Up @@ -106,9 +121,11 @@ public function testMergeRule()
}

/**
* This method can merge an array of rules,
* a single JRules object,
* or a JSON rules string.
* Tests the JRules::merge method.
*
* @return void
*
* @since 11.1
*/
public function testMerge()
{
Expand Down Expand Up @@ -176,6 +193,13 @@ public function testMerge()

}

/**
* Tests the JRules::allow method.
*
* @return void
*
* @since 11.1
*/
function testAllow()
{
$array1 = array(
Expand Down Expand Up @@ -221,6 +245,13 @@ function testAllow()
);
}

/**
* Tests the JRules::getAllowed method.
*
* @return void
*
* @since 11.1
*/
function testGetAllowed()
{
$array1 = array(
Expand Down

0 comments on commit 2960cf1

Please sign in to comment.