forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.php
218 lines (179 loc) · 6.68 KB
/
routes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
45
46
47
48
49
50
51
52
53
54
55
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
include_once(dirname(__FILE__) . '/config.php');
// ID missing entirely
class RouteMapNoIDTest extends WebTestCase {
function test() {
global $webroot, $route;
$map = $this->post($webroot . "php/routes.php");
$this->assertText('Error');
}
}
// Fetch route map for core airport
class RouteMapCoreAirportTest extends WebTestCase {
function test() {
global $webroot, $route;
// First figure out the correct results
$dbh = db_connect();
$sth = $dbh->prepare("SELECT DISTINCT src_apid,dst_apid FROM routes WHERE src_ap=?");
$sth->execute([$route["core_ap_iata"]]);
$rows = $sth->rowCount();
$this->assertTrue($rows >= 1, "No routes found");
$route["routes"] = $rows;
if($row = $sth->fetch()) {
$route["core_apid"] = $row["src_apid"];
}
// Then test
$params = array("apid" => $route["core_apid"]);
$map = $this->post($webroot . "php/routes.php", $params);
$rows = preg_split('/\n/', $map);
// N;Airport name...
$this->assertPattern("/" . $route["routes"] . ';/', $rows[0]);
// Routes (N)
$rts = preg_split('/\t/', $rows[1]);
$this->assertTrue(sizeof($rts) == $route["routes"], "Route count" . sizeof($rts) . " vs " . $route["routes"]);
}
}
// Fetch route map for core airport with airline filter
class RouteMapCoreAirportFilteredTest extends WebTestCase {
function test() {
global $webroot, $route;
// First figure out the correct results
$dbh = db_connect();
$sth = $dbh->prepare("SELECT DISTINCT src_apid,dst_apid,alid FROM routes WHERE src_ap=? AND airline=?");
$sth->execute([$route["core_ap_iata"], $route["core_ap_filter_iata"]]);
$rows = $sth->rowCount();
$this->assertTrue($rows >= 1, "No routes found");
$route["routes"] = $rows;
if($row = $sth->fetch()) {
$route["core_apid"] = $row["src_apid"];
$route["filter_alid"] = $row["alid"];
}
// Then test
$params = array("apid" => $route["core_apid"],
"alid" => $route["filter_alid"]);
$map = $this->post($webroot . "php/routes.php", $params);
$rows = preg_split('/\n/', $map);
// N;Airport name...
$this->assertPattern("/" . $route["routes"] . ';/', $rows[0]);
// Routes (N)
$rts = preg_split('/\t/', $rows[1]);
$this->assertTrue(sizeof($rts) == $route["routes"], "Route count" . sizeof($rts) . " vs " . $route["routes"]);
}
}
// Airport with no routes -- should still display itself!
class RouteMapNoRouteAirportTest extends WebTestCase {
function test() {
global $webroot, $route;
// First figure out the correct apid
$dbh = db_connect();
$sth = $dbh->prepare("SELECT * FROM airports WHERE iata=?");
$sth->execute([$route["noroute_ap_iata"]]);
$this->assertTrue($row = $sth->fetch(), "No-route airport not found");
$route["noroute_apid"] = $row["apid"];
// Then test
$params = array("apid" => $route["noroute_apid"]);
$map = $this->post($webroot . "php/routes.php", $params);
$rows = preg_split('/\n/', $map);
// N;Airport name...
$this->assertPattern("/0;/", $rows[0]);
// No routes
$this->assertTrue($rows[1] == "", "Route count");
// One airport, with details
$aps = preg_split('/\t/', $rows[2]);
$this->assertTrue(sizeof($aps) == 1, "Airport count");
$this->assertText(":" . $row["apid"] . ":" . $row["x"] . ":" . $row["y"]);
}
}
// Invalid airport ID
class RouteMapInvalidAirportTest extends WebTestCase {
function test() {
global $webroot, $route;
$params = array("apid" => $route["invalid_apid"]);
$map = $this->post($webroot . "php/routes.php", $params);
$this->assertText('Error');
}
}
// Fetch route map for core airline
class RouteMapCoreAirlineTest extends WebTestCase {
function test() {
global $webroot, $route;
// First figure out the correct results
$dbh = db_connect();
$sth = $dbh->prepare("SELECT DISTINCT src_apid,dst_apid,alid FROM routes WHERE airline=? AND codeshare=''");
$sth->execute([$route["core_al_iata"]]);
$rows = $sth->rowCount();
$this->assertTrue($rows >= 1, "No routes found");
$route["routes"] = $rows;
if($row = $sth->fetch()) {
$route["core_alid"] = $row["alid"];
}
// Then test
$params = array("apid" => "L" . $route["core_alid"]);
$map = $this->post($webroot . "php/routes.php", $params);
$rows = preg_split('/\n/', $map);
// N;Airline name...
$this->assertPattern("/" . $route["routes"] . ';/', $rows[0]);
// Routes (N)
$rts = preg_split('/\t/', $rows[1]);
$this->assertTrue(sizeof($rts) == $route["routes"], "Route count");
}
}
// Fetch route map for core airline
class RouteMapCoreAirlineWithCodesharesTest extends WebTestCase {
function test() {
global $webroot, $route;
// First figure out the correct results
$dbh = db_connect();
$sth = $dbh->prepare("SELECT DISTINCT src_apid,dst_apid,alid FROM routes WHERE airline=?");
$sth->execute([$route["core_al_iata"]]);
$rows = $sth->rowCount();
$this->assertTrue($rows >= 1, "No routes found");
$route["routes"] = $rows;
if($row = $sth->fetch()) {
$route["core_alid"] = $row["alid"];
}
// Then test
$params = array("apid" => "L" . $route["core_alid"],
"alid" => "1"); // any non-zero alid triggers codeshares
$map = $this->post($webroot . "php/routes.php", $params);
$rows = preg_split('/\n/', $map);
// N;Airline name...
$this->assertPattern("/" . $route["routes"] . ';/', $rows[0]);
// Routes (N)
$rts = preg_split('/\t/', $rows[1]);
$this->assertTrue(sizeof($rts) == $route["routes"], "Route count");
}
}
// Airline with no routes
class RouteMapNoRouteAirlineTest extends WebTestCase {
function test() {
global $webroot, $route;
// First figure out the correct apid
$dbh = db_connect();
$sth = $dbh->prepare("SELECT * FROM airlines WHERE iata=?");
$sth->execute([$route["noroute_al_iata"]]);
$this->assertTrue($row = $sth->fetch(), "No-route airline not found");
$route["noroute_alid"] = $row["alid"];
// Then test
$params = array("apid" => "L" . $route["noroute_apid"]);
$map = $this->post($webroot . "php/routes.php", $params);
$rows = preg_split('/\n/', $map);
// N;Airport name...
$this->assertPattern("/0;/", $rows[0]);
// Routes (N)
$this->assertTrue($rows[1] == "", "Route count");
// Airports (N+1)
$aps = preg_split('/\t/', $rows[2]);
$this->assertTrue(sizeof($aps) == 1, "Airport count");
}
}
// Invalid airline ID
class RouteMapInvalidAirlineTest extends WebTestCase {
function test() {
global $webroot, $route;
$params = array("apid" => "L" . $route["invalid_alid"]);
$map = $this->post($webroot . "php/routes.php", $params);
$this->assertText('Error');
}
}
?>