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) 2009 hamcrest.org
6
 */
7
 
8
class MatcherAssert
9
{
10
 
11
    /**
12
     * Number of assertions performed.
13
     *
14
     * @var int
15
     */
16
    private static $_count = 0;
17
 
18
    /**
19
     * Make an assertion and throw {@link Hamcrest\AssertionError} if it fails.
20
     *
21
     * The first parameter may optionally be a string identifying the assertion
22
     * to be included in the failure message.
23
     *
24
     * If the third parameter is not a matcher it is passed to
25
     * {@link Hamcrest\Core\IsEqual#equalTo} to create one.
26
     *
27
     * Example:
28
     * <pre>
29
     * // With an identifier
30
     * assertThat("apple flavour", $apple->flavour(), equalTo("tasty"));
31
     * // Without an identifier
32
     * assertThat($apple->flavour(), equalTo("tasty"));
33
     * // Evaluating a boolean expression
34
     * assertThat("some error", $a > $b);
35
     * assertThat($a > $b);
36
     * </pre>
37
     */
38
    public static function assertThat(/* $args ... */)
39
    {
40
        $args = func_get_args();
41
        switch (count($args)) {
42
            case 1:
43
                self::$_count++;
44
                if (!$args[0]) {
45
                    throw new AssertionError();
46
                }
47
                break;
48
 
49
            case 2:
50
                self::$_count++;
51
                if ($args[1] instanceof Matcher) {
52
                    self::doAssert('', $args[0], $args[1]);
53
                } elseif (!$args[1]) {
54
                    throw new AssertionError($args[0]);
55
                }
56
                break;
57
 
58
            case 3:
59
                self::$_count++;
60
                self::doAssert(
61
                    $args[0],
62
                    $args[1],
63
                    Util::wrapValueWithIsEqual($args[2])
64
                );
65
                break;
66
 
67
            default:
68
                throw new \InvalidArgumentException('assertThat() requires one to three arguments');
69
        }
70
    }
71
 
72
    /**
73
     * Returns the number of assertions performed.
74
     *
75
     * @return int
76
     */
77
    public static function getCount()
78
    {
79
        return self::$_count;
80
    }
81
 
82
    /**
83
     * Resets the number of assertions performed to zero.
84
     */
85
    public static function resetCount()
86
    {
87
        self::$_count = 0;
88
    }
89
 
90
    /**
91
     * Performs the actual assertion logic.
92
     *
93
     * If <code>$matcher</code> doesn't match <code>$actual</code>,
94
     * throws a {@link Hamcrest\AssertionError} with a description
95
     * of the failure along with the optional <code>$identifier</code>.
96
     *
97
     * @param string $identifier added to the message upon failure
98
     * @param mixed $actual value to compare against <code>$matcher</code>
99
     * @param \Hamcrest\Matcher $matcher applied to <code>$actual</code>
100
     * @throws AssertionError
101
     */
102
    private static function doAssert($identifier, $actual, Matcher $matcher)
103
    {
104
        if (!$matcher->matches($actual)) {
105
            $description = new StringDescription();
106
            if (!empty($identifier)) {
107
                $description->appendText($identifier . PHP_EOL);
108
            }
109
            $description->appendText('Expected: ')
110
                                    ->appendDescriptionOf($matcher)
111
                                    ->appendText(PHP_EOL . '     but: ');
112
 
113
            $matcher->describeMismatch($actual, $description);
114
 
115
            throw new AssertionError((string) $description);
116
        }
117
    }
118
}