AtomPublishingTest.php 4.9 KB

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