Skip to content

Commit

Permalink
Unittest for swagger cli
Browse files Browse the repository at this point in the history
  • Loading branch information
bfanger committed Mar 13, 2015
1 parent ac1aab5 commit 82b6132
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
8 changes: 6 additions & 2 deletions bin/swagger
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ if (count($paths) === 0) {
$paths[] = getcwd();
}
if (class_exists('Swagger\Logger') === false) {
require_once(__DIR__.'/../vendor/autoload.php');
if (file_exists(__DIR__.'/../vendor/autoload.php')) { // cloned / dev environment?
require_once(__DIR__.'/../vendor/autoload.php');
} else {
require_once(realpath(__DIR__.'/../../../').'/autoload.php');
}
}
\Swagger\Logger::getInstance()->log = function ($entry, $type) {
$type = $type === E_USER_NOTICE ? 'INFO' : 'WARN';
Expand Down Expand Up @@ -126,7 +130,7 @@ error_log('----------------------'. str_repeat('-', strlen($counter)));
error_log($counter.' operations documented');
error_log('----------------------'. str_repeat('-', strlen($counter)));
if ($options['output']) {
file_put_contents($options['output'], $swagger);
$swagger->saveAs($options['output']);
} else {
echo $swagger;
error_log('');
Expand Down
1 change: 1 addition & 0 deletions src/Annotations/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class Parameter extends AbstractAnnotation {
'Swagger\Annotations\Delete',
'Swagger\Annotations\Patch',
'Swagger\Annotations\Path',
'Swagger\Annotations\Swagger'
];

public function validate($skip = array()) {
Expand Down
3 changes: 2 additions & 1 deletion src/Annotations/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class Response extends AbstractAnnotation {
'Swagger\Annotations\Post',
'Swagger\Annotations\Put',
'Swagger\Annotations\Patch',
'Swagger\Annotations\Delete'
'Swagger\Annotations\Delete',
'Swagger\Annotations\Swagger'
];

}
17 changes: 12 additions & 5 deletions src/Annotations/Swagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace Swagger\Annotations;

use Swagger\Logger;
use Swagger\Parser;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -74,13 +73,13 @@ class Swagger extends AbstractAnnotation {

/**
* An object to hold parameters that can be used across operations. This property does not define global parameters for all operations.
* @var array
* @var Parameter[]
*/
public $parameters;

/**
* An object to hold responses that can be used across operations. This property does not define global responses for all operations.
* @var array
* @var Response[]
*/
public $responses;

Expand All @@ -98,7 +97,7 @@ class Swagger extends AbstractAnnotation {

/**
* A list of tags used by the specification with additional metadata. The order of the tags can be used to reflect on their order by the parsing tools. Not all tags that are used by the Operation Object must be declared. The tags that are not declared may be organized randomly or based on the tools' logic. Each tag name in the list MUST be unique.
* @var array
* @var Tag[]
*/
public $tags;

Expand All @@ -121,6 +120,8 @@ class Swagger extends AbstractAnnotation {
'Swagger\Annotations\Delete' => 'paths[]',
'Swagger\Annotations\Definition' => 'definitions[]',
'Swagger\Annotations\Tag' => 'tags[]',
'Swagger\Annotations\Parameter' => 'parameters[]',
'Swagger\Annotations\Response' => 'responses[]',
];

/**
Expand All @@ -129,7 +130,7 @@ class Swagger extends AbstractAnnotation {
* @param string|array|Finder $directory
* @param string|array $exclude
*/
function crawl($directory, $exclude = null) {
public function crawl($directory, $exclude = null) {
// Setup Finder
if (is_object($directory)) {
$finder = $directory;
Expand All @@ -150,5 +151,11 @@ function crawl($directory, $exclude = null) {
$this->merge($parser->parseFile($file->getPathname()));
}
}

public function saveAs($filename) {
if (file_put_contents($filename, $this) === false) {
throw new Exception('Failed to saveAs("'.$filename.'")');
}
}

}
4 changes: 2 additions & 2 deletions tests/AbstractAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ function testInvalidField() {

function testUmergedAnnotation() {
$swagger = $this->createSwaggerWithInfo();
$swagger->merge($this->parseComment('@SWG\Parameter()'));
$this->assertSwaggerLogEntryStartsWith('Unexpected @SWG\Parameter(), expected to be inside @SWG\\');
$swagger->merge($this->parseComment('@SWG\Items()'));
$this->assertSwaggerLogEntryStartsWith('Unexpected @SWG\Items(), expected to be inside @SWG\\');
$swagger->validate();
}

Expand Down
38 changes: 38 additions & 0 deletions tests/CommandlineInterfaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* @license Apache 2.0
*/

namespace SwaggerTests;

class CommandlineInterfaceTest extends SwaggerTestCase {

public function testPipe() {
exec(__DIR__ . '/../bin/swagger ' . escapeshellarg(__DIR__ . '/../Examples/swagger-spec/petstore-simple') . ' 2> /dev/null', $output, $retval);
$this->assertSame(0, $retval);
$json = json_decode(implode("\n", $output));
$this->assertSame(JSON_ERROR_NONE, json_last_error());
$this->compareOutput($json);
}

public function testOutputTofile() {
$filename = sys_get_temp_dir() . '/swagger-php-clitest.json';
exec(__DIR__ . '/../bin/swagger -o ' . escapeshellarg($filename) . ' ' . escapeshellarg(__DIR__ . '/../Examples/swagger-spec/petstore-simple') . ' 2> /dev/null', $output, $retval);
$this->assertSame(0, $retval);
$this->assertCount(0, $output, 'No output to stdout');
$contents = file_get_contents($filename);
unlink($filename);
$json = json_decode($contents);
$this->assertSame(JSON_ERROR_NONE, json_last_error());
$this->compareOutput($json);
}

private function compareOutput($actual) {
$expected = json_decode(file_get_contents(__DIR__ . '/ExamplesOutput/petstore-simple.json'));
$expectedJson = json_encode($this->sorted($expected, 'petstore-simple.json'), JSON_PRETTY_PRINT);
$actualJson = json_encode($this->sorted($actual, 'Swagger CLI'), JSON_PRETTY_PRINT);
$this->assertEquals($expectedJson, $actualJson);
}

}

0 comments on commit 82b6132

Please sign in to comment.