public function testCanBuildUpCommands() { $c = new Operation(array()); $c->setName('foo')->setClass('Baz')->setDeprecated(false)->setSummary('summary')->setDocumentationUrl('http://www.foo.com')->setHttpMethod('PUT')->setResponseNotes('oh')->setResponseClass('string')->setUri('/foo/bar')->addParam(new Parameter(array('name' => 'test'))); $this->assertEquals('foo', $c->getName()); $this->assertEquals('Baz', $c->getClass()); $this->assertEquals(false, $c->getDeprecated()); $this->assertEquals('summary', $c->getSummary()); $this->assertEquals('http://www.foo.com', $c->getDocumentationUrl()); $this->assertEquals('PUT', $c->getHttpMethod()); $this->assertEquals('oh', $c->getResponseNotes()); $this->assertEquals('string', $c->getResponseClass()); $this->assertEquals('/foo/bar', $c->getUri()); $this->assertEquals(array('test'), $c->getParamNames()); }
/** * Add a Swagger Api declaration which may consist of multiple operations * @param array consisting of path, description and array of operations * @param string URL inferring the base location for api path * @throws \Exception * @return Swizzle */ public function addApi(array $api, $baseUrl = '') { $service = $this->getServiceDescription(); if (!$baseUrl) { $baseUrl = $service->getBaseUrl(); } // resolve URL relative to base path for all operations $uri = implode('/', array(rtrim($baseUrl, '/'), ltrim($api['path'], '/'))); // keep domain only if not under service base path if (0 === strpos($uri, $service->getBaseUrl())) { $uri = preg_replace('!^https?://[^/]+!', '', $uri); } $this->debug('+ adding api %s ...', $uri); // no need for full url if relative to current // operation keys common to both swagger and guzzle static $common = array('items' => 1, 'summary' => 1); // translate swagger -> guzzle static $trans = array('type' => 'responseType', 'notes' => 'responseNotes', 'method' => 'httpMethod'); static $defaults = array('httpMethod' => 'GET'); foreach ($api['operations'] as $op) { $config = $this->transformArray($op, $common, $trans) + $defaults; $config['uri'] = $uri; // command must have a name, and must be unique across methods if (isset($op['nickname'])) { $id = $config['name'] = $op['nickname']; } else { $method = strtolower($config['httpMethod']); $id = $config['name'] = $method . '_' . str_replace('/', '_', trim($uri, '/')); } // allow custom command class, or global class for all commands if (isset($this->commandClasses[$id])) { $config['class'] = $this->commandClasses[$id]; } else { if (isset($this->commandClasses[''])) { $config['class'] = $this->commandClasses['']; } } // allow registered response class to override all response type logic if (isset($this->responseClasses[$id])) { $config['responseType'] = 'class'; $config['responseClass'] = $this->responseClasses[$id]; } else { if (isset($config['responseType'])) { // Check for primitive values first $type = $this->transformSimpleType($config['responseType']) or $type = $config['responseType']; // Array primitive may be typed with 'items' spec, but Guzzle operation ignores at top level if ('array' === $type) { if (isset($op['items'])) { $this->debug("! no modelling support for root arrays. Item types won't be validated"); } } else { if ('object' === $type) { $model = $this->addModel($op); $type = $model->getName(); } else { if ('number' === $type) { $this->debug('! number type defaulted to string as responseClass'); $type = 'string'; } else { if ('null' === $type) { $this->debug('! empty type "%s" defaulted to empty responseClass', $config['responseType']); $type = ''; } } } } // Ensure service contructor calls inferResponseType by having class but no type // This will handle Guzzle primatives, models and fall back to class $config['responseClass'] = $type; unset($config['responseType']); } } // handle parameters if (isset($op['parameters'])) { $config['parameters'] = $this->transformParams($op['parameters']); } else { $config['parameters'] = array(); } // handle responseMessages -> errorResponses if (isset($op['responseMessages'])) { $config['errorResponses'] = $this->transformResponseMessages($op['responseMessages']); } else { $config['errorResponses'] = array(); } // @todo how to deny additional parameters in command calls? // $config['additionalParameters'] = false; $operation = new Operation($config, $service); // Sanitize custom response class because Guzzle doesn't know it doesn't exist yet if (Operation::TYPE_CLASS === $operation->getResponseType()) { $class = $operation->getResponseClass(); if (empty($this->responseClasses[$id]) || $class !== $this->responseClasses[$id]) { throw new \Exception('responseType defaulted to class "' . $class . '" but class not registered'); } } $service->addOperation($operation); // next operation - } return $this; }