PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Feed/ReaderTest.php

https://github.com/mridgway/zf2
PHP | 373 lines | 289 code | 37 blank | 47 comment | 16 complexity | da52df9ddea399daaf72b74aa4aeec13 MD5 | raw file
  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-2010 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. /**
  23. * @namespace
  24. */
  25. namespace ZendTest\Feed;
  26. use Zend\Feed\Reader;
  27. /**
  28. * @category Zend
  29. * @package Zend_Feed
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Feed
  34. * @group Zend_Feed_Reader
  35. */
  36. class ReaderTest extends \PHPUnit_Framework_TestCase
  37. {
  38. protected $_feedSamplePath = null;
  39. public function setup()
  40. {
  41. $this->_feedSamplePath = __DIR__ . '/Reader/_files';
  42. }
  43. public function tearDown()
  44. {
  45. Reader\Reader::reset();
  46. }
  47. public function testDetectsFeedIsRss20()
  48. {
  49. $feed = Reader\Reader::importString(
  50. file_get_contents($this->_feedSamplePath.'/Reader/rss20.xml'));
  51. $type = Reader\Reader::detectType($feed);
  52. $this->assertEquals(Reader\Reader::TYPE_RSS_20, $type);
  53. }
  54. public function testDetectsFeedIsRss094()
  55. {
  56. $feed = Reader\Reader::importString(
  57. file_get_contents($this->_feedSamplePath.'/Reader/rss094.xml'));
  58. $type = Reader\Reader::detectType($feed);
  59. $this->assertEquals(Reader\Reader::TYPE_RSS_094, $type);
  60. }
  61. public function testDetectsFeedIsRss093()
  62. {
  63. $feed = Reader\Reader::importString(
  64. file_get_contents($this->_feedSamplePath.'/Reader/rss093.xml'));
  65. $type = Reader\Reader::detectType($feed);
  66. $this->assertEquals(Reader\Reader::TYPE_RSS_093, $type);
  67. }
  68. public function testDetectsFeedIsRss092()
  69. {
  70. $feed = Reader\Reader::importString(
  71. file_get_contents($this->_feedSamplePath.'/Reader/rss092.xml'));
  72. $type = Reader\Reader::detectType($feed);
  73. $this->assertEquals(Reader\Reader::TYPE_RSS_092, $type);
  74. }
  75. public function testDetectsFeedIsRss091()
  76. {
  77. $feed = Reader\Reader::importString(
  78. file_get_contents($this->_feedSamplePath.'/Reader/rss091.xml'));
  79. $type = Reader\Reader::detectType($feed);
  80. $this->assertEquals(Reader\Reader::TYPE_RSS_091, $type);
  81. }
  82. public function testDetectsFeedIsRss10()
  83. {
  84. $feed = Reader\Reader::importString(
  85. file_get_contents($this->_feedSamplePath.'/Reader/rss10.xml'));
  86. $type = Reader\Reader::detectType($feed);
  87. $this->assertEquals(Reader\Reader::TYPE_RSS_10, $type);
  88. }
  89. public function testDetectsFeedIsRss090()
  90. {
  91. $feed = Reader\Reader::importString(
  92. file_get_contents($this->_feedSamplePath.'/Reader/rss090.xml'));
  93. $type = Reader\Reader::detectType($feed);
  94. $this->assertEquals(Reader\Reader::TYPE_RSS_090, $type);
  95. }
  96. public function testDetectsFeedIsAtom10()
  97. {
  98. $feed = Reader\Reader::importString(
  99. file_get_contents($this->_feedSamplePath.'/Reader/atom10.xml'));
  100. $type = Reader\Reader::detectType($feed);
  101. $this->assertEquals(Reader\Reader::TYPE_ATOM_10, $type);
  102. }
  103. public function testDetectsFeedIsAtom03()
  104. {
  105. $feed = Reader\Reader::importString(
  106. file_get_contents($this->_feedSamplePath.'/Reader/atom03.xml'));
  107. $type = Reader\Reader::detectType($feed);
  108. $this->assertEquals(Reader\Reader::TYPE_ATOM_03, $type);
  109. }
  110. /**
  111. * @group ZF-9723
  112. */
  113. public function testDetectsTypeFromStringOrToRemindPaddyAboutForgettingATestWhichLetsAStupidTypoSurviveUnnoticedForMonths()
  114. {
  115. $feed = '<?xml version="1.0" encoding="utf-8" ?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/"><channel></channel></rdf:RDF>';
  116. $type = Reader\Reader::detectType($feed);
  117. $this->assertEquals(Reader\Reader::TYPE_RSS_10, $type);
  118. }
  119. public function testGetEncoding()
  120. {
  121. $feed = Reader\Reader::importString(
  122. file_get_contents(__DIR__ . '/Reader/Entry/_files/Atom/title/plain/atom10.xml')
  123. );
  124. $this->assertEquals('utf-8', $feed->getEncoding());
  125. $this->assertEquals('utf-8', $feed->current()->getEncoding());
  126. }
  127. public function testImportsFile()
  128. {
  129. try {
  130. $feed = Reader\Reader::importFile(
  131. __DIR__ . '/Reader/Entry/_files/Atom/title/plain/atom10.xml'
  132. );
  133. } catch(\Exception $e) {
  134. $this->fail($e->getMessage());
  135. }
  136. }
  137. public function testImportsUri()
  138. {
  139. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  140. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  141. ) {
  142. $this->markTestSkipped('testImportsUri() requires a network connection');
  143. return;
  144. }
  145. try {
  146. $feed = Reader\Reader::import('http://www.planet-php.net/rdf/');
  147. } catch(\Exception $e) {
  148. $this->fail($e->getMessage());
  149. }
  150. }
  151. /**
  152. * @group ZF-8328
  153. */
  154. public function testImportsUriAndThrowsExceptionIfNotAFeed()
  155. {
  156. $this->setExpectedException('Zend\Feed\Exception');
  157. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  158. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  159. ) {
  160. $this->markTestSkipped('testImportsUri() requires a network connection');
  161. return;
  162. }
  163. $feed = Reader\Reader::import('http://twitter.com/alganet');
  164. }
  165. public function testGetsFeedLinksAsValueObject()
  166. {
  167. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  168. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  169. ) {
  170. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  171. return;
  172. }
  173. try {
  174. $links = Reader\Reader::findFeedLinks('http://www.planet-php.net');
  175. } catch(\Exception $e) {
  176. $this->fail($e->getMessage());
  177. }
  178. $this->assertEquals('http://www.planet-php.org/rss/', $links->rss);
  179. }
  180. public function testCompilesLinksAsArrayObject()
  181. {
  182. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  183. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  184. ) {
  185. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  186. return;
  187. }
  188. $links = Reader\Reader::findFeedLinks('http://www.planet-php.net');
  189. $this->assertTrue($links instanceof Reader\FeedSet);
  190. $this->assertEquals(array(
  191. 'rel' => 'alternate', 'type' => 'application/rss+xml', 'href' => 'http://www.planet-php.org/rss/'
  192. ), (array) $links->getIterator()->current());
  193. }
  194. public function testFeedSetLoadsFeedObjectWhenFeedArrayKeyAccessed()
  195. {
  196. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  197. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  198. ) {
  199. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  200. return;
  201. }
  202. $links = Reader\Reader::findFeedLinks('http://www.planet-php.net');
  203. $link = $links->getIterator()->current();
  204. $this->assertTrue($link['feed'] instanceof \Zend\Feed\Reader\Feed\RSS);
  205. }
  206. public function testZeroCountFeedSetReturnedFromEmptyList()
  207. {
  208. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  209. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  210. ) {
  211. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  212. return;
  213. }
  214. $links = Reader\Reader::findFeedLinks('http://www.example.com');
  215. $this->assertEquals(0, count($links));
  216. }
  217. /**
  218. * @group ZF-8327
  219. */
  220. public function testGetsFeedLinksAndTrimsNewlines()
  221. {
  222. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  223. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  224. ) {
  225. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  226. return;
  227. }
  228. try {
  229. $links = Reader\Reader::findFeedLinks('http://www.infopod.com.br');
  230. } catch(\Exception $e) {
  231. $this->fail($e->getMessage());
  232. }
  233. $this->assertEquals('http://feeds.feedburner.com/jonnyken/infoblog', $links->rss);
  234. }
  235. /**
  236. * @group ZF-8330
  237. */
  238. public function testGetsFeedLinksAndNormalisesRelativeUrls()
  239. {
  240. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  241. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  242. ) {
  243. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  244. return;
  245. }
  246. try {
  247. $links = Reader\Reader::findFeedLinks('http://meiobit.com');
  248. } catch(\Exception $e) {
  249. $this->fail($e->getMessage());
  250. }
  251. $this->assertEquals('http://meiobit.com/rss.xml', $links->rss);
  252. }
  253. /**
  254. * @group ZF-8330
  255. */
  256. public function testGetsFeedLinksAndNormalisesRelativeUrlsOnUriWithPath()
  257. {
  258. try {
  259. $currClient = Reader\Reader::getHttpClient();
  260. $testAdapter = new \Zend\Http\Client\Adapter\Test();
  261. $testAdapter->setResponse(new \Zend\Http\Response(200, array(), '<!DOCTYPE html><html><head><link rel="alternate" type="application/rss+xml" href="../test.rss"><link rel="alternate" type="application/atom+xml" href="/test.atom"></head><body></body></html>'));
  262. Reader\Reader::setHttpClient(new \Zend\Http\Client(null, array('adapter' => $testAdapter)));
  263. $links = Reader\Reader::findFeedLinks('http://foo/bar');
  264. Reader\Reader::setHttpClient($currClient);
  265. } catch(\Exception $e) {
  266. $this->fail($e->getMessage());
  267. }
  268. $this->assertEquals('http://foo/test.rss', $links->rss);
  269. $this->assertEquals('http://foo/test.atom', $links->atom);
  270. }
  271. public function testAddsPrefixPath()
  272. {
  273. Reader\Reader::addPrefixPath('A\B\C', '/A/B/C');
  274. $prefixPaths = Reader\Reader::getPluginLoader()->getPaths();
  275. $this->assertEquals('/A/B/C/', $prefixPaths['A\B\C\\'][0]);
  276. }
  277. public function testRegistersUserExtension()
  278. {
  279. try {
  280. Reader\Reader::addPrefixPath('My\Extension', __DIR__ . '/Reader/_files/My/Extension');
  281. Reader\Reader::registerExtension('JungleBooks');
  282. } catch(\Exception $e) {
  283. $this->fail($e->getMessage());
  284. }
  285. $this->assertTrue(Reader\Reader::isRegistered('JungleBooks'));
  286. }
  287. protected function _getTempDirectory()
  288. {
  289. $tmpdir = array();
  290. foreach (array($_ENV, $_SERVER) as $tab) {
  291. foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
  292. if (isset($tab[$key])) {
  293. if (($key == 'windir') or ($key == 'SystemRoot')) {
  294. $dir = realpath($tab[$key] . '\\temp');
  295. } else {
  296. $dir = realpath($tab[$key]);
  297. }
  298. if ($this->_isGoodTmpDir($dir)) {
  299. return $dir;
  300. }
  301. }
  302. }
  303. }
  304. if (function_exists('sys_get_temp_dir')) {
  305. $dir = sys_get_temp_dir();
  306. if ($this->_isGoodTmpDir($dir)) {
  307. return $dir;
  308. }
  309. }
  310. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  311. if ($tempFile) {
  312. $dir = realpath(dirname($tempFile));
  313. unlink($tempFile);
  314. if ($this->_isGoodTmpDir($dir)) {
  315. return $dir;
  316. }
  317. }
  318. if ($this->_isGoodTmpDir('/tmp')) {
  319. return '/tmp';
  320. }
  321. if ($this->_isGoodTmpDir('\\temp')) {
  322. return '\\temp';
  323. }
  324. }
  325. protected function _isGoodTmpDir($dir)
  326. {
  327. if (is_readable($dir) && is_writable($dir)) {
  328. return true;
  329. }
  330. return false;
  331. }
  332. }