File: IsArrayContainingKeyTest.php

package info (click to toggle)
php-hamcrest 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,168 kB
  • sloc: php: 6,353; makefile: 14; sh: 9
file content (62 lines) | stat: -rw-r--r-- 1,606 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Hamcrest\Arrays;

use Hamcrest\AbstractMatcherTestCase;

class IsArrayContainingKeyTest extends AbstractMatcherTestCase
{

    protected function createMatcher()
    {
        return IsArrayContainingKey::hasKeyInArray('irrelevant');
    }

    public function testMatchesSingleElementArrayContainingKey()
    {
        $array = array('a'=>1);

        $this->assertMatches(hasKey('a'), $array, 'Matches single key');
    }

    public function testMatchesArrayContainingKey()
    {
        $array = array('a'=>1, 'b'=>2, 'c'=>3);

        $this->assertMatches(hasKey('a'), $array, 'Matches a');
        $this->assertMatches(hasKey('c'), $array, 'Matches c');
    }

    public function testMatchesArrayContainingKeyWithIntegerKeys()
    {
        $array = array(1=>'A', 2=>'B');

        assertThat($array, hasKey(1));
    }

    public function testMatchesArrayContainingKeyWithNumberKeys()
    {
        $array = array(1=>'A', 2=>'B');

        assertThat($array, hasKey(1));

        // very ugly version!
        assertThat($array, IsArrayContainingKey::hasKeyInArray(2));
    }

    public function testHasReadableDescription()
    {
        $this->assertDescription('array with key "a"', hasKey('a'));
    }

    public function testDoesNotMatchEmptyArray()
    {
        $this->assertMismatchDescription('array was []', hasKey('Foo'), array());
    }

    public function testDoesNotMatchArrayMissingKey()
    {
        $array = array('a'=>1, 'b'=>2, 'c'=>3);

        $this->assertMismatchDescription('array was ["a" => <1>, "b" => <2>, "c" => <3>]', hasKey('d'), $array);
    }
}