diff --git a/tests/CommandlineTest.php b/tests/CommandlineTest.php index 0d94b8f5f..0b2289602 100644 --- a/tests/CommandlineTest.php +++ b/tests/CommandlineTest.php @@ -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); @@ -40,7 +55,7 @@ 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)); } @@ -48,7 +63,7 @@ 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); @@ -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); diff --git a/tests/ContextTest.php b/tests/ContextTest.php index 2fd363244..e1c75d494 100644 --- a/tests/ContextTest.php +++ b/tests/ContextTest.php @@ -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() ); diff --git a/tests/UtilTest.php b/tests/UtilTest.php index ca4107608..f8ec669bc 100644 --- a/tests/UtilTest.php +++ b/tests/UtilTest.php @@ -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