ElggCoreUnitTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Elgg Core Unit Tester
  4. *
  5. * This class is to be extended by all Elgg unit tests. As such, any method here
  6. * will be available to the tests.
  7. */
  8. abstract class ElggCoreUnitTest extends UnitTestCase {
  9. /**
  10. * Class constructor.
  11. *
  12. * A simple wrapper to call the parent constructor.
  13. */
  14. public function __construct() {
  15. parent::__construct();
  16. }
  17. /**
  18. * Class destructor.
  19. *
  20. * The parent does not provide a destructor, so including an explicit one here.
  21. */
  22. public function __destruct() {
  23. }
  24. /**
  25. * Will trigger a pass if the two entity parameters have
  26. * the same "value" and same type. Otherwise a fail.
  27. *
  28. * @param mixed $first Entity to compare.
  29. * @param mixed $second Entity to compare.
  30. * @param string $message Message to display.
  31. * @return boolean
  32. */
  33. public function assertIdenticalEntities(\ElggEntity $first, \ElggEntity $second, $message = '%s') {
  34. if (!($res = $this->assertIsA($first, '\ElggEntity'))) {
  35. return $res;
  36. }
  37. if (!($res = $this->assertIsA($second, '\ElggEntity'))) {
  38. return $res;
  39. }
  40. if (!($res = $this->assertEqual(get_class($first), get_class($second)))) {
  41. return $res;
  42. }
  43. return $this->assert(new IdenticalEntityExpectation($first), $second, $message);
  44. }
  45. }
  46. /**
  47. * Test for identity.
  48. * @package SimpleTest
  49. * @subpackage UnitTester
  50. */
  51. class IdenticalEntityExpectation extends EqualExpectation {
  52. /**
  53. * Sets the value to compare against.
  54. *
  55. * @param mixed $value Test value to match.
  56. * @param string $message Customised message on failure.
  57. */
  58. public function __construct($value, $message = '%s') {
  59. parent::__construct($value, $message);
  60. }
  61. /**
  62. * Tests the expectation. True if it exactly matches the held value.
  63. *
  64. * @param mixed $compare Comparison value.
  65. * @return boolean
  66. */
  67. public function test($compare) {
  68. $value = $this->entityToFilteredArray($this->getValue());
  69. $compare = $this->entityToFilteredArray($compare);
  70. return SimpleTestCompatibility::isIdentical($value, $compare);
  71. }
  72. /**
  73. * Converts entity to array and filters not important attributes
  74. *
  75. * @param \ElggEntity $entity An entity to convert
  76. * @return array
  77. */
  78. protected function entityToFilteredArray($entity) {
  79. $skippedKeys = array('last_action');
  80. $array = (array)$entity;
  81. unset($array["\0*\0tables_loaded"]);
  82. foreach ($skippedKeys as $key) {
  83. // See: http://www.php.net/manual/en/language.types.array.php#language.types.array.casting
  84. $array["\0*\0attributes"][$key] = null;
  85. }
  86. ksort($array["\0*\0attributes"]);
  87. return $array;
  88. }
  89. /**
  90. * Returns a human readable test message.
  91. *
  92. * @param mixed $compare Comparison value.
  93. * @return string
  94. */
  95. public function testMessage($compare) {
  96. $dumper = $this->getDumper();
  97. $value2 = $this->entityToFilteredArray($this->getValue());
  98. $compare2 = $this->entityToFilteredArray($compare);
  99. if ($this->test($compare)) {
  100. return "Identical entity expectation [" . $dumper->describeValue($this->getValue()) . "]";
  101. } else {
  102. return "Identical entity expectation [" . $dumper->describeValue($this->getValue()) .
  103. "] fails with [" .
  104. $dumper->describeValue($compare) . "] " .
  105. $dumper->describeDifference($value2, $compare2, TYPE_MATTERS);
  106. }
  107. }
  108. }