SwaggerFactoryTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * File SwaggerFactoryTest.php
  4. *
  5. * @author Edward Pfremmer <epfremme@nerdery.com>
  6. */
  7. namespace Epfremme\Swagger\Tests\Factory;
  8. use JMS\Serializer\Serializer;
  9. use Epfremme\Swagger\Entity\Swagger;
  10. use Epfremme\Swagger\Factory\SwaggerFactory;
  11. use Epfremme\Swagger\Tests\Parser\SwaggerParserTest;
  12. /**
  13. * Class SwaggerFactoryTest
  14. *
  15. * @package Epfremme\Swagger
  16. * @subpackage Tests\Parser
  17. */
  18. class SwaggerFactoryTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var SwaggerFactory
  22. */
  23. protected static $factory;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function setUpBeforeClass()
  28. {
  29. self::$factory = new SwaggerFactory();
  30. }
  31. /**
  32. * Return the swagger factory
  33. *
  34. * @return SwaggerFactory
  35. */
  36. public function getFactory()
  37. {
  38. if (!self::$factory) self::setUpBeforeClass();
  39. return self::$factory;
  40. }
  41. /**
  42. * Return full file path
  43. *
  44. * @param string $filename
  45. * @return string
  46. */
  47. protected function getFile($filename)
  48. {
  49. return realpath(__DIR__ . '/../Resources/' . $filename);
  50. }
  51. /**
  52. * @covers Epfremme\Swagger\Factory\SwaggerFactory::__construct
  53. */
  54. public function testFactoryConstructor()
  55. {
  56. $factory = new SwaggerFactory();
  57. $this->assertInstanceOf(SwaggerFactory::class, $factory);
  58. $this->assertAttributeInstanceOf(Serializer::class, 'serializer', $factory);
  59. }
  60. /**
  61. * @expectedException \InvalidArgumentException
  62. */
  63. public function testBuildMissing()
  64. {
  65. $this->getFactory()->build($this->getFile(SwaggerParserTest::SWAGGER_MISSING_FILE));
  66. }
  67. /**
  68. * @expectedException \Epfremme\Swagger\Exception\InvalidVersionException
  69. */
  70. public function testBuildUnsupportedVersion()
  71. {
  72. $this->getFactory()->build($this->getFile(SwaggerParserTest::SWAGGER_V1_FILE));
  73. }
  74. /**
  75. * @covers Epfremme\Swagger\Factory\SwaggerFactory::build
  76. */
  77. public function testBuildJson()
  78. {
  79. $swagger = $this->getFactory()->build($this->getFile(SwaggerParserTest::SWAGGER_JSON_FILE));
  80. $this->assertInstanceOf(Swagger::class, $swagger);
  81. }
  82. /**
  83. * @covers Epfremme\Swagger\Factory\SwaggerFactory::build
  84. */
  85. public function testBuildYaml()
  86. {
  87. $swagger = $this->getFactory()->build($this->getFile(SwaggerParserTest::SWAGGER_YAML_FILE));
  88. $this->assertInstanceOf(Swagger::class, $swagger);
  89. }
  90. /**
  91. * @covers Epfremme\Swagger\Factory\SwaggerFactory::serialize
  92. */
  93. public function testSerialize()
  94. {
  95. $swagger = $this->getFactory()->build($this->getFile(SwaggerParserTest::SWAGGER_JSON_FILE));
  96. $json = $this->getFactory()->serialize($swagger);
  97. $this->assertJson($json);
  98. $this->assertJsonStringEqualsJsonString(file_get_contents($this->getFile(SwaggerParserTest::SWAGGER_JSON_FILE)), $json);
  99. }
  100. /**
  101. * @expectedException \Epfremme\Swagger\Exception\InvalidVersionException
  102. */
  103. public function testSerializeUnsupportedVersion()
  104. {
  105. $swagger = $this->getFactory()->build($this->getFile(SwaggerParserTest::SWAGGER_JSON_FILE));
  106. $swagger->setVersion('1.0');
  107. $this->getFactory()->serialize($swagger);
  108. }
  109. }