PageRenderTime 25ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Feed/ReaderTest.php

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