| 148 |
lars |
1 |
<?php
|
|
|
2 |
namespace Hamcrest\Core;
|
|
|
3 |
|
|
|
4 |
class IsCollectionContainingTest extends \Hamcrest\AbstractMatcherTest
|
|
|
5 |
{
|
|
|
6 |
|
|
|
7 |
protected function createMatcher()
|
|
|
8 |
{
|
|
|
9 |
return \Hamcrest\Core\IsCollectionContaining::hasItem(equalTo('irrelevant'));
|
|
|
10 |
}
|
|
|
11 |
|
|
|
12 |
public function testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher()
|
|
|
13 |
{
|
|
|
14 |
$itemMatcher = hasItem(equalTo('a'));
|
|
|
15 |
|
|
|
16 |
$this->assertMatches(
|
|
|
17 |
$itemMatcher,
|
|
|
18 |
array('a', 'b', 'c'),
|
|
|
19 |
"should match list that contains 'a'"
|
|
|
20 |
);
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
public function testDoesNotMatchCollectionThatDoesntContainAnElementMatchingTheGivenMatcher()
|
|
|
24 |
{
|
|
|
25 |
$matcher1 = hasItem(equalTo('a'));
|
|
|
26 |
$this->assertDoesNotMatch(
|
|
|
27 |
$matcher1,
|
|
|
28 |
array('b', 'c'),
|
|
|
29 |
"should not match list that doesn't contain 'a'"
|
|
|
30 |
);
|
|
|
31 |
|
|
|
32 |
$matcher2 = hasItem(equalTo('a'));
|
|
|
33 |
$this->assertDoesNotMatch(
|
|
|
34 |
$matcher2,
|
|
|
35 |
array(),
|
|
|
36 |
'should not match the empty list'
|
|
|
37 |
);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public function testDoesNotMatchNull()
|
|
|
41 |
{
|
|
|
42 |
$this->assertDoesNotMatch(
|
|
|
43 |
hasItem(equalTo('a')),
|
|
|
44 |
null,
|
|
|
45 |
'should not match null'
|
|
|
46 |
);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public function testHasAReadableDescription()
|
|
|
50 |
{
|
|
|
51 |
$this->assertDescription('a collection containing "a"', hasItem(equalTo('a')));
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public function testMatchesAllItemsInCollection()
|
|
|
55 |
{
|
|
|
56 |
$matcher1 = hasItems(equalTo('a'), equalTo('b'), equalTo('c'));
|
|
|
57 |
$this->assertMatches(
|
|
|
58 |
$matcher1,
|
|
|
59 |
array('a', 'b', 'c'),
|
|
|
60 |
'should match list containing all items'
|
|
|
61 |
);
|
|
|
62 |
|
|
|
63 |
$matcher2 = hasItems('a', 'b', 'c');
|
|
|
64 |
$this->assertMatches(
|
|
|
65 |
$matcher2,
|
|
|
66 |
array('a', 'b', 'c'),
|
|
|
67 |
'should match list containing all items (without matchers)'
|
|
|
68 |
);
|
|
|
69 |
|
|
|
70 |
$matcher3 = hasItems(equalTo('a'), equalTo('b'), equalTo('c'));
|
|
|
71 |
$this->assertMatches(
|
|
|
72 |
$matcher3,
|
|
|
73 |
array('c', 'b', 'a'),
|
|
|
74 |
'should match list containing all items in any order'
|
|
|
75 |
);
|
|
|
76 |
|
|
|
77 |
$matcher4 = hasItems(equalTo('a'), equalTo('b'), equalTo('c'));
|
|
|
78 |
$this->assertMatches(
|
|
|
79 |
$matcher4,
|
|
|
80 |
array('e', 'c', 'b', 'a', 'd'),
|
|
|
81 |
'should match list containing all items plus others'
|
|
|
82 |
);
|
|
|
83 |
|
|
|
84 |
$matcher5 = hasItems(equalTo('a'), equalTo('b'), equalTo('c'));
|
|
|
85 |
$this->assertDoesNotMatch(
|
|
|
86 |
$matcher5,
|
|
|
87 |
array('e', 'c', 'b', 'd'), // 'a' missing
|
|
|
88 |
'should not match list unless it contains all items'
|
|
|
89 |
);
|
|
|
90 |
}
|
|
|
91 |
}
|