FeedTest.php 3.0 KB

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