Skip to content

Commit 7a6544c

Browse files
authored
Fix tests for windows (#1764)
1 parent 86ba87c commit 7a6544c

3 files changed

Lines changed: 37 additions & 10 deletions

File tree

tests/CommandlineTest.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,37 @@ class CommandlineTest extends OpenApiTestCase
1212
{
1313
use UsesExamples;
1414

15+
private function getCommandToExecute(string $cmd, ?string $devNullRedir = null): string
16+
{
17+
if (PHP_OS_FAMILY === 'Windows') {
18+
$cmd = 'php ' . $cmd;
19+
$devNull = 'NUL';
20+
} else {
21+
$devNull = '/dev/null';
22+
}
23+
if ($devNullRedir) {
24+
$cmd .= " $devNullRedir $devNull";
25+
}
26+
27+
return $cmd;
28+
}
29+
1530
public function testStdout(): void
1631
{
1732
$basePath = $this->examplePath('petstore');
1833
$path = "$basePath/annotations";
19-
exec(__DIR__ . '/../bin/openapi --bootstrap ' . __DIR__ . '/cl_bootstrap.php --format yaml ' . escapeshellarg($path) . ' 2> /dev/null', $output, $retval);
34+
exec($this->getCommandToExecute(__DIR__ . '/../bin/openapi --bootstrap ' . __DIR__ . '/cl_bootstrap.php --format yaml ' . escapeshellarg($path), '2>'), $output, $retval);
2035
$this->assertSame(0, $retval, implode(PHP_EOL, $output));
2136
$yaml = implode(PHP_EOL, $output);
2237
$this->assertSpecEquals(file_get_contents($this->getSpecFilename('petstore')), $yaml);
2338
}
2439

25-
public function testOutputTofile(): void
40+
public function testOutputToFile(): void
2641
{
2742
$basePath = $this->examplePath('petstore');
2843
$path = "$basePath/annotations";
2944
$filename = sys_get_temp_dir() . '/swagger-php-clitest.yaml';
30-
exec(__DIR__ . '/../bin/openapi --bootstrap ' . __DIR__ . '/cl_bootstrap.php --format yaml -o ' . escapeshellarg($filename) . ' ' . escapeshellarg($path) . ' 2> /dev/null', $output, $retval);
45+
exec($this->getCommandToExecute(__DIR__ . '/../bin/openapi --bootstrap ' . __DIR__ . '/cl_bootstrap.php --format yaml -o ' . escapeshellarg($filename) . ' ' . escapeshellarg($path), '2>'), $output, $retval);
3146
$this->assertSame(0, $retval, implode(PHP_EOL, $output));
3247
$this->assertCount(0, $output, 'No output to stdout');
3348
$yaml = file_get_contents($filename);
@@ -40,15 +55,15 @@ public function testAddProcessor(): void
4055
$basePath = $this->examplePath('petstore');
4156
$path = "$basePath/annotations";
4257
$cmd = __DIR__ . '/../bin/openapi --bootstrap ' . __DIR__ . '/cl_bootstrap.php --processor OperationId --format yaml ' . escapeshellarg($path);
43-
exec($cmd . ' 2> /dev/null', $output, $retval);
58+
exec($this->getCommandToExecute($cmd, '2>'), $output, $retval);
4459
$this->assertSame(0, $retval, $cmd . PHP_EOL . implode(PHP_EOL, $output));
4560
}
4661

4762
public function testExcludeListWarning(): void
4863
{
4964
$basePath = $this->examplePath('petstore');
5065
$path = "$basePath/annotations";
51-
exec(__DIR__ . '/../bin/openapi -e foo,bar ' . escapeshellarg($path) . ' 2>&1', $output, $retval);
66+
exec($this->getCommandToExecute(__DIR__ . '/../bin/openapi -e foo,bar ' . escapeshellarg($path) . ' 2>&1'), $output, $retval);
5267
$this->assertSame(1, $retval);
5368
$output = implode(PHP_EOL, $output);
5469
$this->assertStringContainsString('Comma-separated exclude paths are deprecated', $output);
@@ -58,7 +73,7 @@ public function testMissingArg(): void
5873
{
5974
$basePath = $this->examplePath('petstore');
6075
$path = "$basePath/annotations";
61-
exec(__DIR__ . '/../bin/openapi ' . escapeshellarg($path) . ' -e 2>&1', $output, $retval);
76+
exec($this->getCommandToExecute(__DIR__ . '/../bin/openapi ' . escapeshellarg($path) . ' -e 2>&1'), $output, $retval);
6277
$this->assertSame(1, $retval);
6378
$output = implode(PHP_EOL, $output);
6479
$this->assertStringContainsString('Error: Missing argument for "-e"', $output);

tests/ContextTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testDebugLocation(): void
5858

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

tests/UtilTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,25 @@ public function testFinder(): void
3131
$finder = (new Finder())->in($this->examplePath('using-traits/annotations'));
3232
$this->assertGreaterThan(0, iterator_count($finder), 'There should be at least a few files and a directory.');
3333
$finder_array = \iterator_to_array($finder);
34-
$directory_path = $this->examplePath('using-traits/annotations/Decoration');
35-
$this->assertArrayHasKey($directory_path, $finder_array, 'The directory should be a path in the finder.');
34+
$normalize = static function (string $path): string {
35+
return str_replace('\\', '/', $path);
36+
};
37+
$directory_path = $normalize($this->examplePath('using-traits/annotations/Decoration'));
38+
$normalizePathKeys = static function ($paths) use ($normalize) {
39+
return \array_combine(
40+
\array_map(
41+
$normalize,
42+
\array_keys($paths)
43+
),
44+
\array_values($paths)
45+
);
46+
};
47+
$this->assertArrayHasKey($directory_path, $normalizePathKeys($finder_array), 'The directory should be a path in the finder.');
3648
// Use the Util method that should set the finder to only find files, since swagger-php only needs files.
3749
$finder_result = Util::finder($finder);
3850
$this->assertGreaterThan(0, iterator_count($finder_result), 'There should be at least a few file paths.');
3951
$finder_result_array = \iterator_to_array($finder_result);
40-
$this->assertArrayNotHasKey($directory_path, $finder_result_array, 'The directory should not be a path in the finder.');
52+
$this->assertArrayNotHasKey($directory_path, $normalizePathKeys($finder_result_array), 'The directory should not be a path in the finder.');
4153
}
4254

4355
public static function shortenFixtures(): iterable

0 commit comments

Comments
 (0)