/** * @group mock * @depends testFactory * @returns SwaggerClient */ public function testMockApiDeclaration(SwaggerClient $client) { $plugin = new MockPlugin(); $plugin->addResponse(new Response(200, array(), $this->declarationJson)); $client->addSubscriber($plugin); $declaration = $client->getDeclaration(array('path' => '/ping')); $this->assertInstanceOf('\\Loco\\Utils\\Swizzle\\Response\\ApiDeclaration', $declaration); $this->assertEquals('/ping', $declaration->getResourcePath()); // Should have one API $apis = $declaration->getApis(); $this->assertCount(1, $apis); // Should have one model $models = $declaration->getModels(); $this->assertCount(1, $models); $this->assertArrayHasKey('Echo', $models->toArray()); }
/** * Build from a live endpoint * @param string Swagger compliant JSON endpoint for resource listing * @throws \Exception * @return Swizzle */ public function build($base_url) { $this->service = null; $client = SwaggerClient::factory(compact('base_url')); $this->debug('pulling resource listing from %s', $base_url); /* @var $listing ResourceListing */ $listing = $client->getResources(); // check this looks like a resource listing if (!$listing->isSwagger()) { throw new \Exception("This doesn't look like a Swagger spec"); } if (!$listing->getApis()) { $this->logger->addAlert("Resource listing doesn't define any APIs"); } // check swagger version if (self::SWAGGER_VERSION !== $listing->getSwaggerVersion()) { throw new \Exception('Unsupported Swagger version, Swizzle expects ' . self::SWAGGER_VERSION); } // Declared version overrides anything we've set if ($version = $listing->getApiVersion()) { $this->debug('+ set apiVersion %s', $version); $this->setApiVersion($version); } // Set description if missing from constructor if (!$this->init['description']) { $info = $listing->getInfo(); $this->init['description'] = $info['description'] ?: $this->init['title']; } // no more configs allowed now, Guzzle service gets constructed $service = $this->getServiceDescription(); // set base path from docs location if not provided if (!$service->getBaseUrl()) { $service->setBaseUrl(self::mergeUrl('/', $base_url)); } // ready to pull each api declaration foreach ($listing->getApiPaths() as $path) { if ($this->delay) { usleep($this->delay); } // @todo do proper path resolution here, allowing a cross-domain spec. $path = trim($path, '/ '); $this->debug('pulling /%s ...', $path); $declaration = $client->getDeclaration(compact('path')); foreach ($declaration->getModels() as $model) { $this->addModel($model); } // Ensure a fully qualified base url for this api $baseUrl = self::mergeUrl($declaration->getBasePath(), $service->getBaseUrl()); // add each api against required base url foreach ($declaration->getApis() as $api) { $this->addApi($api, $baseUrl); } } $this->debug('finished'); return $this; }