ReaderTest.php 9.5 KB

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