| 148 |
lars |
1 |
<?php
|
|
|
2 |
namespace Hamcrest\Arrays;
|
|
|
3 |
|
|
|
4 |
use Hamcrest\AbstractMatcherTest;
|
|
|
5 |
|
|
|
6 |
class IsArrayContainingKeyTest extends AbstractMatcherTest
|
|
|
7 |
{
|
|
|
8 |
|
|
|
9 |
protected function createMatcher()
|
|
|
10 |
{
|
|
|
11 |
return IsArrayContainingKey::hasKeyInArray('irrelevant');
|
|
|
12 |
}
|
|
|
13 |
|
|
|
14 |
public function testMatchesSingleElementArrayContainingKey()
|
|
|
15 |
{
|
|
|
16 |
$array = array('a'=>1);
|
|
|
17 |
|
|
|
18 |
$this->assertMatches(hasKey('a'), $array, 'Matches single key');
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
public function testMatchesArrayContainingKey()
|
|
|
22 |
{
|
|
|
23 |
$array = array('a'=>1, 'b'=>2, 'c'=>3);
|
|
|
24 |
|
|
|
25 |
$this->assertMatches(hasKey('a'), $array, 'Matches a');
|
|
|
26 |
$this->assertMatches(hasKey('c'), $array, 'Matches c');
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
public function testMatchesArrayContainingKeyWithIntegerKeys()
|
|
|
30 |
{
|
|
|
31 |
$array = array(1=>'A', 2=>'B');
|
|
|
32 |
|
|
|
33 |
assertThat($array, hasKey(1));
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function testMatchesArrayContainingKeyWithNumberKeys()
|
|
|
37 |
{
|
|
|
38 |
$array = array(1=>'A', 2=>'B');
|
|
|
39 |
|
|
|
40 |
assertThat($array, hasKey(1));
|
|
|
41 |
|
|
|
42 |
// very ugly version!
|
|
|
43 |
assertThat($array, IsArrayContainingKey::hasKeyInArray(2));
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function testHasReadableDescription()
|
|
|
47 |
{
|
|
|
48 |
$this->assertDescription('array with key "a"', hasKey('a'));
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function testDoesNotMatchEmptyArray()
|
|
|
52 |
{
|
|
|
53 |
$this->assertMismatchDescription('array was []', hasKey('Foo'), array());
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function testDoesNotMatchArrayMissingKey()
|
|
|
57 |
{
|
|
|
58 |
$array = array('a'=>1, 'b'=>2, 'c'=>3);
|
|
|
59 |
|
|
|
60 |
$this->assertMismatchDescription('array was ["a" => <1>, "b" => <2>, "c" => <3>]', hasKey('d'), $array);
|
|
|
61 |
}
|
|
|
62 |
}
|