AtomPublishingTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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-2015 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. * @see Zend_Feed_Entry_Atom
  24. */
  25. require_once 'Zend/Feed/Entry/Atom.php';
  26. /**
  27. * @see Zend_Http_Client_File
  28. */
  29. require_once 'Zend/Http/Client.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Feed
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Feed
  37. */
  38. class Zend_Feed_AtomPublishingTest extends PHPUnit_Framework_TestCase
  39. {
  40. protected $_uri;
  41. public function setUp()
  42. {
  43. $this->_uri = 'http://fubar.com/myFeed';
  44. }
  45. public function tearDown()
  46. {
  47. Zend_Feed::setHttpClient(new Zend_Http_Client());
  48. }
  49. public function testPost()
  50. {
  51. Zend_Feed::setHttpClient(new TestClient());
  52. $entry = new Zend_Feed_Entry_Atom();
  53. /* Give the entry its initial values. */
  54. $entry->title = 'Entry 1';
  55. $entry->content = '1.1';
  56. $entry->content['type'] = 'text';
  57. /* Do the initial post. The base feed URI is the same as the
  58. * POST URI, so just supply save() with that. */
  59. $entry->save($this->_uri);
  60. /* $entry will be filled in with any elements returned by the
  61. * server (id, updated, link rel="edit", etc). */
  62. $this->assertEquals('1', $entry->id(), 'Expected id to be 1');
  63. $this->assertEquals('Entry 1', $entry->title(), 'Expected title to be "Entry 1"');
  64. $this->assertEquals('1.1', $entry->content(), 'Expected content to be "1.1"');
  65. $this->assertEquals('text', $entry->content['type'], 'Expected content/type to be "text"');
  66. $this->assertEquals('2005-05-23T16:26:00-08:00', $entry->updated(), 'Expected updated date of 2005-05-23T16:26:00-08:00');
  67. $this->assertEquals('http://fubar.com/myFeed/1/1/', $entry->link('edit'), 'Expected edit URI of http://fubar.com/myFeed/1/1/');
  68. }
  69. public function testEdit()
  70. {
  71. Zend_Feed::setHttpClient(new TestClient());
  72. $contents = file_get_contents(dirname(__FILE__) . '/_files/AtomPublishingTest-before-update.xml');
  73. /* The base feed URI is the same as the POST URI, so just supply the
  74. * Zend_Feed_Entry_Atom object with that. */
  75. $entry = new Zend_Feed_Entry_Atom($this->_uri, $contents);
  76. /* Initial state. */
  77. $this->assertEquals('2005-05-23T16:26:00-08:00', $entry->updated(), 'Initial state of updated timestamp does not match');
  78. $this->assertEquals('http://fubar.com/myFeed/1/1/', $entry->link('edit'), 'Initial state of edit link does not match');
  79. /* Just change the entry's properties directly. */
  80. $entry->content = '1.2';
  81. /* Then save the changes. */
  82. $entry->save();
  83. /* New state. */
  84. $this->assertEquals('1.2', $entry->content(), 'Content change did not stick');
  85. $this->assertEquals('2005-05-23T16:27:00-08:00', $entry->updated(), 'New updated link is not correct');
  86. $this->assertEquals('http://fubar.com/myFeed/1/2/', $entry->link('edit'), 'New edit link is not correct');
  87. }
  88. }
  89. /**
  90. * A test wrapper around Zend_Http_Client, not actually performing
  91. * the request.
  92. *
  93. */
  94. class TestClient extends Zend_Http_Client
  95. {
  96. public function request($method = null)
  97. {
  98. $code = 400;
  99. $body = '';
  100. switch ($method) {
  101. case self::POST:
  102. $code = 201;
  103. $body = file_get_contents(dirname(__FILE__) . '/_files/AtomPublishingTest-created-entry.xml');
  104. break;
  105. case self::PUT:
  106. $doc1 = new DOMDocument();
  107. $doc1->load(dirname(__FILE__) . '/_files/AtomPublishingTest-expected-update.xml');
  108. $doc2 = new DOMDocument();
  109. $doc2->loadXML($this->raw_post_data);
  110. if ($doc1->saveXml() == $doc2->saveXml()) {
  111. $code = 200;
  112. $body = file_get_contents(dirname(__FILE__) . '/_files/AtomPublishingTest-updated-entry.xml');
  113. }
  114. break;
  115. default:
  116. break;
  117. }
  118. return new Zend_Http_Response($code, array(), $body);
  119. }
  120. }