dotNotationUtilTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. final class dotNotationUtilTest extends \PHPUnit\Framework\TestCase {
  5. /**
  6. * @return Traversable<array{array<string,mixed>,string,string}>
  7. */
  8. public static function provideJsonDots(): Traversable {
  9. $json = <<<json
  10. {
  11. "hello": "world",
  12. "deeper": {
  13. "hello": "again"
  14. },
  15. "items": [
  16. {
  17. "meta": {"title": "first"}
  18. },
  19. {
  20. "meta": {"title": "second"}
  21. }
  22. ]
  23. }
  24. json;
  25. $array = json_decode($json, true);
  26. yield [$array, 'hello', 'world'];
  27. yield [$array, 'deeper.hello', 'again'];
  28. yield [$array, 'items.0.meta.title', 'first'];
  29. yield [$array, 'items[0].meta.title', 'first'];
  30. yield [$array, 'items.1.meta.title', 'second'];
  31. yield [$array, 'items[1].meta.title', 'second'];
  32. yield [$array, '"Hello " & hello & \'!\'', 'Hello world!'];
  33. yield [$array, '"Hello & goodbye " & hello & \'!\'', 'Hello & goodbye world!'];
  34. yield [$array, '"Hello " & hello & deeper.hello & "!"', 'Hello worldagain!'];
  35. }
  36. /**
  37. * @param array<string,mixed> $array
  38. */
  39. #[DataProvider('provideJsonDots')]
  40. public static function testJsonDots(array $array, string $key, string $expected): void {
  41. $value = FreshRSS_dotNotation_Util::get($array, $key);
  42. self::assertSame($expected, $value);
  43. }
  44. public static function testConvertJsonToRssEscapesCategories(): void {
  45. FreshRSS_Context::initSystem();
  46. Minz_Translate::init('en');
  47. $json = <<<json
  48. {
  49. "items": [
  50. {"title": "First", "url": "https://example.net/1", "categories": ["News & Politics", "Arts & Culture"]},
  51. {"title": "Second", "url": "https://example.net/2", "categories": "Science & Tech"}
  52. ]
  53. }
  54. json;
  55. $jf = json_decode($json, true);
  56. self::assertIsArray($jf);
  57. $rss = FreshRSS_dotNotation_Util::convertJsonToRss($jf, 'https://example.net/', [
  58. 'item' => 'items',
  59. 'itemTitle' => 'title',
  60. 'itemUri' => 'url',
  61. 'itemCategories' => 'categories',
  62. ], 'Test');
  63. self::assertIsString($rss);
  64. self::assertStringContainsString('<category>News &amp; Politics</category>', $rss);
  65. self::assertStringContainsString('<category>Arts &amp; Culture</category>', $rss);
  66. self::assertStringContainsString('<category>Science &amp; Tech</category>', $rss);
  67. self::assertNotFalse(simplexml_load_string($rss), 'Output must be valid XML');
  68. }
  69. public static function testConvertJsonToRssKeepsAttachments(): void {
  70. FreshRSS_Context::initSystem();
  71. Minz_Translate::init('en');
  72. $json = <<<json
  73. {
  74. "items": [
  75. {
  76. "title": "Episode 1",
  77. "url": "https://example.net/1",
  78. "attachments": [
  79. {"url": "https://example.net/1.mp3?a=1&b=2", "mime_type": "audio/mpeg", "size_in_bytes": 1234}
  80. ]
  81. }
  82. ]
  83. }
  84. json;
  85. $jf = json_decode($json, true);
  86. self::assertIsArray($jf);
  87. $rss = FreshRSS_dotNotation_Util::convertJsonToRss($jf, 'https://example.net/', [
  88. 'item' => 'items',
  89. 'itemTitle' => 'title',
  90. 'itemUri' => 'url',
  91. 'itemAttachment' => 'attachments',
  92. 'itemAttachmentUrl' => 'url',
  93. 'itemAttachmentType' => 'mime_type',
  94. 'itemAttachmentLength' => 'size_in_bytes',
  95. ], 'Test');
  96. self::assertIsString($rss);
  97. self::assertStringContainsString(
  98. '<media:content url="https://example.net/1.mp3?a=1&amp;b=2" type="audio/mpeg" length="1234">', $rss);
  99. self::assertNotFalse(simplexml_load_string($rss), 'Output must be valid XML');
  100. }
  101. }