Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions tests/CommandlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,37 @@ class CommandlineTest extends OpenApiTestCase
{
use UsesExamples;

private function getCommandToExecute(string $cmd, ?string $devNullRedir = null): string
{
if (PHP_OS_FAMILY === 'Windows') {
$cmd = 'php ' . $cmd;
$devNull = 'NUL';
} else {
$devNull = '/dev/null';
}
if ($devNullRedir) {
$cmd .= " $devNullRedir $devNull";
}

return $cmd;
}

public function testStdout(): void
{
$basePath = $this->examplePath('petstore');
$path = "$basePath/annotations";
exec(__DIR__ . '/../bin/openapi --bootstrap ' . __DIR__ . '/cl_bootstrap.php --format yaml ' . escapeshellarg($path) . ' 2> /dev/null', $output, $retval);
exec($this->getCommandToExecute(__DIR__ . '/../bin/openapi --bootstrap ' . __DIR__ . '/cl_bootstrap.php --format yaml ' . escapeshellarg($path), '2>'), $output, $retval);
$this->assertSame(0, $retval, implode(PHP_EOL, $output));
$yaml = implode(PHP_EOL, $output);
$this->assertSpecEquals(file_get_contents($this->getSpecFilename('petstore')), $yaml);
}

public function testOutputTofile(): void
public function testOutputToFile(): void
{
$basePath = $this->examplePath('petstore');
$path = "$basePath/annotations";
$filename = sys_get_temp_dir() . '/swagger-php-clitest.yaml';
exec(__DIR__ . '/../bin/openapi --bootstrap ' . __DIR__ . '/cl_bootstrap.php --format yaml -o ' . escapeshellarg($filename) . ' ' . escapeshellarg($path) . ' 2> /dev/null', $output, $retval);
exec($this->getCommandToExecute(__DIR__ . '/../bin/openapi --bootstrap ' . __DIR__ . '/cl_bootstrap.php --format yaml -o ' . escapeshellarg($filename) . ' ' . escapeshellarg($path), '2>'), $output, $retval);
$this->assertSame(0, $retval, implode(PHP_EOL, $output));
$this->assertCount(0, $output, 'No output to stdout');
$yaml = file_get_contents($filename);
Expand All @@ -40,15 +55,15 @@ public function testAddProcessor(): void
$basePath = $this->examplePath('petstore');
$path = "$basePath/annotations";
$cmd = __DIR__ . '/../bin/openapi --bootstrap ' . __DIR__ . '/cl_bootstrap.php --processor OperationId --format yaml ' . escapeshellarg($path);
exec($cmd . ' 2> /dev/null', $output, $retval);
exec($this->getCommandToExecute($cmd, '2>'), $output, $retval);
$this->assertSame(0, $retval, $cmd . PHP_EOL . implode(PHP_EOL, $output));
}

public function testExcludeListWarning(): void
{
$basePath = $this->examplePath('petstore');
$path = "$basePath/annotations";
exec(__DIR__ . '/../bin/openapi -e foo,bar ' . escapeshellarg($path) . ' 2>&1', $output, $retval);
exec($this->getCommandToExecute(__DIR__ . '/../bin/openapi -e foo,bar ' . escapeshellarg($path) . ' 2>&1'), $output, $retval);
$this->assertSame(1, $retval);
$output = implode(PHP_EOL, $output);
$this->assertStringContainsString('Comma-separated exclude paths are deprecated', $output);
Expand All @@ -58,7 +73,7 @@ public function testMissingArg(): void
{
$basePath = $this->examplePath('petstore');
$path = "$basePath/annotations";
exec(__DIR__ . '/../bin/openapi ' . escapeshellarg($path) . ' -e 2>&1', $output, $retval);
exec($this->getCommandToExecute(__DIR__ . '/../bin/openapi ' . escapeshellarg($path) . ' -e 2>&1'), $output, $retval);
$this->assertSame(1, $retval);
$output = implode(PHP_EOL, $output);
$this->assertStringContainsString('Error: Missing argument for "-e"', $output);
Expand Down
2 changes: 1 addition & 1 deletion tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testDebugLocation(): void

$customerSchema = $openapi->components->schemas[0];
$this->assertStringContainsString(
'Fixtures/Customer.php on line ',
'Fixtures' . DIRECTORY_SEPARATOR . 'Customer.php on line ',
$customerSchema->_context->getDebugLocation()
);

Expand Down
18 changes: 15 additions & 3 deletions tests/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,25 @@ public function testFinder(): void
$finder = (new Finder())->in($this->examplePath('using-traits/annotations'));
$this->assertGreaterThan(0, iterator_count($finder), 'There should be at least a few files and a directory.');
$finder_array = \iterator_to_array($finder);
$directory_path = $this->examplePath('using-traits/annotations/Decoration');
$this->assertArrayHasKey($directory_path, $finder_array, 'The directory should be a path in the finder.');
$normalize = static function (string $path): string {
return str_replace('\\', '/', $path);
};
$directory_path = $normalize($this->examplePath('using-traits/annotations/Decoration'));
$normalizePathKeys = static function ($paths) use ($normalize) {
return \array_combine(
\array_map(
$normalize,
\array_keys($paths)
),
\array_values($paths)
);
};
$this->assertArrayHasKey($directory_path, $normalizePathKeys($finder_array), 'The directory should be a path in the finder.');
// Use the Util method that should set the finder to only find files, since swagger-php only needs files.
$finder_result = Util::finder($finder);
$this->assertGreaterThan(0, iterator_count($finder_result), 'There should be at least a few file paths.');
$finder_result_array = \iterator_to_array($finder_result);
$this->assertArrayNotHasKey($directory_path, $finder_result_array, 'The directory should not be a path in the finder.');
$this->assertArrayNotHasKey($directory_path, $normalizePathKeys($finder_result_array), 'The directory should not be a path in the finder.');
}

public static function shortenFixtures(): iterable
Expand Down