TestCase.php 5.3 KB

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