TestCase.php 5.2 KB

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