Advertisement
Guest User

object.php

a guest
Mar 27th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. /* Testing
  5. /*
  6. /* Testing operators: <, >, ==, ===
  7. /* Testing type: object
  8. /*
  9. /* Expected behaviour:
  10. /* Based on [1] quote "Built-in classes can define its own comparison, different classes are uncomparable, same class - compare properties the same way as arrays (PHP 4), PHP 5 has its own explanation", the expected behaviour for < and > would be the same as for arrays pretty much.
  11. /* The expected behaviour for == and === are explained here [2], quote == -> "Two object instances are equal if they have the same attributes and values, and are instances of the same class." and === -> "object variables are identical if and only if they refer to the same instance of the same class.".
  12. /*
  13. /* Actual behaviour:
  14. /* The actual behaviour is as descired in the manual.
  15. /*
  16. /*
  17. /* Links:
  18. /* [1]: http://php.net/manual/en/language.operators.comparison.php
  19. /* [2]: http://php.net/manual/en/language.oop5.object-comparison.php#language.oop5.object-comparison
  20. /*
  21. */
  22.  
  23.  
  24.  
  25.  
  26.  
  27. /**
  28. /*
  29. /* Testing operators: < and >
  30. /*
  31. */
  32.  
  33. //Test case
  34. //Variations: amount, values and keys (order)
  35. //Test count: 10
  36. // Failed: 0
  37. // Passed: 10
  38. {
  39. //Test case 1.1
  40. $a = (object)["a" => 1];
  41. $b = (object)["a" => 1];
  42.  
  43. //Passed
  44. var_dump("Same amount of elements, keys and values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  45.  
  46. //Test case 1.2
  47. $a = (object)["a" => 1];
  48. $b = (object)["a" => 1, "b" => 1];
  49.  
  50. //Passed
  51. var_dump("NOT same amount of elements, but same values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  52.  
  53. //Test case 1.3
  54. $a = (object)["a" => 10];
  55. $b = (object)["a" => 1, "b" => 1];
  56.  
  57. //Passed
  58. var_dump("NOT same amount of elements nor values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  59.  
  60. //Test case 1.4
  61. $a = (object)["a" => 1];
  62. $b = (object)["b" => 1];
  63.  
  64. //Passed
  65. var_dump("Same amount of element and values, NOT same keys: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  66.  
  67. //Test case 1.5
  68. $a = (object)["a" => 10];
  69. $b = (object)["a" => 1];
  70.  
  71. //Passed
  72. var_dump("Same amount of elements and keys, NOT same values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  73.  
  74. //Test case 1.6
  75. $a = (object)["a" => 1, "b" => 1];
  76. $b = (object)["b" => 1, "a" => 1];
  77.  
  78. //Passed
  79. var_dump("Same amount of elements and keys in different order: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  80.  
  81. //Test case 1.7
  82. $a = (object)["a" => 1, "b" => 5];
  83. $b = (object)["b" => 5];
  84.  
  85. //Passed
  86. var_dump("Same values, NOT same amount of elements nor keys: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  87.  
  88. //Test case 1.8
  89. $a = (object)["c" => 1];
  90. $b = (object)["a" => 10];
  91.  
  92. //Passed
  93. var_dump("NOT same keys nor values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  94.  
  95. //Test case 1.9
  96. $a = (object)["a" => 1, "b" => 1];
  97. $b = (object)["b" => 10, "a" => 1];
  98.  
  99. //Passed
  100. var_dump("Same amount of elements and values, NOT same keys nor order: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  101.  
  102. //Test case 1.10
  103. class A {public $a = 1;}
  104. $a = new A;
  105. class B {public $a = 1;}
  106. $b = new B;
  107.  
  108. //Passed
  109. var_dump("Same amount of elements and values and keys, but different not built-in class: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  110.  
  111. }
  112.  
  113.  
  114. echo PHP_EOL . PHP_EOL . PHP_EOL; //Test case separator
  115.  
  116. /**
  117. /*
  118. /* Test case end
  119. /*
  120. */
  121.  
  122.  
  123.  
  124.  
  125.  
  126. /**
  127. /*
  128. /* Testing operators: == and ===
  129. /*
  130. */
  131.  
  132. //Test case
  133. //Variations: amount, values and keys (order)
  134. //Test count: 7
  135. // Failed: 0
  136. // Passed: 7
  137. {
  138. //Test case 2.1
  139. $a = (object)["a" => 1];
  140. $b = (object)["a" => 1];
  141.  
  142. //Passed
  143. var_dump("Same amount of elements, values and keys: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  144.  
  145. //Test case 2.2
  146. $a = (object)["a" => 1];
  147. $b = (object)["a" => 10, "b" => 1];
  148.  
  149. //Passed
  150. var_dump("NOT same amount of elements, but same values: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  151.  
  152. //Test case 2.3
  153. $a = (object)["a" => 10];
  154. $b = (object)["a" => 1];
  155.  
  156. //Passed
  157. var_dump("Same amount of elements, but not values: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  158.  
  159. //Test case 2.4
  160. $a = (object)["a" => 1];
  161. $b = (object)["b" => 1];
  162.  
  163. //Passed
  164. var_dump("Same amount of elements and values, but not keys: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  165.  
  166. //Test case 2.5
  167. $a = (object)["a" => 1, "b" => 2];
  168. $b = (object)["b" => 2, "a" => 1];
  169.  
  170. //Passed
  171. var_dump("Same amount of elements, key and values, but different order: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  172.  
  173. //Test case 2.6
  174. class C {public $a = 1;}
  175. $a = new A;
  176. class D {public $a = 1;}
  177. $b = new B;
  178.  
  179. //Passed
  180. var_dump("Same amount of elements and values and keys, but different not built-in class: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  181.  
  182. //Test case 2.7
  183. $a = (object)["a" => 1];
  184. $b = $a;
  185.  
  186. //Passed
  187. var_dump("Same exact instance: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  188.  
  189. }
  190.  
  191.  
  192. echo PHP_EOL . PHP_EOL . PHP_EOL; //Test case separator
  193.  
  194. /**
  195. /*
  196. /* Test case end
  197. /*
  198. */
  199.  
  200.  
  201.  
  202. //NULL, TRUE, FALSE 2 str func
  203. function bool2str($v){if($v === NULL)return "NULL";elseif($v === FALSE)return "FALSE";elseif($v === TRUE)return "TRUE";else return "UNEXPECTED: '$v'";}
  204.  
  205. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement