Paladin
unread,Nov 24, 2009, 3:11:25 PM11/24/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Joomla! Framework Development
No, I'm not trying to start a new sport, nor am I recommending a
personal course of action.
But I think I'm finally narrowing down the JSession interference from
calls to JFactory::getUser(). Here's my current approach:
1) Setting up the mock:
$user = new JUser;
$mockSession = $this->getMock('JSession', array('_start',
'get'));
$mockSession->expects($this->once())->method('get')->will($this-
>returnValue($user));
JFactory::$session = $mockSession;
Since JFactory calls the session to get the user, and since
instatiating a session calls JSession::_start() to start the session,
I'm creating a mock of Session that will mock the two methods 'get'
and 'start'. By listing those as mocks, I set up essentially blank
functions that return void. Since I want 'get' to return the user, I
create a user object, and tell the session mock that I want get to be
called once and once only, and when it's called I want the user object
returned.
By listing '_start' in the methods to be mocked array, I essentially
nullify the entire routine, replacing it with a method that does
nothing at all, so no header problems.
2) Ironing out the wrinkles:
Because php by its nature pollutes the space with things we create,
creating the session mock will create a definition of JSession in the
air. Which means when JSessionTest comes along it's impossible to
include session.php for the "real" definition of JSession.
So the session.php file needs to be loaded before any of the session
mocks are created. I accomplish this by putting:
require_once JPATH_BASE . '/libraries/joomla/session/session.php';
at the end of the bootstrap file.
While this is working so far in my local tree, I wanted to post it
here before committing it, to make sure the bootstrap.php addition
doesn't foul up anyone else's tree. Another way to handle this would
be to make sure the session tests run before any mocking is done.