Skip to content
This repository has been archived by the owner on Jul 9, 2019. It is now read-only.

Commit

Permalink
Cleanup some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesrosiers committed Feb 8, 2014
1 parent 4d04758 commit cfc7762
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 78 deletions.
14 changes: 7 additions & 7 deletions src/JDesrosiers/Silex/Provider/SwaggerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public function getResourceList(Application $app, Request $request)
"output" => "json",
"json_pretty_print" => $app["swagger.prettyPrint"],
'basePath' => $app["swagger.basePath"],
'prefix' => $app["swagger.resourcePrefix"],
'suffix' => $app["swagger.resourceSuffix"],
'apiVersion' => $app["swagger.apiVersion"],
'swaggerVersion' => $app["swagger.swaggerVersion"],
"prefix" => $app["swagger.resourcePrefix"],
"suffix" => $app["swagger.resourceSuffix"],
"apiVersion" => $app["swagger.apiVersion"],
"swaggerVersion" => $app["swagger.swaggerVersion"],
);
$json = $app["swagger"]->getResourceList($options);

Expand Down Expand Up @@ -114,9 +114,9 @@ public function getResourceDefinition(Application $app, Request $request, $servi
$options = array(
"output" => "json",
"json_pretty_print" => $app["swagger.prettyPrint"],
'defaultBasePath' => $app["swagger.basePath"],
'defaultApiVersion' => $app["swagger.apiVersion"],
'defaultSwaggerVersion' => $app["swagger.swaggerVersion"],
"defaultBasePath" => $app["swagger.basePath"],
"defaultApiVersion" => $app["swagger.apiVersion"],
"defaultSwaggerVersion" => $app["swagger.swaggerVersion"],
);
$json = $app["swagger"]->getResource($resourceName, $options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,30 @@ public function setUp()

public function dataProviderApiDocs()
{
$configs = array(
"swagger.basePath" => "http://localhost:8888",
"swagger.apiVersion" => "1.1",
"swagger.swaggerVersion" => "9.9.9",
);

return array(
array("/api-docs", "/foo", "/api-docs/foo", "/api-docs/baz"),
array("/api-docs", "/foo/bar", "/api-docs/foo-bar", "/api-docs/baz"),
array("/api-docs", "/fooBarDefaults", "/api-docs/fooBarDefaults", "/api-docs/baz"),
array("/api/api-docs", "/foo", "/api/api-docs/foo", "/api/api-docs/baz"),
array("/api/api-docs", "/foo/bar", "/api/api-docs/foo-bar", "/api/api-docs/baz"),
array("/api/api-docs", "/fooBarDefaults", "/api/api-docs/fooBarDefaults", "/api/api-docs/baz"),
array("/api-docs", array(), "/foo", "/api-docs/foo", "/api-docs/baz"),
array("/api-docs", array(), "/foo/bar", "/api-docs/foo-bar", "/api-docs/baz"),
array("/api-docs", $configs, "/fooBarDefaults", "/api-docs/fooBarDefaults", "/api-docs/baz"),
array("/api/api-docs", array(), "/foo", "/api/api-docs/foo", "/api/api-docs/baz"),
array("/api/api-docs", array(), "/foo/bar", "/api/api-docs/foo-bar", "/api/api-docs/baz"),
array("/api/api-docs", $configs, "/fooBarDefaults", "/api/api-docs/fooBarDefaults", "/api/api-docs/baz"),
);
}

/**
* @dataProvider dataProviderApiDocs
*/
public function testApiDocs($apiDocPath, $resource, $resourcePath, $excludePath)
public function testApiDocs($apiDocPath, $options, $resource, $resourcePath, $excludePath)
{
$resourceList = array(
"apiVersion" => "0.1",
"swaggerVersion" => "1.2",
"apiVersion" => isset($options["swagger.apiVersion"]) ? $options["swagger.apiVersion"] : "0.1",
"swaggerVersion" => isset($options["swagger.swaggerVersion"]) ? $options["swagger.swaggerVersion"] : "1.2",
"apis" => array(
array(
"path" => "/foo",
Expand All @@ -56,76 +62,27 @@ public function testApiDocs($apiDocPath, $resource, $resourcePath, $excludePath)
),
),
);
$options = array(
"output" => "json",
"json_pretty_print" => $this->app["swagger.prettyPrint"]
);
$expectedResponse = $this->app["swagger"]->getResource($resource, $options);

$this->app["swagger.apiDocPath"] = $apiDocPath;

// Test resource list
$client = new Client($this->app);
$client->request("GET", $apiDocPath);
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals("application/json", $response->headers->get("Content-Type"));
$this->assertEquals($resourceList, json_decode($response->getContent(), true));

// Test resource
$client = new Client($this->app);
$client->request("GET", $resourcePath);
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals("application/json", $response->headers->get("Content-Type"));
$this->assertEquals($expectedResponse, $response->getContent());

// Test excluded resource
$client = new Client($this->app);
$client->request("GET", $excludePath);
$response = $client->getResponse();
$this->assertEquals(404, $response->getStatusCode());
}


/**
* @dataProvider dataProviderApiDocs
* Ensure that swagger.basePath, .apiVersion and .swaggerVersion options
* are respected.
*/
public function testApiDocsDefaults($apiDocPath, $resource, $resourcePath, $excludePath)
{
$resourceList = array(
"apiVersion" => "1.1",
"swaggerVersion" => "9.9.9",
"basePath" => "http://localhost:8888",
"apis" => array(
array(
"path" => "/foo",
),
array(
"path" => "/foo-bar",
),
array(
"path" => "/fooBarDefaults",
),
),
);
if (isset($options["swagger.basePath"])) {
$resourceList["basePath"] = $options["swagger.basePath"];
}

$this->app["swagger.apiDocPath"] = $apiDocPath;
$this->app["swagger.basePath"] = $resourceList['basePath'];
$this->app["swagger.apiVersion"] = $resourceList['apiVersion'];
$this->app["swagger.swaggerVersion"] = $resourceList['swaggerVersion'];
// Ensure that swagger.basePath, .apiVersion and .swaggerVersion options are respected.
foreach ($options as $key => $value) {
$this->app[$key] = $value;
}

$options = array(
"output" => "json",
"json_pretty_print" => $this->app["swagger.prettyPrint"],
"defaultBasePath" => $resourceList['basePath'],
"defaultApiVersion" => $resourceList['apiVersion'],
"defaultSwaggerVersion" => $resourceList['swaggerVersion'],
"defaultBasePath" => $this->app["swagger.basePath"],
"defaultApiVersion" => $this->app["swagger.apiVersion"],
"defaultSwaggerVersion" => $this->app["swagger.swaggerVersion"],
);
$expectedResponse = $this->app["swagger"]->getResource($resource, $options);

$this->app["swagger.apiDocPath"] = $apiDocPath;

// Test resource list
$client = new Client($this->app);
$client->request("GET", $apiDocPath);
Expand Down Expand Up @@ -210,7 +167,7 @@ public function testLogging()
$realPath = realpath($this->app["swagger.servicePath"]);

$this->app["logger"] = $this->getMock("Symfony\Component\HttpKernel\Log\LoggerInterface");
$this->app["logger"]->expects($this->once())
$this->app["logger"]->expects($this->any())
->method("notice")
->with("Skipping files in \"$realPath/tests\" add your \"vendor\" directory to the exclude paths");

Expand Down

0 comments on commit cfc7762

Please sign in to comment.