4
0

CommandlineInterfaceTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApiTests;
  6. class CommandlineInterfaceTest extends OpenApiTestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. parent::setUp();
  11. }
  12. public function testStdout()
  13. {
  14. exec(__DIR__.'/../bin/openapi --format json '.escapeshellarg(__DIR__.'/../Examples/swagger-spec/petstore-simple').' 2> /dev/null', $output, $retval);
  15. $this->assertSame(0, $retval);
  16. $json = json_decode(implode("\n", $output));
  17. $this->assertSame(JSON_ERROR_NONE, json_last_error());
  18. $this->compareOutput($json);
  19. }
  20. public function testOutputTofile()
  21. {
  22. $filename = sys_get_temp_dir().'/swagger-php-clitest.json';
  23. exec(__DIR__.'/../bin/openapi --format json -o '.escapeshellarg($filename).' '.escapeshellarg(__DIR__.'/../Examples/swagger-spec/petstore-simple').' 2> /dev/null', $output, $retval);
  24. $this->assertSame(0, $retval);
  25. $this->assertCount(0, $output, 'No output to stdout');
  26. $contents = file_get_contents($filename);
  27. unlink($filename);
  28. $json = json_decode($contents);
  29. $this->assertSame(JSON_ERROR_NONE, json_last_error());
  30. $this->compareOutput($json);
  31. }
  32. private function compareOutput($actual)
  33. {
  34. $expected = json_decode(file_get_contents(__DIR__.'/ExamplesOutput/petstore-simple.json'));
  35. $expectedJson = json_encode($this->sorted($expected, 'petstore-simple.json'), JSON_PRETTY_PRINT);
  36. $actualJson = json_encode($this->sorted($actual, 'Swagger CLI'), JSON_PRETTY_PRINT);
  37. $this->assertEquals($expectedJson, $actualJson);
  38. }
  39. }