SenseiTestCase.class.php 3.63 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
doctrine's avatar
doctrine committed
2
class Sensei_UnitTestCase extends UnitTestCase {
doctrine's avatar
doctrine committed
3 4 5 6 7 8 9 10 11 12
    protected $manager;
    protected $session;
    protected $dbh;
    protected $listener;
    protected $users;
    protected $tables;

    private $init = false;

    public function init() {
13 14 15
        if(headers_sent())
            throw new Exception("'".ob_end_flush()."'");

doctrine's avatar
doctrine committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        $this->manager   = Doctrine_Manager::getInstance();
        $this->manager->setAttribute(Doctrine::ATTR_CACHE, Doctrine::CACHE_NONE);

        if($this->manager->count() > 0) {
            $this->session = $this->manager->getSession(0);
            $this->session->clear();
            $this->dbh     = $this->session->getDBH();
            $this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER);
        } else {
            $this->dbh     = Doctrine_DB::getConnection();
            $this->session = $this->manager->openSession($this->dbh);
        }



        $this->tables = array("sensei_group","sensei_user","sensei_entity","sensei_session","sensei_variable");
        $tables = $this->tables;
        foreach($tables as $name) {
            $this->dbh->query("DROP TABLE IF EXISTS $name");
        }
        
        $this->sensei = new Sensei();
        
        $entity = new Sensei_Entity();
        
        $entity->loginname = "Chuck Norris";
        $entity->password = "toughguy";
43

doctrine's avatar
doctrine committed
44 45 46 47 48 49 50 51 52 53 54
        $entity->save();

        $this->init   = true;
        
        $this->record = $this->sensei->getRecord();
    }
    public function setUp() {
        if( ! $this->init)
            $this->init();
    }

doctrine's avatar
doctrine committed
55
    public function testConstructor() {   
doctrine's avatar
doctrine committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        $this->assertTrue($this->record instanceof Sensei_Session);

        if(isset($_COOKIE["PHPSESSID"])) {
            $this->assertEqual($this->record->session_id, $_COOKIE["PHPSESSID"]);
        }
        $updated = $this->record->updated;
        $this->assertFalse(empty($updated));
        $created = $this->record->created;
        $this->assertFalse(empty($created));

        $this->assertEqual($this->record->user_agent, $_SERVER['HTTP_USER_AGENT']);

        // make the changes persistent

        $this->sensei->flush();

        if(isset($_COOKIE["PHPSESSID"])) {
            $this->assertEqual($this->record->session_id, $_COOKIE["PHPSESSID"]);
        }
        $updated = $this->record->updated;
        $this->assertFalse(empty($updated));
        $created = $this->record->created;
        $this->assertFalse(empty($created));

        $this->assertEqual($this->record->user_agent, $_SERVER['HTTP_USER_AGENT']);
    }

    public function testLogin() {

        $this->assertFalse($this->sensei->login('Chuck Norris','unknown'));
        $this->assertEqual($this->record->logged_in, null);

        $this->assertEqual($this->record->entity_id, null);

        $this->assertTrue($this->sensei->login('Chuck Norris','toughguy'));
        $this->assertEqual($this->record->logged_in, 1);

        $this->assertEqual($this->record->entity_id, 1);
    }
95

doctrine's avatar
doctrine committed
96 97 98 99 100
    public function testLogout() {
        $this->assertTrue($this->sensei->logout());
        $this->assertEqual($this->record->logged_in, 0);

        $this->assertEqual($this->record->entity_id, 0);
101 102 103 104
        $this->assertEqual($this->record->getState(), Doctrine_Record::STATE_DIRTY);
        $this->assertEqual($this->record->getTable()->getIdentifierType(), Doctrine_Identifier::AUTO_INCREMENT);

        $this->assertEqual($this->record->getID(), 1);
doctrine's avatar
doctrine committed
105 106 107 108 109 110 111 112 113

        $this->sensei->flush();

        $this->assertEqual($this->record->logged_in, 0);

        $this->assertEqual($this->record->entity_id, 0);
    }
}
?>