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;
3
 
4
/*
5
 Copyright (c) 2012 hamcrest.org
6
 */
7
 
8
/**
9
 * Contains utility methods for handling Hamcrest matchers.
10
 *
11
 * @see Hamcrest\Matcher
12
 */
13
class Util
14
{
15
    public static function registerGlobalFunctions()
16
    {
17
        require_once __DIR__.'/../Hamcrest.php';
18
    }
19
 
20
    /**
21
     * Wraps the item with an IsEqual matcher if it isn't a matcher already.
22
     *
23
     * @param mixed $item matcher or any value
24
     * @return \Hamcrest\Matcher
25
     */
26
    public static function wrapValueWithIsEqual($item)
27
    {
28
        return ($item instanceof Matcher)
29
            ? $item
30
            : Core\IsEqual::equalTo($item)
31
            ;
32
    }
33
 
34
    /**
35
     * Throws an exception if any item in $matchers is not a Hamcrest\Matcher.
36
     *
37
     * @param array $matchers expected to contain only matchers
38
     * @throws \InvalidArgumentException if any item is not a matcher
39
     */
40
    public static function checkAllAreMatchers(array $matchers)
41
    {
42
        foreach ($matchers as $m) {
43
            if (!($m instanceof Matcher)) {
44
                throw new \InvalidArgumentException(
45
                    'Each argument or element must be a Hamcrest matcher'
46
                );
47
            }
48
        }
49
    }
50
 
51
    /**
52
     * Returns a copy of $items where each non-Matcher item is replaced by
53
     * a Hamcrest\Core\IsEqual matcher for the item. If the first and only item
54
     * is an array, it is used as the $items array to support the old style
55
     * of passing an array as the sole argument to a matcher.
56
     *
57
     * @param array $items contains items and matchers
58
     * @return array<Matchers> all items are
59
     */
60
    public static function createMatcherArray(array $items)
61
    {
62
        //Extract single array item
63
        if (count($items) == 1 && is_array($items[0])) {
64
            $items = $items[0];
65
        }
66
 
67
        //Replace non-matchers
68
        foreach ($items as &$item) {
69
            if (!($item instanceof Matcher)) {
70
                $item = Core\IsEqual::equalTo($item);
71
            }
72
        }
73
 
74
        return $items;
75
    }
76
}