Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Commit

Permalink
Merge pull request #717 from pasamio/test-improvements
Browse files Browse the repository at this point in the history
Test Improvement from eBay Content repository
  • Loading branch information
ianmacl committed Jan 9, 2012
2 parents dcbb3ed + 26955ef commit 4d6a3c1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
16 changes: 9 additions & 7 deletions tests/includes/JoomlaDatabaseTestCase.php
Expand Up @@ -510,22 +510,24 @@ public function getMockSession($options = array())

return JSessionGlobalMock::create($this, $options);
}

/**
* Gets a mock web object.
*
* @return JWeb
* @param array $options A set of options to configure the mock.
*
* @return JApplicationWeb
*
* @since 12.1
*/
public function getMockWeb()
public function getMockWeb($options = array())
{
// Load the real class first otherwise the mock will be used if jimport is called again.
require_once JPATH_PLATFORM . '/joomla/application/web.php';

// Load the mock class builder.
require_once JPATH_TESTS . '/includes/mocks/JWebMock.php';
return JWebGlobalMock::create($this);
require_once JPATH_TESTS . '/includes/mocks/JApplicationWebMock.php';

return JApplicationWebGlobalMock::create($this, $options);
}
}
16 changes: 9 additions & 7 deletions tests/includes/JoomlaTestCase.php
Expand Up @@ -451,22 +451,24 @@ public function getMockSession($options = array())

return JSessionGlobalMock::create($this, $options);
}

/**
* Gets a mock web object.
*
* @return JWeb
* @param array $options A set of options to configure the mock.
*
* @return JApplicationWeb
*
* @since 12.1
*/
public function getMockWeb()
public function getMockWeb($options = array())
{
// Load the real class first otherwise the mock will be used if jimport is called again.
require_once JPATH_PLATFORM . '/joomla/application/web.php';

// Load the mock class builder.
require_once JPATH_TESTS . '/includes/mocks/JWebMock.php';
return JWebGlobalMock::create($this);
require_once JPATH_TESTS . '/includes/mocks/JApplicationWebMock.php';

return JApplicationWebGlobalMock::create($this, $options);
}
}
Expand Up @@ -11,31 +11,32 @@
require_once __DIR__ . '/JSessionMock.php';

/**
* Mock class for JWeb.
* Mock class for JApplicationWeb.
*
* @package Joomla.UnitTest
* @since 12.1
*/
class JWebGlobalMock
class JApplicationWebGlobalMock
{
/**
* Creates and instance of the mock JWeb object.
* Creates and instance of the mock JApplicationWeb object.
*
* @param object $test A test object.
* @param object $test A test object.
* @param array $options A set of options to configure the mock.
*
* @return object
*
* @since 11.3
*/
public static function create($test)
public static function create($test, $options = array())
{
// Set expected server variables.
if (!isset($_SERVER['HTTP_HOST']))
{
$_SERVER['HTTP_HOST'] = 'localhost';
}

// Collect all the relevant methods in JWeb (work in progress).
// Collect all the relevant methods in JApplicationWeb (work in progress).
$methods = array(
'get',
'getDocument',
Expand All @@ -45,7 +46,7 @@ public static function create($test)

// Create the mock.
$mockObject = $test->getMock(
'JWeb',
'JApplicationWeb',
$methods,
// Constructor arguments.
array(),
Expand All @@ -55,15 +56,22 @@ public static function create($test)
true
);

// Mock calls to JWeb::getDocument().
// Mock calls to JApplicationWeb::getDocument().
$mockObject->expects($test->any())->method('getDocument')->will($test->returnValue(JDocumentGlobalMock::create($test)));
// Mock calls to JWeb::getDocument().

// Mock calls to JApplicationWeb::getLanguage().
$mockObject->expects($test->any())->method('getLanguage')->will($test->returnValue(JLanguageGlobalMock::create($test)));

// Mock calls to JWeb::getSession().
$mockObject->expects($test->any())->method('getSession')->will($test->returnValue(JSessionGlobalMock::create($test)));

// Mock a call to JApplicationWeb::getSession().
if (isset($options['session']))
{
$mockObject->expects($test->any())->method('getSession')->will($test->returnValue($options['session']));
}
else
{
$mockObject->expects($test->any())->method('getSession')->will($test->returnValue(JSessionGlobalMock::create($test)));
}

return $mockObject;
}
}
}
17 changes: 9 additions & 8 deletions tests/includes/mocks/JSessionMock.php
Expand Up @@ -37,14 +37,14 @@ public function getOption($name, $default = null)
}

/**
* Creates an instance of the mock JDatabase object.
* Creates an instance of the mock JSession object.
*
* @param object $test A test object.
* @param array $config An array of optional configuration values.
* getId : the value to be returned by the mock getId method
* get.user.id : the value to assign to the user object id returned by get('user')
* get.user.name : the value to assign to the user object name returned by get('user')
* get.user.username : the value to assign to the user object username returned by get('user')
* @param object $test A test object.
* @param array $options An array of optional configuration values.
* getId : the value to be returned by the mock getId method
* get.user.id : the value to assign to the user object id returned by get('user')
* get.user.name : the value to assign to the user object name returned by get('user')
* get.user.username : the value to assign to the user object username returned by get('user')
*
* @return object
*
Expand Down Expand Up @@ -110,7 +110,7 @@ public static function create($test, $options = array())
}

/**
* Mocking the quote method.
* Mocking the get method.
*
* @param string $key The key to get.
*
Expand All @@ -130,6 +130,7 @@ public function mockGet($key)
$user->id = (int) self::getOption('get.user.id', 0);
$user->name = self::getOption('get.user.name');
$user->username = self::getOption('get.user.username');
$user->guest = (int) self::getOption('get.user.guest', 1);

return $user;
}
Expand Down

0 comments on commit 4d6a3c1

Please sign in to comment.