AtomPublishingTest.php 4.9 KB

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