CommandlineInterfaceTest.php 1.7 KB

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