row = is_array($row) ? $row : array(); } function commandFieldChangesNotAllowed($fields) { return array(); } function Capturing() { return isset($this->row["Capturing"]) ? $this->row["Capturing"] : "None"; } function Type() { return isset($this->row["Type"]) ? $this->row["Type"] : "Ftp"; } function zmcControl($command) {} } class Zone { static $saves = array(); function save($data) { self::$saves[] = $data; return true; } }'); class FakeMonitorModel { public $alias = 'Monitor'; public $primaryKey = 'Id'; public $id = null; public $saveResult = true; public $validatesResult = true; public $validationErrors = array(); public $createCalls = 0; public $saveCalls = 0; public $findCalls = 0; function create() { $this->createCalls++; } function save($data) { $this->saveCalls++; if (!$this->saveResult) return false; $this->id = 77; return true; } function find($type, $options) { $this->findCalls++; return array('Monitor' => array('Id' => 77, 'Capturing' => 'None', 'Type' => 'Ftp')); } function validates() { return $this->validatesResult; } } class FakeRequest { public $post; public $data; function __construct($post, $data) { $this->post = $post; $this->data = $data; } function is($type) { return $type === 'post' ? $this->post : false; } } // The method's require_once paths are relative to its own file, so the class lives in a // scratch tree that also holds empty Monitor.php and Zone.php stubs. $scratch = '/tmp/numes-tests/madd-run-'.getmypid(); $ctrlDir = $scratch.'/web/api/app/Controller'; if (!is_dir($ctrlDir)) mkdir($ctrlDir, 0777, true); if (!is_dir($scratch.'/web/includes')) mkdir($scratch.'/web/includes', 0777, true); file_put_contents($scratch.'/web/includes/Monitor.php', "setCalls++; \$this->out = \$data; }\n" ." function requestCommandFields() { return array(); }\n" ." protected function pinRequestId(\$model, \$id) { \$model->id = \$id; }\n" .$method."\n" ."}\n"; file_put_contents($ctrlDir.'/FakeMonitorsController.php', "Monitor = new FakeMonitorModel(); $c->Monitor->saveResult = true; $c->request = new FakeRequest(true, array('Monitor' => array('Name' => 'Front door'))); $user = null; $c->add(); check('POST save answers message Saved', $c->out['message'], 'Saved'); check('POST save answers the new id', array_key_exists('id', $c->out) ? $c->out['id'] : 'missing', 77); check('POST save serializes message', in_array('message', $c->out['_serialize'], true), true); check('POST save serializes id', in_array('id', $c->out['_serialize'], true), true); // POST that fails validation: the errors are the message and id is null. $c = new FakeController(); $c->Monitor = new FakeMonitorModel(); $c->Monitor->saveResult = false; $c->Monitor->validatesResult = false; $c->Monitor->validationErrors = array('Name' => array('This field cannot be left blank')); $c->request = new FakeRequest(true, array('Monitor' => array('Name' => ''))); $user = null; $c->add(); check('failed POST answers the validation errors', $c->out['message'], $c->Monitor->validationErrors); check('failed POST answers id null', array_key_exists('id', $c->out) ? $c->out['id'] : 'missing', null); check('failed POST serializes id', in_array('id', $c->out['_serialize'], true), true); // GET: refused before anything is created or set. $c = new FakeController(); $c->Monitor = new FakeMonitorModel(); $c->request = new FakeRequest(false, array()); $user = null; $thrown = false; $message = null; try { $c->add(); } catch (BadRequestException $e) { $thrown = true; $message = $e->getMessage(); } check('GET is refused with BadRequestException', $thrown, true); check('GET message names the method', is_string($message) && strpos($message, 'Invalid method') !== false, true); check('GET does not set a response', $c->out, null); check('GET does not set anything', $c->setCalls, 0); check('GET does not create a monitor', $c->Monitor->createCalls, 0); check('GET does not save a monitor', $c->Monitor->saveCalls, 0); echo $failures ? "\n$failures failed\n" : "\nall passed\n"; exit($failures ? 1 : 0);