TestCase.php 5.1 KB

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