data = $data; } function is($t) { return true; } function allowMethod() {} } class FakeModel { public $ok = true; public $validationErrors = array(); function exists($id) { return true; } function save($d) { return $this->ok; } } class Resp { public $status = 200; function statusCode($c) { $this->status = $c; } } $source = file_get_contents("$root/web/api/app/Controller/UserPreferenceController.php"); $start = strpos($source, 'public function edit('); $depth = 0; for ($i = strpos($source, '{', $start); $i < strlen($source); $i++) { if ($source[$i] == '{') $depth++; else if ($source[$i] == '}' && --$depth == 0) break; } $code = substr($source, $start, $i - $start + 1); eval('class Ctl { public $request; public $UserPreference; public $response; public $vars = array(); function set($v) { $this->vars = $v; } function assertOwned($id) {} '.$code.' }'); function run($ok, $errors = array()) { $c = new Ctl; $c->request = new FakeRequest(array('Value' => 'v')); $c->UserPreference = new FakeModel; $c->UserPreference->ok = $ok; $c->UserPreference->validationErrors = $errors; $c->response = new Resp; $warned = false; set_error_handler(function() use (&$warned) { $warned = true; return true; }); $c->edit(7); restore_error_handler(); return array($c->response->status, $c->vars, $warned); } list($status, $vars, $warned) = run(true); check('a saved edit answers 200', $status, 200); check('a saved edit answers a Saved message', $vars, array('message' => 'Saved', '_serialize' => array('message'))); check('a saved edit raises no warning', $warned, false); list($status, $vars, $warned) = run(false, array('Value' => array('too long'))); check('a refused edit answers 400', $status, 400); check('a refused edit answers the validation errors', $vars, array('message' => array('Value' => array('too long')), '_serialize' => array('message'))); list($status, $vars) = run(false); check('a refused edit without errors answers Error', $vars['message'], 'Error'); exit($failures ? 1 : 0);