UseResponseFileTagTest.php 3.1 KB

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