public function testServiceRequestEvents() { // A post test Platform::on('user.list', 'http://dsp.local/web/eventReceiver', static::API_KEY); // An inline test Platform::on('user.list', function ($event, $eventName, $dispatcher) { $this->assertEquals('user.list', $eventName); $this->_actionEventFired = 1; echo 'event "user.list" has been fired.'; }, static::API_KEY); $this->_preProcessFired = $this->_postProcessFired = 0; $_config = (require dirname(dirname(__DIR__)) . '/config/web.php'); /** @var \RestController $_controller */ list($_controller, $_actionId) = Pii::app()->createController('rest/user'); try { $_controller->setService('user'); $_service = ServiceHandler::getService($_controller->getService()); $_service->on(PlatformServiceEvents::PRE_PROCESS, function ($event, $eventName, $dispatcher) { $this->assertEquals('user.get.pre_process', $eventName); $this->_preProcessFired = 1; }); $_service->on(PlatformServiceEvents::POST_PROCESS, function ($event, $eventName, $dispatcher) { $this->assertEquals('user.get.post_process', $eventName); $this->_postProcessFired = 1; }); // Test GET $_request = Pii::app()->getRequestObject(); $_request->query->set('app_name', Inflector::neutralize(__CLASS__)); $_request->overrideGlobals(); $_response = $_service->processRequest(null, HttpMethod::GET, false); $this->assertTrue(is_array($_response) && isset($_response['resource'])); $this->assertTrue(1 == $this->_preProcessFired && 1 == $this->_postProcessFired && 1 == $this->_actionEventFired); } catch (\Exception $ex) { RestResponse::sendErrors($ex); } }
/** * Generic action handler * * @param string $action * * @return mixed */ protected function _handleAction($action) { try { $_service = ServiceHandler::getService($this->_service); return $_service->processRequest($this->_resource, $action); } catch (\Exception $ex) { RestResponse::sendErrors($ex, isset($_service) ? $_service->getOutputFormat() : DataFormats::JSON, false, false); return null; } }