Skip to content

Commit 8353faf

Browse files
Add ClassHasAttribute constraint.
1 parent 0d83002 commit 8353faf

3 files changed

Lines changed: 109 additions & 23 deletions

File tree

PHPUnit/Framework/Constraint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public static function negate($string)
141141

142142
require_once 'PHPUnit/Framework/Constraint/And.php';
143143
require_once 'PHPUnit/Framework/Constraint/ArrayHasKey.php';
144+
require_once 'PHPUnit/Framework/Constraint/ClassHasAttribute.php';
144145
require_once 'PHPUnit/Framework/Constraint/FileExists.php';
145146
require_once 'PHPUnit/Framework/Constraint/GreaterThan.php';
146147
require_once 'PHPUnit/Framework/Constraint/IsAnything.php';
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* PHPUnit
4+
*
5+
* Copyright (c) 2002-2007, Sebastian Bergmann <[email protected]>.
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
*
15+
* * Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in
17+
* the documentation and/or other materials provided with the
18+
* distribution.
19+
*
20+
* * Neither the name of Sebastian Bergmann nor the names of his
21+
* contributors may be used to endorse or promote products derived
22+
* from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
33+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35+
* POSSIBILITY OF SUCH DAMAGE.
36+
*
37+
* @category Testing
38+
* @package PHPUnit
39+
* @author Jan Borsodi <[email protected]>
40+
* @author Sebastian Bergmann <[email protected]>
41+
* @copyright 2002-2007 Sebastian Bergmann <[email protected]>
42+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
43+
* @version SVN: $Id$
44+
* @link http://www.phpunit.de/
45+
* @since File available since Release 3.1.0
46+
*/
47+
48+
require_once 'PHPUnit/Framework.php';
49+
require_once 'PHPUnit/Util/Filter.php';
50+
require_once 'PHPUnit/Util/Type.php';
51+
52+
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
53+
54+
/**
55+
* Constraint that asserts that the class it is evaluated for has a given
56+
* attribute.
57+
*
58+
* The attribute name is passed in the constructor.
59+
*
60+
* @category Testing
61+
* @package PHPUnit
62+
* @author Sebastian Bergmann <[email protected]>
63+
* @copyright 2002-2007 Sebastian Bergmann <[email protected]>
64+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
65+
* @version Release: @package_version@
66+
* @link http://www.phpunit.de/
67+
* @since Class available since Release 3.1.0
68+
*/
69+
class PHPUnit_Framework_Constraint_ClassHasAttribute extends PHPUnit_Framework_Constraint
70+
{
71+
protected $attributeName;
72+
73+
public function __construct($attributeName)
74+
{
75+
$this->attributeName = $attributeName;
76+
}
77+
78+
/**
79+
* Evaluates the constraint for parameter $other. Returns TRUE if the
80+
* constraint is met, FALSE otherwise.
81+
*
82+
* @param mixed $other Value or object to evaluate.
83+
* @return bool
84+
*/
85+
public function evaluate($other)
86+
{
87+
$class = new ReflectionClass($other);
88+
89+
return $class->hasProperty($this->attributeName);
90+
}
91+
92+
/**
93+
* Returns a string representation of the constraint.
94+
*
95+
* @return string
96+
* @access public
97+
*/
98+
public function toString()
99+
{
100+
return sprintf(
101+
'has attribute "%s"',
102+
103+
$this->attributeName
104+
);
105+
}
106+
}
107+
?>

PHPUnit/Framework/Constraint/ObjectHasAttribute.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,8 @@
6666
* @link http://www.phpunit.de/
6767
* @since Class available since Release 3.0.0
6868
*/
69-
class PHPUnit_Framework_Constraint_ObjectHasAttribute extends PHPUnit_Framework_Constraint
69+
class PHPUnit_Framework_Constraint_ObjectHasAttribute extends PHPUnit_Framework_Constraint_ClassHasAttribute
7070
{
71-
private $attributeName;
72-
73-
public function __construct($attributeName)
74-
{
75-
$this->attributeName = $attributeName;
76-
}
77-
7871
/**
7972
* Evaluates the constraint for parameter $other. Returns TRUE if the
8073
* constraint is met, FALSE otherwise.
@@ -88,20 +81,5 @@ public function evaluate($other)
8881

8982
return $object->hasProperty($this->attributeName);
9083
}
91-
92-
/**
93-
* Returns a string representation of the constraint.
94-
*
95-
* @return string
96-
* @access public
97-
*/
98-
public function toString()
99-
{
100-
return sprintf(
101-
'has attribute "%s"',
102-
103-
$this->attributeName
104-
);
105-
}
10684
}
10785
?>

0 commit comments

Comments
 (0)