method = $m; } function is($type) { return in_array($this->method, (array)$type); } function allowMethod() { if (!in_array($this->method, func_get_args())) throw new MethodNotAllowedException(); throw new Reached(); } function __get($n) { throw new Reached(); } function __call($n, $a) { throw new Reached(); } } function extract_method($source, $name) { $start = strpos($source, 'public function '.$name.'('); if ($start === false) return null; $open = strpos($source, '{', $start); $depth = 0; for ($i = $open; $i < strlen($source); $i++) { if ($source[$i] == '{') $depth++; else if ($source[$i] == '}' && --$depth == 0) break; } return substr($source, $start, $i - $start + 1); } foreach (array('Roles', 'UserPreference') as $n => $controller) { $src = file_get_contents("$root/web/api/app/Controller/{$controller}Controller.php"); $methods = extract_method($src, 'add')."\n".extract_method($src, 'edit'); $class = "Fake{$controller}"; eval("class $class { public \$request; function __get(\$n) { throw new Reached(); } function __call(\$n, \$a) { throw new Reached(); } $methods }"); $actions = array('edit' => array('get', 'delete')); if ($controller == 'UserPreference') $actions = array('add' => array('get', 'put', 'delete')) + $actions; foreach ($actions as $action => $refused) { foreach ($refused as $m) { $c = new $class(); $c->request = new FakeRequest($m); try { $c->$action(1); $got = 'answered'; } catch (MethodNotAllowedException $e) { $got = 'refused'; } catch (Reached $e) { $got = "ran"; } catch (Exception $e) { $got = "answered"; } check("$controller $action refuses ".strtoupper($m), $got, 'refused'); } $ok = ($action == 'add') ? 'post' : 'put'; $c = new $class(); $c->request = new FakeRequest($ok); try { $c->$action(1); $got = 'answered'; } catch (MethodNotAllowedException $e) { $got = 'refused'; } catch (Reached $e) { $got = 'ran'; } check("$controller $action accepts ".strtoupper($ok), $got, 'ran'); } } echo $failures ? "\n$failures check(s) FAILED\n" : "\nall checks passed\n"; exit($failures ? 1 : 0);