auth uses the super-global variable $_SESSION which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies
of your class. This makes your code less dependent on global state and it
becomes generally more testable:
// BadclassRouter{publicfunctiongenerate($path){return$_SERVER['HOST'].$path;}}// BetterclassRouter{private$host;publicfunction__construct($host){$this->host=$host;}publicfunctiongenerate($path){return$this->host.$path;}}classController{publicfunctionmyAction(Request$request){// Instead of$page=isset($_GET['page'])?intval($_GET['page']):1;// Better (assuming you use the Symfony2 request)$page=$request->query->get('page',1);}}
Loading history...
22
{
23
$_SESSION['intraface_logged_in_user_id'] = 1;
24
return new FakeAuthUser;
25
}
26
27
function getIdentification()
28
{
29
return 'fake user';
30
}
31
}
32
33
class AuthTest extends PHPUnit_Framework_TestCase
34
{
35
const SESSION_LOGIN = 'thissessionfirstlog';
36
private $auth;
37
private $db;
38
protected $backupGlobals = false;
39
40
function setUp()
41
{
42
$this->db = MDB2::singleton(DB_DSN);
43
44
$this->auth = new Intraface_Auth(self::SESSION_LOGIN);
45
}
46
47
function tearDown()
48
{
49
$this->db->exec('TRUNCATE user');
50
unset($this->auth);
51
}
52
53
function testConstructionOfAuth()
54
{
55
$this->assertTrue(is_object($this->auth));
56
}
57
58
function testAuthMethodReturnsAnObjectOnSuccessFullAuthentication()
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: