UseResponseFileTagTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\Responses;
  3. use Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseFileTag;
  4. use Knuckles\Scribe\ScribeServiceProvider;
  5. use Knuckles\Scribe\Tools\DocumentationConfig;
  6. use Mpociot\Reflection\DocBlock\Tag;
  7. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  8. use Orchestra\Testbench\TestCase;
  9. class UseResponseFileTagTest extends TestCase
  10. {
  11. use ArraySubsetAsserts;
  12. protected function getPackageProviders($app)
  13. {
  14. $providers = [
  15. ScribeServiceProvider::class,
  16. ];
  17. if (class_exists(\Dingo\Api\Provider\LaravelServiceProvider::class)) {
  18. $providers[] = \Dingo\Api\Provider\LaravelServiceProvider::class;
  19. }
  20. return $providers;
  21. }
  22. /**
  23. * @test
  24. * @dataProvider responseFileTags
  25. */
  26. public function allows_multiple_responsefile_tags_for_multiple_statuses_and_scenarios(array $tags, array $expected)
  27. {
  28. $filePath = __DIR__ . '/../../Fixtures/response_test.json';
  29. $filePath2 = __DIR__ . '/../../Fixtures/response_error_test.json';
  30. copy($filePath, storage_path('response_test.json'));
  31. copy($filePath2, storage_path('response_error_test.json'));
  32. $strategy = new UseResponseFileTag(new DocumentationConfig([]));
  33. $results = $strategy->getFileResponses($tags);
  34. $this->assertArraySubset([
  35. [
  36. 'status' => 200,
  37. 'description' => $expected[0]['description'],
  38. 'content' => file_get_contents($filePath),
  39. ],
  40. [
  41. 'status' => 401,
  42. 'description' => $expected[1]['description'],
  43. 'content' => file_get_contents($filePath2),
  44. ],
  45. ], $results);
  46. unlink(storage_path('response_test.json'));
  47. unlink(storage_path('response_error_test.json'));
  48. }
  49. /** @test */
  50. public function can_add_or_replace_key_value_pair_in_response_file()
  51. {
  52. $filePath = __DIR__ . '/../../Fixtures/response_test.json';
  53. copy($filePath, storage_path('response_test.json'));
  54. $strategy = new UseResponseFileTag(new DocumentationConfig([]));
  55. $tags = [
  56. new Tag('responseFile', 'response_test.json {"message" : "Serendipity", "gender": "male"}'),
  57. ];
  58. $results = $strategy->getFileResponses($tags);
  59. $this->assertArraySubset([
  60. [
  61. 'status' => 200,
  62. 'content' => '{"id":5,"name":"Jessica Jones","gender":"male","message":"Serendipity"}',
  63. ],
  64. ], $results);
  65. }
  66. public function responseFileTags()
  67. {
  68. return [
  69. "with status as initial position" => [
  70. [
  71. new Tag('responseFile', 'response_test.json'),
  72. new Tag('responseFile', '401 response_error_test.json'),
  73. ],
  74. [
  75. [
  76. 'status' => 200,
  77. 'description' => '200',
  78. ],
  79. [
  80. 'status' => 401,
  81. 'description' => '401',
  82. ],
  83. ],
  84. ],
  85. "with attributes" => [
  86. [
  87. new Tag('responseFile', 'scenario="success" response_test.json'),
  88. new Tag('responseFile', 'status=401 scenario=\'auth problem\' response_error_test.json'),
  89. ],
  90. [
  91. [
  92. 'status' => 200,
  93. 'description' => '200, success',
  94. ],
  95. [
  96. 'status' => 401,
  97. 'description' => '401, auth problem',
  98. ],
  99. ],
  100. ],
  101. ];
  102. }
  103. }