| 148 |
lars |
1 |
<?php
|
|
|
2 |
namespace Hamcrest\Core;
|
|
|
3 |
|
|
|
4 |
class DummyToStringClass
|
|
|
5 |
{
|
|
|
6 |
private $_arg;
|
|
|
7 |
|
|
|
8 |
public function __construct($arg)
|
|
|
9 |
{
|
|
|
10 |
$this->_arg = $arg;
|
|
|
11 |
}
|
|
|
12 |
|
|
|
13 |
public function __toString()
|
|
|
14 |
{
|
|
|
15 |
return $this->_arg;
|
|
|
16 |
}
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
class IsEqualTest extends \Hamcrest\AbstractMatcherTest
|
|
|
20 |
{
|
|
|
21 |
|
|
|
22 |
protected function createMatcher()
|
|
|
23 |
{
|
|
|
24 |
return \Hamcrest\Core\IsEqual::equalTo('irrelevant');
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
public function testComparesObjectsUsingEqualityOperator()
|
|
|
28 |
{
|
|
|
29 |
assertThat("hi", equalTo("hi"));
|
|
|
30 |
assertThat("bye", not(equalTo("hi")));
|
|
|
31 |
|
|
|
32 |
assertThat(1, equalTo(1));
|
|
|
33 |
assertThat(1, not(equalTo(2)));
|
|
|
34 |
|
|
|
35 |
assertThat("2", equalTo(2));
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function testCanCompareNullValues()
|
|
|
39 |
{
|
|
|
40 |
assertThat(null, equalTo(null));
|
|
|
41 |
|
|
|
42 |
assertThat(null, not(equalTo('hi')));
|
|
|
43 |
assertThat('hi', not(equalTo(null)));
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function testComparesTheElementsOfAnArray()
|
|
|
47 |
{
|
|
|
48 |
$s1 = array('a', 'b');
|
|
|
49 |
$s2 = array('a', 'b');
|
|
|
50 |
$s3 = array('c', 'd');
|
|
|
51 |
$s4 = array('a', 'b', 'c', 'd');
|
|
|
52 |
|
|
|
53 |
assertThat($s1, equalTo($s1));
|
|
|
54 |
assertThat($s2, equalTo($s1));
|
|
|
55 |
assertThat($s3, not(equalTo($s1)));
|
|
|
56 |
assertThat($s4, not(equalTo($s1)));
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public function testComparesTheElementsOfAnArrayOfPrimitiveTypes()
|
|
|
60 |
{
|
|
|
61 |
$i1 = array(1, 2);
|
|
|
62 |
$i2 = array(1, 2);
|
|
|
63 |
$i3 = array(3, 4);
|
|
|
64 |
$i4 = array(1, 2, 3, 4);
|
|
|
65 |
|
|
|
66 |
assertThat($i1, equalTo($i1));
|
|
|
67 |
assertThat($i2, equalTo($i1));
|
|
|
68 |
assertThat($i3, not(equalTo($i1)));
|
|
|
69 |
assertThat($i4, not(equalTo($i1)));
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public function testRecursivelyTestsElementsOfArrays()
|
|
|
73 |
{
|
|
|
74 |
$i1 = array(array(1, 2), array(3, 4));
|
|
|
75 |
$i2 = array(array(1, 2), array(3, 4));
|
|
|
76 |
$i3 = array(array(5, 6), array(7, 8));
|
|
|
77 |
$i4 = array(array(1, 2, 3, 4), array(3, 4));
|
|
|
78 |
|
|
|
79 |
assertThat($i1, equalTo($i1));
|
|
|
80 |
assertThat($i2, equalTo($i1));
|
|
|
81 |
assertThat($i3, not(equalTo($i1)));
|
|
|
82 |
assertThat($i4, not(equalTo($i1)));
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
public function testIncludesTheResultOfCallingToStringOnItsArgumentInTheDescription()
|
|
|
86 |
{
|
|
|
87 |
$argumentDescription = 'ARGUMENT DESCRIPTION';
|
|
|
88 |
$argument = new \Hamcrest\Core\DummyToStringClass($argumentDescription);
|
|
|
89 |
$this->assertDescription('<' . $argumentDescription . '>', equalTo($argument));
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public function testReturnsAnObviousDescriptionIfCreatedWithANestedMatcherByMistake()
|
|
|
93 |
{
|
|
|
94 |
$innerMatcher = equalTo('NestedMatcher');
|
|
|
95 |
$this->assertDescription('<' . (string) $innerMatcher . '>', equalTo($innerMatcher));
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
public function testReturnsGoodDescriptionIfCreatedWithNullReference()
|
|
|
99 |
{
|
|
|
100 |
$this->assertDescription('null', equalTo(null));
|
|
|
101 |
}
|
|
|
102 |
}
|