AnnotationParserTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Knuckles\Scribe\Tests\BaseUnitTest;
  4. use Knuckles\Scribe\Tools\AnnotationParser;
  5. class AnnotationParserTest extends BaseUnitTest
  6. {
  7. /**
  8. * @test
  9. * @dataProvider annotationsWithContentAndFields
  10. */
  11. public function can_parse_annotation_into_content_and_fields(string $annotation, array $expected)
  12. {
  13. $result = AnnotationParser::parseIntoContentAndFields($annotation, ['status', 'scenario']);
  14. $this->assertEquals($expected, $result);
  15. }
  16. public static function annotationsWithContentAndFields()
  17. {
  18. return [
  19. "when fields come first" => [
  20. 'status=400 scenario="things go wrong" {"message": "failed"}',
  21. [
  22. 'fields' => ['status' => '400', 'scenario' => 'things go wrong'],
  23. 'content' => '{"message": "failed"}',
  24. ],
  25. ],
  26. "when fields come last" => [
  27. '{"message": "failed"} status=400 scenario="things go wrong"',
  28. [
  29. 'fields' => ['status' => '400', 'scenario' => 'things go wrong'],
  30. 'content' => '{"message": "failed"}',
  31. ],
  32. ],
  33. "when there are no fields" => [
  34. '{"message": "failed"} ',
  35. [
  36. 'fields' => ['status' => null, 'scenario' => null],
  37. 'content' => '{"message": "failed"}',
  38. ],
  39. ],
  40. "when there are some fields" => [
  41. ' status=hey {"message": "failed"} ',
  42. [
  43. 'fields' => ['status' => 'hey', 'scenario' => null],
  44. 'content' => '{"message": "failed"}',
  45. ],
  46. ],
  47. ];
  48. }
  49. /**
  50. * @test
  51. * @dataProvider annotationsWithFields
  52. */
  53. public function can_parse_annotation_into_fields(string $annotation, array $expected)
  54. {
  55. $result = AnnotationParser::parseIntoFields($annotation);
  56. $this->assertEquals($expected, $result);
  57. }
  58. public static function annotationsWithFields()
  59. {
  60. return [
  61. "with or without quotes" => [
  62. 'title=This message="everything good" "dummy field"="dummy data", "snaked_data"=value',
  63. [
  64. 'title' => 'This',
  65. 'message' => "everything good",
  66. 'dummy field' => 'dummy data',
  67. 'snaked_data' => 'value'
  68. ]
  69. ],
  70. "no fields" => [
  71. '{"message": "failed"}',
  72. []
  73. ],
  74. "fields with empty values" => [
  75. 'title= message="everything good"',
  76. [
  77. 'message' => 'everything good'
  78. ]
  79. ]
  80. ];
  81. }
  82. }