forgotten changes
[L-seed.git] / web / php-tests / DatabaseTest.php
1 <?php
2         include("../Database.php");
3
4         class TestOfDataBase extends UnitTestCase
5         {
6                 private $m_Database;
7                 
8                 function __construct() {
9                 }
10                 
11                 function setUp() {
12                         $this->m_Database = new DataBase();
13                         $this->m_Database->Clear();
14                 }
15                 
16                 function tearDown() {
17                         $this->m_Database->Clear();
18                         $this->m_Database->Close();
19                 }
20                 
21                 function testClear() {
22                         $username = "argletam";
23                         $this->m_Database->CreateUser($username, "42aa");
24                         $user = $this->m_Database->GetUser($username);
25                         $userid = $user->ID;
26                         
27                         $res = $this->m_Database->Clear();
28                         $this->assertTrue($res, "Clear failed");
29                         
30                         $user = $this->m_Database->GetUser($username);
31                         $this->assertTrue($user == null, "A User was found even though the database was cleared");
32                         
33                         $plants = $this->m_Database->GetPlantsForUser($userid);
34                         $this->assertTrue(count($user) == 0, "Even though the database was cleared we found plants for a user");
35                 }
36                 
37                 function testUser() {
38                         $username = "testuser";
39                         
40                         $result = $this->m_Database->CreateUser($username, "test");
41                         $this->assertTrue($result);
42                         
43                         $user = $this->m_Database->GetUser($username);
44                         $this->assertTrue($user != null);
45                         
46                         $user = $this->m_Database->GetUser("a");
47                         $this->assertTrue($user == null);
48                         
49                         $user = $this->m_Database->GetUser(null);
50                         $this->assertTrue($user == null);
51                 }
52                 
53                 function testPlant() {
54                         $username = "testuser";
55                         
56                         $result = $this->m_Database->CreateUser($username, "test");
57                         $user = $this->m_Database->GetUser($username);
58                         
59                         $data_name = "myplant";
60                         $data_code = "my fancy plant code!";
61                         
62                         $result = $this->m_Database->InsertNewPlant($user->ID, $data_name, $data_code);
63                         $this->assertTrue($result);
64                         
65                         $plant = $this->m_Database->GetPlant($user->ID, $data_name);
66                         $this->assertTrue($plant != null);
67                         $this->assertTrue($plant->UserID == $user->ID);
68                         $this->assertTrue($plant->Name == $data_name);
69                         $this->assertTrue($plant->Code == $data_code);
70                         
71                         $plants = $this->m_Database->GetPlantsForUser($user->ID);
72                         $this->assertTrue($plants != null);
73                         $this->assertTrue(count($plants) == 1);
74                         $this->assertTrue($plants[0]->ID == $plant->ID);
75                         
76                         $data_code = $plant->Code = "My New and improved Plantcode. Featuring: even fancier code!";
77                         $result = $this->m_Database->UpdatePlant($plant);
78                         $this->assertTrue($result);
79                         
80                         $plant = $this->m_Database->GetPlant($user->ID, $data_name);
81                         $this->assertTrue($plant != null);
82                         $this->assertTrue($plant->UserID == $user->ID);
83                         $this->assertTrue($plant->Name == $data_name);
84                         $this->assertTrue($plant->Code == $data_code);
85                         
86                         $result = $this->m_Database->DropPlant($plant);
87                         $this->assertTrue($result);
88                         
89                         $plant = $this->m_Database->GetPlant($user->ID, $data_name);
90                         $this->assertTrue($plant == null);
91                 }
92                 
93                 function testNextSeed() {
94                         $username = "testuser";
95                         $data_name = "myplant";
96                         $data_code = "my fancy plant code!";
97                         
98                         $this->m_Database->CreateUser($username, "test");
99                         $user = $this->m_Database->GetUser($username);
100                         
101                         $this->m_Database->InsertNewPlant($user->ID, $data_name, $data_code);
102                         $plant = $this->m_Database->GetPlant($user->ID, $data_name);
103                         
104                         $result = $this->m_Database->SetUsersNextSeed($user->ID, $plant->ID);
105                         $this->assertTrue($result);
106                         
107                         $user = $this->m_Database->GetUserByID($user->ID);
108                         $this->assertTrue($user != null);
109                         $this->assertTrue($user->NextSeedID == $plant->ID);
110                         
111                         $plant = $this->m_Database->GetPlantByID($user->ID, $plant->ID);
112                         $this->assertTrue($plant != null);
113                         $this->assertTrue($plant->IsActive);
114                 }
115                 
116                 function testSeason() {
117                         $username = "testuser";
118                         $data_name = "myplant";
119                         $data_code = "my fancy plant code!";
120                         
121                         $this->m_Database->CreateUser($username, "test");
122                         $user = $this->m_Database->GetUser($username);
123                         
124                         $this->m_Database->m_Connection->query("INSERT INTO season (IsRunning) VALUES (true)");
125                         $stmt = $this->m_Database->m_Connection->query("SELECT ID FROM season");
126                         $seasonid = $stmt->fetch_object()->ID;
127                         $this->assertTrue($seasonid != null);
128                         $stmt->close();
129                         
130                         $this->m_Database->m_Connection->query("INSERT INTO seasonscore (UserID, SeasonID, Score) VALUES (".$user->ID.", ".$seasonid.", 5)");
131                         $seasonscores = $this->m_Database->GetAllSeasonScores();
132                         $this->assertTrue($seasonscores != null);
133                         $this->assertTrue(count($seasonscores) == 1, "expected 1 got ".count($seasonscores));
134                         $this->assertTrue($seasonscores[0]->UserID == $user->ID);
135                         $this->assertTrue($seasonscores[0]->SeasonID == $seasonid);
136                         $this->assertTrue($seasonscores[0]->Season != null);
137                         $this->assertTrue($seasonscores[0]->Season->IsRunning);
138                 }
139         }
140
141 ?>