FeedTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Test for feed helper
  4. *
  5. * @group ko7
  6. * @group ko7.core
  7. * @group ko7.core.feed
  8. *
  9. * @package KO7
  10. * @category Tests
  11. *
  12. * @author Jeremy Bush <contractfrombelow@gmail.com>
  13. * @copyright (c) 2007-2016 Kohana Team
  14. * @copyright (c) since 2016 Koseven Team
  15. * @license https://koseven.dev/LICENSE
  16. */
  17. class KO7_FeedTest extends Unittest_TestCase
  18. {
  19. /**
  20. * Sets up the environment
  21. */
  22. // @codingStandardsIgnoreStart
  23. public function setUp(): void
  24. // @codingStandardsIgnoreEnd
  25. {
  26. parent::setUp();
  27. KO7::$config->load('url')->set('trusted_hosts', ['localhost']);
  28. }
  29. /**
  30. * Provides test data for test_parse()
  31. *
  32. * @return array
  33. */
  34. public function provider_parse()
  35. {
  36. return [
  37. // $source, $expected
  38. [realpath(__DIR__.'/../test_data/feeds/activity.atom'), ['Proposals (Political/Workflow) #4839 (New)', 'Proposals (Political/Workflow) #4782']],
  39. [realpath(__DIR__.'/../test_data/feeds/example.rss20'), ['Example entry']],
  40. ];
  41. }
  42. /**
  43. * Tests that Feed::parse gets the correct number of elements
  44. *
  45. * @test
  46. * @dataProvider provider_parse
  47. * @covers feed::parse
  48. * @param string $source URL to test
  49. * @param integer $expected Count of items
  50. */
  51. public function test_parse($source, $expected_titles)
  52. {
  53. $titles = [];
  54. foreach (Feed::parse($source) as $item)
  55. {
  56. $titles[] = $item['title'];
  57. }
  58. $this->assertSame($expected_titles, $titles);
  59. }
  60. /**
  61. * Provides test data for test_create()
  62. *
  63. * @return array
  64. */
  65. public function provider_create()
  66. {
  67. $info = ['pubDate' => 123, 'image' => ['link' => 'http://koseven.dev/image.png', 'url' => 'http://koseven.dev/', 'title' => 'title']];
  68. return [
  69. // $source, $expected
  70. [$info, ['foo' => ['foo' => 'bar', 'pubDate' => 123, 'link' => 'foo']], ['_SERVER' => ['HTTP_HOST' => 'localhost']+$_SERVER],
  71. [
  72. 'tag' => 'channel',
  73. 'descendant' => [
  74. 'tag' => 'item',
  75. 'child' => [
  76. 'tag' => 'foo',
  77. 'content' => 'bar'
  78. ]
  79. ]
  80. ],
  81. [
  82. $this->matcher_composer($info, 'image', 'link'),
  83. $this->matcher_composer($info, 'image', 'url'),
  84. $this->matcher_composer($info, 'image', 'title')
  85. ]
  86. ],
  87. ];
  88. }
  89. /**
  90. * Helper for handy matcher composing
  91. *
  92. * @param array $data
  93. * @param string $tag
  94. * @param string $child
  95. * @return array
  96. */
  97. private function matcher_composer($data, $tag, $child)
  98. {
  99. return [
  100. 'tag' => 'channel',
  101. 'descendant' => [
  102. 'tag' => $tag,
  103. 'child' => [
  104. 'tag' => $child,
  105. 'content' => $data[$tag][$child]
  106. ]
  107. ]
  108. ];
  109. }
  110. /**
  111. * @test
  112. *
  113. * @dataProvider provider_create
  114. *
  115. * @covers feed::create
  116. *
  117. * @param string $info info to pass
  118. * @param integer $items items to add
  119. * @param integer $matcher output
  120. */
  121. public function test_create($info, $items, $enviroment, $matcher_item, $matchers_image)
  122. {
  123. $this->setEnvironment($enviroment);
  124. $this->assertTag($matcher_item, Feed::create($info, $items), '', FALSE);
  125. foreach ($matchers_image as $matcher_image)
  126. {
  127. $this->assertTag($matcher_image, Feed::create($info, $items), '', FALSE);
  128. }
  129. }
  130. }