Skip to content

Commit

Permalink
Merge 18fc8ee into 7f90803
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Aug 16, 2016
2 parents 7f90803 + 18fc8ee commit 93b1390
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Operation.php
Expand Up @@ -8,6 +8,7 @@
use gossi\swagger\parts\ProducesPart;
use gossi\swagger\parts\ResponsesPart;
use gossi\swagger\parts\SchemesPart;
use gossi\swagger\parts\SecurityPart;
use gossi\swagger\parts\TagsPart;
use phootwork\collection\CollectionUtils;
use phootwork\lang\Arrayable;
Expand All @@ -17,6 +18,7 @@ class Operation extends AbstractModel implements Arrayable {
use ConsumesPart;
use ProducesPart;
use TagsPart;
use SecurityPart;
use ParametersPart;
use ResponsesPart;
use SchemesPart;
Expand Down Expand Up @@ -51,6 +53,7 @@ private function parse($contents = []) {
$this->parseConsumes($data);
$this->parseProduces($data);
$this->parseTags($data);
$this->parseSecurity($data);
$this->parseParameters($data);
$this->parseResponses($data);
$this->parseSchemes($data);
Expand All @@ -60,8 +63,8 @@ private function parse($contents = []) {

public function toArray() {
return $this->export('summary', 'description', 'operationId', 'deprecated',
'consumes', 'produces', 'parameters', 'responses', 'schemes', 'tags',
'externalDocs');
'consumes', 'produces', 'security', 'parameters', 'responses', 'schemes',
'tags', 'externalDocs');
}

/**
Expand Down
25 changes: 24 additions & 1 deletion src/SecurityScheme.php
@@ -1,17 +1,40 @@
<?php
namespace gossi\swagger;

class SecurityScheme {
use phootwork\lang\Arrayable;

class SecurityScheme extends AbstractModel implements Arrayable {

/** @var string */
private $name;

/** @var string */
private $type;

/** @var string */
private $description;

/**
* @param string $name
* @param array $contents
*/
public function __construct($name, $contents = []) {
$this->name = $name;
$this->parse($contents);
}

/**
* @param array $contents
*/
private function parse($contents = []) {
$this->type = $contents->get('type');
$this->description = $contents->get('description');
}

/**
* @return array
*/
public function toArray() {
return $this->export('type', 'description');
}
}
2 changes: 1 addition & 1 deletion src/Swagger.php
Expand Up @@ -121,7 +121,7 @@ private function parse($contents) {

public function toArray() {
return $this->export('swagger', 'info', 'host', 'basePath', 'schemes', 'consumes', 'produces',
'paths', 'definitions', 'parameters', 'responses', 'tags', 'externalDocs'
'securityDefinitions', 'paths', 'definitions', 'parameters', 'responses', 'tags', 'externalDocs'
);
}

Expand Down
31 changes: 31 additions & 0 deletions src/parts/SecurityPart.php
@@ -0,0 +1,31 @@
<?php
namespace gossi\swagger\parts;

use phootwork\collection\Map;

trait SecurityPart {

/** @var object */
private $security;

private function parseSecurity(Map $data) {
$this->security = $data->get('security');
}

/**
* @return object
*/
public function getSecurity() {
return $this->security;
}

/**
* @param object $security
* @return $this
*/
public function setSecurity($security) {
$this->security = $security;
return $this;
}

}
27 changes: 27 additions & 0 deletions tests/BasicAuthTest.php
@@ -0,0 +1,27 @@
<?php
namespace gossi\swagger\tests;

use gossi\swagger\Swagger;
use phootwork\file\exception\FileNotFoundException;
use phootwork\file\File;
use phootwork\json\Json;

class BasicAuth extends \PHPUnit_Framework_TestCase {

private function fileToArray($filename) {
$file = new File($filename);

if (!$file->exists()) {
throw new FileNotFoundException(sprintf('File not found at: %s', $filename));
}

return Json::decode($file->read());
}

public function testUser() {
$filename = __DIR__ . '/fixtures/basic-auth.json';
$swagger = Swagger::fromFile($filename);

$this->assertEquals($this->fileToArray($filename), $swagger->toArray());
}
}
35 changes: 35 additions & 0 deletions tests/fixtures/basic-auth.json
@@ -0,0 +1,35 @@
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Basic Auth Example",
"description": "An example for how to use Basic Auth with Swagger.\nServer code is available [here](https://github.com/mohsen1/basic-auth-server). It's running on Heroku.\n\n**User Name and Password**\n* User Name: `user`\n* Password: `pass`\n"
},
"host": "basic-auth-server.herokuapp.com",
"schemes": [
"http",
"https"
],
"securityDefinitions": {
"basicAuth": {
"type": "basic",
"description": "HTTP Basic Authentication. Works over `HTTP` and `HTTPS`"
}
},
"paths": {
"/": {
"get": {
"security": [
{
"basicAuth": []
}
],
"responses": {
"200": {
"description": "Will send `Authenticated` if authentication is succesful, otherwise it will send `Unauthorized`"
}
}
}
}
}
}

0 comments on commit 93b1390

Please sign in to comment.