~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/phpunit/phpunit/src/Framework/Constraint/ClassHasAttribute.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
11
/**
 
12
 * Constraint that asserts that the class it is evaluated for has a given
 
13
 * attribute.
 
14
 *
 
15
 * The attribute name is passed in the constructor.
 
16
 *
 
17
 * @package    PHPUnit
 
18
 * @subpackage Framework_Constraint
 
19
 * @author     Sebastian Bergmann <sebastian@phpunit.de>
 
20
 * @author     Bernhard Schussek <bschussek@2bepublished.at>
 
21
 * @copyright  Sebastian Bergmann <sebastian@phpunit.de>
 
22
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 
23
 * @link       http://www.phpunit.de/
 
24
 * @since      Class available since Release 3.1.0
 
25
 */
 
26
class PHPUnit_Framework_Constraint_ClassHasAttribute extends PHPUnit_Framework_Constraint
 
27
{
 
28
    /**
 
29
     * @var string
 
30
     */
 
31
    protected $attributeName;
 
32
 
 
33
    /**
 
34
     * @param string $attributeName
 
35
     */
 
36
    public function __construct($attributeName)
 
37
    {
 
38
        parent::__construct();
 
39
        $this->attributeName = $attributeName;
 
40
    }
 
41
 
 
42
    /**
 
43
     * Evaluates the constraint for parameter $other. Returns true if the
 
44
     * constraint is met, false otherwise.
 
45
     *
 
46
     * @param  mixed $other Value or object to evaluate.
 
47
     * @return bool
 
48
     */
 
49
    protected function matches($other)
 
50
    {
 
51
        $class = new ReflectionClass($other);
 
52
 
 
53
        return $class->hasProperty($this->attributeName);
 
54
    }
 
55
 
 
56
    /**
 
57
     * Returns a string representation of the constraint.
 
58
     *
 
59
     * @return string
 
60
     */
 
61
    public function toString()
 
62
    {
 
63
        return sprintf(
 
64
            'has attribute "%s"',
 
65
            $this->attributeName
 
66
        );
 
67
    }
 
68
 
 
69
    /**
 
70
     * Returns the description of the failure
 
71
     *
 
72
     * The beginning of failure messages is "Failed asserting that" in most
 
73
     * cases. This method should return the second part of that sentence.
 
74
     *
 
75
     * @param  mixed  $other Evaluated value or object.
 
76
     * @return string
 
77
     */
 
78
    protected function failureDescription($other)
 
79
    {
 
80
        return sprintf(
 
81
            '%sclass "%s" %s',
 
82
            is_object($other) ? 'object of ' : '',
 
83
            is_object($other) ? get_class($other) : $other,
 
84
            $this->toString()
 
85
        );
 
86
    }
 
87
}