1<?php
2/*
3 * This file is part of PHPUnit.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11class PHPUnit_Framework_Constraint_Attribute extends PHPUnit_Framework_Constraint_Composite
12{
13    /**
14     * @var string
15     */
16    protected $attributeName;
17
18    /**
19     * @param PHPUnit_Framework_Constraint $constraint
20     * @param string                       $attributeName
21     */
22    public function __construct(PHPUnit_Framework_Constraint $constraint, $attributeName)
23    {
24        parent::__construct($constraint);
25
26        $this->attributeName = $attributeName;
27    }
28
29    /**
30     * Evaluates the constraint for parameter $other
31     *
32     * If $returnResult is set to false (the default), an exception is thrown
33     * in case of a failure. null is returned otherwise.
34     *
35     * If $returnResult is true, the result of the evaluation is returned as
36     * a boolean value instead: true in case of success, false in case of a
37     * failure.
38     *
39     * @param mixed  $other        Value or object to evaluate.
40     * @param string $description  Additional information about the test
41     * @param bool   $returnResult Whether to return a result or throw an exception
42     *
43     * @return mixed
44     *
45     * @throws PHPUnit_Framework_ExpectationFailedException
46     */
47    public function evaluate($other, $description = '', $returnResult = false)
48    {
49        return parent::evaluate(
50            PHPUnit_Framework_Assert::readAttribute(
51                $other,
52                $this->attributeName
53            ),
54            $description,
55            $returnResult
56        );
57    }
58
59    /**
60     * Returns a string representation of the constraint.
61     *
62     * @return string
63     */
64    public function toString()
65    {
66        return 'attribute "' . $this->attributeName . '" ' .
67               $this->innerConstraint->toString();
68    }
69
70    /**
71     * Returns the description of the failure
72     *
73     * The beginning of failure messages is "Failed asserting that" in most
74     * cases. This method should return the second part of that sentence.
75     *
76     * @param mixed $other Evaluated value or object.
77     *
78     * @return string
79     */
80    protected function failureDescription($other)
81    {
82        return $this->toString();
83    }
84}
85