Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
namespace Hamcrest\Arrays;
3
 
4
/**
5
 * Tests for the presence of both a key and value inside an array.
6
 */
7
use Hamcrest\Description;
8
use Hamcrest\Matcher;
9
use Hamcrest\TypeSafeMatcher;
10
use Hamcrest\Util;
11
 
12
/**
13
 * @namespace
14
 */
15
 
16
class IsArrayContainingKeyValuePair extends TypeSafeMatcher
17
{
18
 
19
    private $_keyMatcher;
20
    private $_valueMatcher;
21
 
22
    public function __construct(Matcher $keyMatcher, Matcher $valueMatcher)
23
    {
24
        parent::__construct(self::TYPE_ARRAY);
25
 
26
        $this->_keyMatcher = $keyMatcher;
27
        $this->_valueMatcher = $valueMatcher;
28
    }
29
 
30
    protected function matchesSafely($array)
31
    {
32
        foreach ($array as $key => $value) {
33
            if ($this->_keyMatcher->matches($key) && $this->_valueMatcher->matches($value)) {
34
                return true;
35
            }
36
        }
37
 
38
        return false;
39
    }
40
 
41
    protected function describeMismatchSafely($array, Description $mismatchDescription)
42
    {
43
        //Not using appendValueList() so that keys can be shown
44
        $mismatchDescription->appendText('array was ')
45
                                                ->appendText('[')
46
                                                ;
47
        $loop = false;
48
        foreach ($array as $key => $value) {
49
            if ($loop) {
50
                $mismatchDescription->appendText(', ');
51
            }
52
            $mismatchDescription->appendValue($key)->appendText(' => ')->appendValue($value);
53
            $loop = true;
54
        }
55
        $mismatchDescription->appendText(']');
56
    }
57
 
58
    public function describeTo(Description $description)
59
    {
60
        $description->appendText('array containing [')
61
                                ->appendDescriptionOf($this->_keyMatcher)
62
                                ->appendText(' => ')
63
                                ->appendDescriptionOf($this->_valueMatcher)
64
                                ->appendText(']')
65
                                ;
66
    }
67
 
68
    /**
69
     * Test if an array has both an key and value in parity with each other.
70
     *
71
     * @factory hasEntry
72
     */
73
    public static function hasKeyValuePair($key, $value)
74
    {
75
        return new self(
76
            Util::wrapValueWithIsEqual($key),
77
            Util::wrapValueWithIsEqual($value)
78
        );
79
    }
80
}