ReaderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 testCompilesLinksAsArrayObject()
  156. {
  157. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  158. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  159. ) {
  160. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  161. return;
  162. }
  163. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  164. $this->assertTrue($links instanceof Zend_Feed_Reader_FeedSet);
  165. $this->assertEquals(array(
  166. 'rel' => 'alternate', 'type' => 'application/rss+xml', 'href' => 'http://www.planet-php.org/rss/'
  167. ), (array) $links->getIterator()->current());
  168. }
  169. public function testFeedSetLoadsFeedObjectWhenFeedArrayKeyAccessed()
  170. {
  171. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  172. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  173. ) {
  174. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  175. return;
  176. }
  177. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  178. $link = $links->getIterator()->current();
  179. $this->assertTrue($link['feed'] instanceof Zend_Feed_Reader_Feed_Rss);
  180. }
  181. public function testZeroCountFeedSetReturnedFromEmptyList()
  182. {
  183. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  184. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  185. ) {
  186. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  187. return;
  188. }
  189. $links = Zend_Feed_Reader::findFeedLinks('http://www.example.com');
  190. $this->assertEquals(0, count($links));
  191. }
  192. public function testAddsPrefixPath()
  193. {
  194. Zend_Feed_Reader::addPrefixPath('A_B_C', '/A/B/C');
  195. $prefixPaths = Zend_Feed_Reader::getPluginLoader()->getPaths();
  196. $this->assertEquals('/A/B/C/', $prefixPaths['A_B_C_'][0]);
  197. }
  198. public function testRegistersUserExtension()
  199. {
  200. try {
  201. Zend_Feed_Reader::addPrefixPath('My_FeedReader_Extension',dirname(__FILE__) . '/Reader/_files/My/Extension');
  202. Zend_Feed_Reader::registerExtension('JungleBooks');
  203. } catch(Exception $e) {
  204. $this->fail($e->getMessage());
  205. }
  206. $this->assertTrue(Zend_Feed_Reader::isRegistered('JungleBooks'));
  207. }
  208. /** Test condition for ZF-7486 (Could Not Replicate) **/
  209. public function testRepeatedFeedImportsWithCacheEnabledDirectOrIndirectDoNotCreateARedeclarationOfAbstractClassFatalError()
  210. {
  211. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  212. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  213. ) {
  214. $this->markTestSkipped('testRepeatedFeedImportsDoNotCreateARedeclarationOfAbstractClassFatalError() requires a network connection');
  215. return;
  216. }
  217. $frontendOptions = array(
  218. 'lifetime' => 7200,
  219. 'automatic_serialization' => true
  220. );
  221. $backendOptions = array(
  222. 'cache_dir' => $this->_getTempDirectory()
  223. );
  224. $cache = Zend_Cache::factory('Core','File',$frontendOptions,$backendOptions);
  225. Zend_Feed_Reader::setCache($cache);
  226. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rss/');
  227. $cache->save($feed, 'feed1');
  228. $feed2 = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  229. $cache->save($feed, 'feed2');
  230. $feed3 = Zend_Feed_Reader::import('http://www.planet-php.net/atom/');
  231. $cache->save($feed, 'feed3');
  232. $feed4 = Zend_Feed_Reader::import('http://www.phpdeveloper.org/feed');
  233. $cache->save($feed, 'feed4');
  234. $feedXmlFromCache = $cache->load('Zend_Feed_Reader_' . md5('http://www.planet-php.net/rss/'));
  235. $feedFromCache = Zend_Feed_Reader::importString($feedXmlFromCache);
  236. $this->assertTrue($feedFromCache instanceof Zend_Feed_Reader_FeedAbstract);
  237. $feedFromCache2 = $cache->load('feed4');
  238. $this->assertTrue($feedFromCache2 instanceof Zend_Feed_Reader_FeedAbstract);
  239. }
  240. protected function _getTempDirectory()
  241. {
  242. $tmpdir = array();
  243. foreach (array($_ENV, $_SERVER) as $tab) {
  244. foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
  245. if (isset($tab[$key])) {
  246. if (($key == 'windir') or ($key == 'SystemRoot')) {
  247. $dir = realpath($tab[$key] . '\\temp');
  248. } else {
  249. $dir = realpath($tab[$key]);
  250. }
  251. if ($this->_isGoodTmpDir($dir)) {
  252. return $dir;
  253. }
  254. }
  255. }
  256. }
  257. if (function_exists('sys_get_temp_dir')) {
  258. $dir = sys_get_temp_dir();
  259. if ($this->_isGoodTmpDir($dir)) {
  260. return $dir;
  261. }
  262. }
  263. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  264. if ($tempFile) {
  265. $dir = realpath(dirname($tempFile));
  266. unlink($tempFile);
  267. if ($this->_isGoodTmpDir($dir)) {
  268. return $dir;
  269. }
  270. }
  271. if ($this->_isGoodTmpDir('/tmp')) {
  272. return '/tmp';
  273. }
  274. if ($this->_isGoodTmpDir('\\temp')) {
  275. return '\\temp';
  276. }
  277. }
  278. protected function _isGoodTmpDir($dir)
  279. {
  280. if (is_readable($dir) && is_writable($dir)) {
  281. return true;
  282. }
  283. return false;
  284. }
  285. }