| 148 |
lars |
1 |
<?php
|
|
|
2 |
namespace Hamcrest;
|
|
|
3 |
|
|
|
4 |
/*
|
|
|
5 |
Copyright (c) 2009 hamcrest.org
|
|
|
6 |
*/
|
|
|
7 |
|
|
|
8 |
/**
|
|
|
9 |
* Supporting class for matching a feature of an object. Implement
|
|
|
10 |
* <code>featureValueOf()</code> in a subclass to pull out the feature to be
|
|
|
11 |
* matched against.
|
|
|
12 |
*/
|
|
|
13 |
abstract class FeatureMatcher extends TypeSafeDiagnosingMatcher
|
|
|
14 |
{
|
|
|
15 |
|
|
|
16 |
private $_subMatcher;
|
|
|
17 |
private $_featureDescription;
|
|
|
18 |
private $_featureName;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Constructor.
|
|
|
22 |
*
|
|
|
23 |
* @param string $type
|
|
|
24 |
* @param string $subtype
|
|
|
25 |
* @param \Hamcrest\Matcher $subMatcher The matcher to apply to the feature
|
|
|
26 |
* @param string $featureDescription Descriptive text to use in describeTo
|
|
|
27 |
* @param string $featureName Identifying text for mismatch message
|
|
|
28 |
*/
|
|
|
29 |
public function __construct($type, $subtype, Matcher $subMatcher, $featureDescription, $featureName)
|
|
|
30 |
{
|
|
|
31 |
parent::__construct($type, $subtype);
|
|
|
32 |
|
|
|
33 |
$this->_subMatcher = $subMatcher;
|
|
|
34 |
$this->_featureDescription = $featureDescription;
|
|
|
35 |
$this->_featureName = $featureName;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Implement this to extract the interesting feature.
|
|
|
40 |
*
|
|
|
41 |
* @param mixed $actual the target object
|
|
|
42 |
*
|
|
|
43 |
* @return mixed the feature to be matched
|
|
|
44 |
*/
|
|
|
45 |
abstract protected function featureValueOf($actual);
|
|
|
46 |
|
|
|
47 |
public function matchesSafelyWithDiagnosticDescription($actual, Description $mismatchDescription)
|
|
|
48 |
{
|
|
|
49 |
$featureValue = $this->featureValueOf($actual);
|
|
|
50 |
|
|
|
51 |
if (!$this->_subMatcher->matches($featureValue)) {
|
|
|
52 |
$mismatchDescription->appendText($this->_featureName)
|
|
|
53 |
->appendText(' was ')->appendValue($featureValue);
|
|
|
54 |
|
|
|
55 |
return false;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
return true;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
final public function describeTo(Description $description)
|
|
|
62 |
{
|
|
|
63 |
$description->appendText($this->_featureDescription)->appendText(' ')
|
|
|
64 |
->appendDescriptionOf($this->_subMatcher)
|
|
|
65 |
;
|
|
|
66 |
}
|
|
|
67 |
}
|