[symfony-users] Re: How to get user object on libs to not get the erro The default context does not exist.

2010-02-22 Thread Nei Rauni Santos
I had to change this function to work both on functional test and unit test.

public static function getUser(){

if( ! sfContext::hasInstance() ){

  $configuration =
sfApplicationConfiguration::getApplicationConfiguration(
sfConfig::get('sf_app') );
  $context = sfContext::createInstance($configuration);
  return $context-getUser();

}else{
  return sfContext::getInstance()-getUser();
}

  }


Nei


On Fri, Feb 19, 2010 at 2: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.comwrote:

 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




-- 
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.



[symfony-users] Re: How to get user object on libs to not get the erro The default context does not exist.

2010-02-22 Thread lawrence


On Feb 19, 12: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()



Do you need to call the user? Do you need data from the user object?
If not, you can use a mock object. If you do, then, your unit test is
really a functional test, not a unit test. You might as well be
running in the test in the context of the whole site.

-- 
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.



[symfony-users] Re: How to get user object on libs to not get the erro The default context does not exist.

2010-02-19 Thread Nei Rauni Santos
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.



[symfony-users] Re: How to get user object on libs to not get the erro The default context does not exist.

2010-02-19 Thread rooster (Russ)
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.