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\Core;
3
 
4
/*
5
 Copyright (c) 2009 hamcrest.org
6
 */
7
use Hamcrest\Description;
8
use Hamcrest\Matcher;
9
use Hamcrest\TypeSafeMatcher;
10
use Hamcrest\Util;
11
 
12
/**
13
 * Tests if an array contains values that match one or more Matchers.
14
 */
15
class IsCollectionContaining extends TypeSafeMatcher
16
{
17
 
18
    private $_elementMatcher;
19
 
20
    public function __construct(Matcher $elementMatcher)
21
    {
22
        parent::__construct(self::TYPE_ARRAY);
23
 
24
        $this->_elementMatcher = $elementMatcher;
25
    }
26
 
27
    protected function matchesSafely($items)
28
    {
29
        foreach ($items as $item) {
30
            if ($this->_elementMatcher->matches($item)) {
31
                return true;
32
            }
33
        }
34
 
35
        return false;
36
    }
37
 
38
    protected function describeMismatchSafely($items, Description $mismatchDescription)
39
    {
40
        $mismatchDescription->appendText('was ')->appendValue($items);
41
    }
42
 
43
    public function describeTo(Description $description)
44
    {
45
        $description
46
                ->appendText('a collection containing ')
47
                ->appendDescriptionOf($this->_elementMatcher)
48
                ;
49
    }
50
 
51
    /**
52
     * Test if the value is an array containing this matcher.
53
     *
54
     * Example:
55
     * <pre>
56
     * assertThat(array('a', 'b'), hasItem(equalTo('b')));
57
     * //Convenience defaults to equalTo()
58
     * assertThat(array('a', 'b'), hasItem('b'));
59
     * </pre>
60
     *
61
     * @factory ...
62
     */
63
    public static function hasItem()
64
    {
65
        $args = func_get_args();
66
        $firstArg = array_shift($args);
67
 
68
        return new self(Util::wrapValueWithIsEqual($firstArg));
69
    }
70
 
71
    /**
72
     * Test if the value is an array containing elements that match all of these
73
     * matchers.
74
     *
75
     * Example:
76
     * <pre>
77
     * assertThat(array('a', 'b', 'c'), hasItems(equalTo('a'), equalTo('b')));
78
     * </pre>
79
     *
80
     * @factory ...
81
     */
82
    public static function hasItems(/* args... */)
83
    {
84
        $args = func_get_args();
85
        $matchers = array();
86
 
87
        foreach ($args as $arg) {
88
            $matchers[] = self::hasItem($arg);
89
        }
90
 
91
        return AllOf::allOf($matchers);
92
    }
93
}