TestCase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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-2009 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-2009 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. // This test is unnecessary. PHP type hinting is well tested, and will throw
  66. // catchable fatal errors on invalid argument types. Do nothing here.
  67. }
  68. protected function _testResultSetItemsInstanceOfResult($resultSetClassName, $args, $resultClassName)
  69. {
  70. $reflection = new ReflectionClass($resultSetClassName);
  71. $resultset = $reflection->newInstanceArgs($args);
  72. foreach ($resultset as $result) {
  73. $this->assertType($resultClassName, $result);
  74. }
  75. }
  76. protected function _testResultSetSerialization($resultSet)
  77. {
  78. $unobject = unserialize(serialize($resultSet));
  79. $unresult = null;
  80. $this->assertType(get_class($resultSet), $unobject);
  81. foreach ($resultSet as $index => $result) {
  82. try {
  83. $unobject->seek($index);
  84. $unresult = $unobject->current();
  85. } catch(OutOfBoundsException $e) {
  86. $this->fail("Missing result index $index");
  87. }
  88. $this->assertEquals($result, $unresult);
  89. }
  90. }
  91. protected function _testResultSerialization($result)
  92. {
  93. /**
  94. * Both Result and ResultSet objects includes variables
  95. * that references special objects such as DomDocuments.
  96. *
  97. * Unlike ResultSet(s), Result instances uses Dom fragments
  98. * only to construct the instance itself, then both Dom and Xpath objects
  99. * are no longer required.
  100. *
  101. * It means serializing a Result is not a painful job.
  102. * We don't need to implement any __wakeup or _sleep function
  103. * because PHP is able to create a perfect serialized snapshot
  104. * of current object status.
  105. *
  106. * Thought this situation makes our life easier, it's not safe
  107. * to assume things will not change in the future.
  108. * Testing each object now against a serialization request
  109. * makes this library more secure in the future!
  110. */
  111. $unresult = unserialize(serialize($result));
  112. $this->assertType(get_class($result), $unresult);
  113. $this->assertEquals($result, $unresult);
  114. }
  115. public static function getTestFilePath($file)
  116. {
  117. return dirname(__FILE__) . '/_files/' . $file;
  118. }
  119. public static function getTestFileContentAsDom($file)
  120. {
  121. $dom = new DOMDocument();
  122. $dom->load(self::getTestFilePath($file));
  123. return $dom;
  124. }
  125. public static function getTestFileElementsAsDom($file, $exp = '//item')
  126. {
  127. $dom = self::getTestFileContentAsDom($file);
  128. $xpath = new DOMXPath($dom);
  129. return $xpath->query($exp);
  130. }
  131. public static function getTestFileElementAsDom($file, $exp = '//item', $item = 0)
  132. {
  133. $dom = self::getTestFileContentAsDom($file);
  134. $xpath = new DOMXPath($dom);
  135. $domElements = $xpath->query($exp);
  136. return $domElements->item($item);
  137. }
  138. public static function skipInvalidArgumentTypeTests()
  139. {
  140. // PHP < 5.2.0 returns a fatal error
  141. // instead of a catchable Exception (ZF-2334)
  142. return version_compare(phpversion(), "5.2.0", "<");
  143. }
  144. }