This is not a good solution as your libs will be referencing the
context you create, and not the singleton that is running your app. It
may seem to work, since the configuration will be identical - but it's
really not a good solution.

You have 2 options in my opinion.

1. Dirty: Create an sfContext instance when you are testing - then you
won't get the error.
2. Clean: Do not ever use sfContext::getInstance() in your model! It's
bad bad bad.

If your methods require a user object, pass them one, or pass one to
the class (via a setUser() method or similar) so that it is available
to your methods.

Read 
http://eatmymonkeydust.com/2009/08/symfony-forms-flexible-widgets-based-on-user-credentials/
for some ideas.

Russ.

On Feb 19, 6:08 pm, Nei Rauni Santos <nra...@gmail.com> wrote:
> It worked for me, do you think it's a good solution?
>
> <?php
>
> class tools
> {
>
>   /*
>    * This code is used to allow get the user object on sf 1.3
>    * I usually used to $sf_user = sfContext::getInstance()->getUser();
>    * but it throw exception with the message: The "default" context does not
> exist.
>    * on enviroment test
>    * */
>   public static function getUser(){
>
>     $configuration =
> sfApplicationConfiguration::getApplicationConfiguration(
> sfConfig::get('sf_app') );
>     $context = sfContext::createInstance($configuration);
>     return $context->getUser();
>
>   }
>
> now, at my libs i use tools:getUser()
>
> Nei
>
> On Fri, Feb 19, 2010 at 12:10 PM, Nei Rauni Santos <nra...@gmail.com> wrote:
>
>
>
> > I'm trying to write unit tests and all methods that I use
> > sfContext::getInstance()->getUser() not work. I always get this error The
> > "default" context does not exist.
>
> > My libs check informations on user's attributes, so I need get the user
> > object.
>
> > I need fix it to another way but I didn't find a solution. Can you help me?
>
> > Nei
>
> > --
> > Nei Rauni Santos
> > nra...@gmail.com
> > +55 41 85020985
>
> --
> Nei Rauni Santos
> nra...@gmail.com
> +55 41 85020985

-- 
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to symfony-us...@googlegroups.com.
To unsubscribe from this group, send email to 
symfony-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en.

Reply via email to