2
0

TestCase.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_Service_Technorati
  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. * Exclude from code coverage report
  28. */
  29. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  30. /**
  31. * Patch for default timezone in PHP >= 5.1.0
  32. */
  33. if (!ini_get('date.timezone')) {
  34. date_default_timezone_set(@date_default_timezone_get());
  35. }
  36. /**
  37. * @see Zend_Service_Technorati
  38. */
  39. require_once 'Zend/Service/Technorati.php';
  40. /**
  41. * @category Zend
  42. * @package Zend_Service_Technorati
  43. * @subpackage UnitTests
  44. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class Zend_Service_Technorati_TestCase extends PHPUnit_Framework_TestCase
  48. {
  49. protected function _testConstruct($className, $args)
  50. {
  51. $reflection = new ReflectionClass($className);
  52. try {
  53. $object = $reflection->newInstanceArgs($args);
  54. $this->assertType($className, $object);
  55. } catch (Zend_Service_Technorati_Exception $e) {
  56. $this->fail("Exception " . $e->getMessage() . " thrown");
  57. }
  58. }
  59. protected function _testConstructThrowsExceptionWithInvalidDom($className, $match)
  60. {
  61. if (self::skipInvalidArgumentTypeTests()) {
  62. $this->markTestIncomplete('Failure to meet type hint results in fatal error in PHP < 5.2.0');
  63. return;
  64. }
  65. $reflection = new ReflectionClass($className);
  66. try {
  67. $object = $reflection->newInstanceArgs(array('foo'));
  68. $this->fail('Expected Zend_Service_Technorati_Exception not thrown');
  69. } catch (Exception $e) {
  70. $this->assertContains($match, $e->getMessage());
  71. }
  72. }
  73. protected function _testResultSetItemsInstanceOfResult($resultSetClassName, $args, $resultClassName)
  74. {
  75. $reflection = new ReflectionClass($resultSetClassName);
  76. $resultset = $reflection->newInstanceArgs($args);
  77. foreach ($resultset as $result) {
  78. $this->assertType($resultClassName, $result);
  79. }
  80. }
  81. protected function _testResultSetSerialization($resultSet)
  82. {
  83. $unobject = unserialize(serialize($resultSet));
  84. $unresult = null;
  85. $this->assertType(get_class($resultSet), $unobject);
  86. foreach ($resultSet as $index => $result) {
  87. try {
  88. $unobject->seek($index);
  89. $unresult = $unobject->current();
  90. } catch(OutOfBoundsException $e) {
  91. $this->fail("Missing result index $index");
  92. }
  93. $this->assertEquals($result, $unresult);
  94. }
  95. }
  96. protected function _testResultSerialization($result)
  97. {
  98. /**
  99. * Both Result and ResultSet objects includes variables
  100. * that references special objects such as DomDocuments.
  101. *
  102. * Unlike ResultSet(s), Result instances uses Dom fragments
  103. * only to construct the instance itself, then both Dom and Xpath objects
  104. * are no longer required.
  105. *
  106. * It means serializing a Result is not a painful job.
  107. * We don't need to implement any __wakeup or _sleep function
  108. * because PHP is able to create a perfect serialized snapshot
  109. * of current object status.
  110. *
  111. * Thought this situation makes our life easier, it's not safe
  112. * to assume things will not change in the future.
  113. * Testing each object now against a serialization request
  114. * makes this library more secure in the future!
  115. */
  116. $unresult = unserialize(serialize($result));
  117. $this->assertType(get_class($result), $unresult);
  118. $this->assertEquals($result, $unresult);
  119. }
  120. public static function getTestFilePath($file)
  121. {
  122. return dirname(__FILE__) . '/_files/' . $file;
  123. }
  124. public static function getTestFileContentAsDom($file)
  125. {
  126. $dom = new DOMDocument();
  127. $dom->load(self::getTestFilePath($file));
  128. return $dom;
  129. }
  130. public static function getTestFileElementsAsDom($file, $exp = '//item')
  131. {
  132. $dom = self::getTestFileContentAsDom($file);
  133. $xpath = new DOMXPath($dom);
  134. return $xpath->query($exp);
  135. }
  136. public static function getTestFileElementAsDom($file, $exp = '//item', $item = 0)
  137. {
  138. $dom = self::getTestFileContentAsDom($file);
  139. $xpath = new DOMXPath($dom);
  140. $domElements = $xpath->query($exp);
  141. return $domElements->item($item);
  142. }
  143. public static function skipInvalidArgumentTypeTests()
  144. {
  145. // PHP < 5.2.0 returns a fatal error
  146. // instead of a catchable Exception (ZF-2334)
  147. return version_compare(phpversion(), "5.2.0", "<");
  148. }
  149. }