dotNotationUtilTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. try {
  68. $dom = Dom\XMLDocument::createFromString($rss);
  69. } catch (Throwable $ex) { // @phpstan-ignore catch.neverThrown (Dom\XMLDocument::createFromString() does throw)
  70. self::fail('Output must be valid XML: ' . $ex->getMessage());
  71. }
  72. self::assertInstanceOf(Dom\XMLDocument::class, $dom);
  73. }
  74. public static function testConvertJsonToRssKeepsAttachments(): void {
  75. FreshRSS_Context::initSystem();
  76. Minz_Translate::init('en');
  77. $json = <<<json
  78. {
  79. "items": [
  80. {
  81. "title": "Episode 1",
  82. "url": "https://example.net/1",
  83. "attachments": [
  84. {"url": "https://example.net/1.mp3?a=1&b=2", "mime_type": "audio/mpeg", "size_in_bytes": 1234}
  85. ]
  86. }
  87. ]
  88. }
  89. json;
  90. $jf = json_decode($json, true);
  91. self::assertIsArray($jf);
  92. $rss = FreshRSS_dotNotation_Util::convertJsonToRss($jf, 'https://example.net/', [
  93. 'item' => 'items',
  94. 'itemTitle' => 'title',
  95. 'itemUri' => 'url',
  96. 'itemAttachment' => 'attachments',
  97. 'itemAttachmentUrl' => 'url',
  98. 'itemAttachmentType' => 'mime_type',
  99. 'itemAttachmentLength' => 'size_in_bytes',
  100. ], 'Test');
  101. self::assertIsString($rss);
  102. self::assertStringContainsString(
  103. '<media:content url="https://example.net/1.mp3?a=1&amp;b=2" type="audio/mpeg" length="1234">', $rss);
  104. try {
  105. $dom = Dom\XMLDocument::createFromString($rss);
  106. } catch (Throwable $ex) { // @phpstan-ignore catch.neverThrown (the XML parser throws on malformed documents, not disclosed in the PHP stubs)
  107. self::fail('Output must be valid XML: ' . $ex->getMessage());
  108. }
  109. self::assertInstanceOf(Dom\XMLDocument::class, $dom);
  110. }
  111. }