2
0

ReaderTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. require_once 'PHPUnit/Framework/TestCase.php';
  3. require_once 'Zend/Feed/Reader.php';
  4. require_once 'Zend/Cache.php';
  5. class Zend_Feed_ReaderTest extends PHPUnit_Framework_TestCase
  6. {
  7. protected $_feedSamplePath = null;
  8. public function setup()
  9. {
  10. $this->_feedSamplePath = dirname(__FILE__) . '/Reader/_files';
  11. }
  12. public function tearDown()
  13. {
  14. Zend_Feed_Reader::reset();
  15. }
  16. public function testDetectsFeedIsRss20()
  17. {
  18. $feed = Zend_Feed_Reader::importString(
  19. file_get_contents($this->_feedSamplePath.'/Reader/rss20.xml'));
  20. $type = Zend_Feed_Reader::detectType($feed);
  21. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_20, $type);
  22. }
  23. public function testDetectsFeedIsRss094()
  24. {
  25. $feed = Zend_Feed_Reader::importString(
  26. file_get_contents($this->_feedSamplePath.'/Reader/rss094.xml'));
  27. $type = Zend_Feed_Reader::detectType($feed);
  28. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_094, $type);
  29. }
  30. public function testDetectsFeedIsRss093()
  31. {
  32. $feed = Zend_Feed_Reader::importString(
  33. file_get_contents($this->_feedSamplePath.'/Reader/rss093.xml'));
  34. $type = Zend_Feed_Reader::detectType($feed);
  35. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_093, $type);
  36. }
  37. public function testDetectsFeedIsRss092()
  38. {
  39. $feed = Zend_Feed_Reader::importString(
  40. file_get_contents($this->_feedSamplePath.'/Reader/rss092.xml'));
  41. $type = Zend_Feed_Reader::detectType($feed);
  42. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_092, $type);
  43. }
  44. public function testDetectsFeedIsRss091()
  45. {
  46. $feed = Zend_Feed_Reader::importString(
  47. file_get_contents($this->_feedSamplePath.'/Reader/rss091.xml'));
  48. $type = Zend_Feed_Reader::detectType($feed);
  49. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_091, $type);
  50. }
  51. public function testDetectsFeedIsRss10()
  52. {
  53. $feed = Zend_Feed_Reader::importString(
  54. file_get_contents($this->_feedSamplePath.'/Reader/rss10.xml'));
  55. $type = Zend_Feed_Reader::detectType($feed);
  56. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_10, $type);
  57. }
  58. public function testDetectsFeedIsRss090()
  59. {
  60. $feed = Zend_Feed_Reader::importString(
  61. file_get_contents($this->_feedSamplePath.'/Reader/rss090.xml'));
  62. $type = Zend_Feed_Reader::detectType($feed);
  63. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_090, $type);
  64. }
  65. public function testDetectsFeedIsAtom10()
  66. {
  67. $feed = Zend_Feed_Reader::importString(
  68. file_get_contents($this->_feedSamplePath.'/Reader/atom10.xml'));
  69. $type = Zend_Feed_Reader::detectType($feed);
  70. $this->assertEquals(Zend_Feed_Reader::TYPE_ATOM_10, $type);
  71. }
  72. public function testDetectsFeedIsAtom03()
  73. {
  74. $feed = Zend_Feed_Reader::importString(
  75. file_get_contents($this->_feedSamplePath.'/Reader/atom03.xml'));
  76. $type = Zend_Feed_Reader::detectType($feed);
  77. $this->assertEquals(Zend_Feed_Reader::TYPE_ATOM_03, $type);
  78. }
  79. public function testGetEncoding()
  80. {
  81. $feed = Zend_Feed_Reader::importString(
  82. file_get_contents(dirname(__FILE__) . '/Reader/Entry/_files/Atom/title/plain/atom10.xml')
  83. );
  84. $this->assertEquals('utf-8', $feed->getEncoding());
  85. $this->assertEquals('utf-8', $feed->current()->getEncoding());
  86. }
  87. public function testImportsFile()
  88. {
  89. try {
  90. $feed = Zend_Feed_Reader::importFile(
  91. dirname(__FILE__) . '/Reader/Entry/_files/Atom/title/plain/atom10.xml'
  92. );
  93. } catch(Exception $e) {
  94. $this->fail($e->getMessage());
  95. }
  96. }
  97. public function testImportsUri()
  98. {
  99. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  100. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  101. ) {
  102. $this->markTestSkipped('testImportsUri() requires a network connection');
  103. return;
  104. }
  105. try {
  106. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  107. } catch(Exception $e) {
  108. $this->fail($e->getMessage());
  109. }
  110. }
  111. public function testGetsFeedLinksAsValueObject()
  112. {
  113. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  114. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  115. ) {
  116. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  117. return;
  118. }
  119. try {
  120. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  121. } catch(Exception $e) {
  122. $this->fail($e->getMessage());
  123. }
  124. $this->assertEquals('http://www.planet-php.org/rss/', $links->rss);
  125. }
  126. public function testAddsPrefixPath()
  127. {
  128. Zend_Feed_Reader::addPrefixPath('A_B_C', '/A/B/C');
  129. $prefixPaths = Zend_Feed_Reader::getPluginLoader()->getPaths();
  130. $this->assertEquals('/A/B/C/', $prefixPaths['A_B_C_'][0]);
  131. }
  132. public function testRegistersUserExtension()
  133. {
  134. try {
  135. Zend_Feed_Reader::addPrefixPath('My_FeedReader_Extension',dirname(__FILE__) . '/Reader/_files/My/Extension');
  136. Zend_Feed_Reader::registerExtension('JungleBooks');
  137. } catch(Exception $e) {
  138. $this->fail($e->getMessage());
  139. }
  140. $this->assertTrue(Zend_Feed_Reader::isRegistered('JungleBooks'));
  141. }
  142. /** Test condition for ZF-7486 (Could Not Replicate) **/
  143. public function testRepeatedFeedImportsWithCacheEnabledDirectOrIndirectDoNotCreateARedeclarationOfAbstractClassFatalError()
  144. {
  145. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  146. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  147. ) {
  148. $this->markTestSkipped('testRepeatedFeedImportsDoNotCreateARedeclarationOfAbstractClassFatalError() requires a network connection');
  149. return;
  150. }
  151. $frontendOptions = array(
  152. 'lifetime' => 7200,
  153. 'automatic_serialization' => true
  154. );
  155. $backendOptions = array(
  156. 'cache_dir' => $this->_getTempDirectory()
  157. );
  158. $cache = Zend_Cache::factory('Core','File',$frontendOptions,$backendOptions);
  159. Zend_Feed_Reader::setCache($cache);
  160. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rss/');
  161. $cache->save($feed, 'feed1');
  162. $feed2 = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  163. $cache->save($feed, 'feed2');
  164. $feed3 = Zend_Feed_Reader::import('http://www.planet-php.net/atom/');
  165. $cache->save($feed, 'feed3');
  166. $feed4 = Zend_Feed_Reader::import('http://www.phpdeveloper.org/feed');
  167. $cache->save($feed, 'feed4');
  168. $feedFromCache = $cache->load('feed4');
  169. $this->assertTrue($feedFromCache instanceof Zend_Feed_Reader_FeedAbstract);
  170. }
  171. protected function _getTempDirectory()
  172. {
  173. $tmpdir = array();
  174. foreach (array($_ENV, $_SERVER) as $tab) {
  175. foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
  176. if (isset($tab[$key])) {
  177. if (($key == 'windir') or ($key == 'SystemRoot')) {
  178. $dir = realpath($tab[$key] . '\\temp');
  179. } else {
  180. $dir = realpath($tab[$key]);
  181. }
  182. if ($this->_isGoodTmpDir($dir)) {
  183. return $dir;
  184. }
  185. }
  186. }
  187. }
  188. if (function_exists('sys_get_temp_dir')) {
  189. $dir = sys_get_temp_dir();
  190. if ($this->_isGoodTmpDir($dir)) {
  191. return $dir;
  192. }
  193. }
  194. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  195. if ($tempFile) {
  196. $dir = realpath(dirname($tempFile));
  197. unlink($tempFile);
  198. if ($this->_isGoodTmpDir($dir)) {
  199. return $dir;
  200. }
  201. }
  202. if ($this->_isGoodTmpDir('/tmp')) {
  203. return '/tmp';
  204. }
  205. if ($this->_isGoodTmpDir('\\temp')) {
  206. return '\\temp';
  207. }
  208. }
  209. protected function _isGoodTmpDir($dir)
  210. {
  211. if (is_readable($dir) && is_writable($dir)) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. }