4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Argument\Token;
14 use SebastianBergmann\Comparator\ComparisonFailure;
15 use Prophecy\Comparator\Factory as ComparatorFactory;
16 use Prophecy\Util\StringUtil;
19 * Object state-checker token.
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 class ObjectStateToken implements TokenInterface
28 private $comparatorFactory;
33 * @param string $methodName
34 * @param mixed $value Expected return value
35 * @param null|StringUtil $util
36 * @param ComparatorFactory $comparatorFactory
38 public function __construct(
41 StringUtil $util = null,
42 ComparatorFactory $comparatorFactory = null
44 $this->name = $methodName;
45 $this->value = $value;
46 $this->util = $util ?: new StringUtil;
48 $this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
52 * Scores 8 if argument is an object, which method returns expected value.
54 * @param mixed $argument
58 public function scoreArgument($argument)
60 if (is_object($argument) && method_exists($argument, $this->name)) {
61 $actual = call_user_func(array($argument, $this->name));
63 $comparator = $this->comparatorFactory->getComparatorFor(
68 $comparator->assertEquals($this->value, $actual);
70 } catch (ComparisonFailure $failure) {
75 if (is_object($argument) && property_exists($argument, $this->name)) {
76 return $argument->{$this->name} === $this->value ? 8 : false;
87 public function isLast()
93 * Returns string representation for token.
97 public function __toString()
99 return sprintf('state(%s(), %s)',
101 $this->util->stringify($this->value)