Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Prophecy.
|
Chris@0
|
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
6 * Marcello Duarte <marcello.duarte@gmail.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Prophecy\Argument\Token;
|
Chris@0
|
13
|
Chris@0
|
14 use SebastianBergmann\Comparator\ComparisonFailure;
|
Chris@0
|
15 use Prophecy\Comparator\Factory as ComparatorFactory;
|
Chris@0
|
16 use Prophecy\Util\StringUtil;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Object state-checker token.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 class ObjectStateToken implements TokenInterface
|
Chris@0
|
24 {
|
Chris@0
|
25 private $name;
|
Chris@0
|
26 private $value;
|
Chris@0
|
27 private $util;
|
Chris@0
|
28 private $comparatorFactory;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Initializes token.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @param string $methodName
|
Chris@0
|
34 * @param mixed $value Expected return value
|
Chris@0
|
35 * @param null|StringUtil $util
|
Chris@0
|
36 * @param ComparatorFactory $comparatorFactory
|
Chris@0
|
37 */
|
Chris@0
|
38 public function __construct(
|
Chris@0
|
39 $methodName,
|
Chris@0
|
40 $value,
|
Chris@0
|
41 StringUtil $util = null,
|
Chris@0
|
42 ComparatorFactory $comparatorFactory = null
|
Chris@0
|
43 ) {
|
Chris@0
|
44 $this->name = $methodName;
|
Chris@0
|
45 $this->value = $value;
|
Chris@0
|
46 $this->util = $util ?: new StringUtil;
|
Chris@0
|
47
|
Chris@0
|
48 $this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Scores 8 if argument is an object, which method returns expected value.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @param mixed $argument
|
Chris@0
|
55 *
|
Chris@0
|
56 * @return bool|int
|
Chris@0
|
57 */
|
Chris@0
|
58 public function scoreArgument($argument)
|
Chris@0
|
59 {
|
Chris@0
|
60 if (is_object($argument) && method_exists($argument, $this->name)) {
|
Chris@0
|
61 $actual = call_user_func(array($argument, $this->name));
|
Chris@0
|
62
|
Chris@0
|
63 $comparator = $this->comparatorFactory->getComparatorFor(
|
Chris@0
|
64 $this->value, $actual
|
Chris@0
|
65 );
|
Chris@0
|
66
|
Chris@0
|
67 try {
|
Chris@0
|
68 $comparator->assertEquals($this->value, $actual);
|
Chris@0
|
69 return 8;
|
Chris@0
|
70 } catch (ComparisonFailure $failure) {
|
Chris@0
|
71 return false;
|
Chris@0
|
72 }
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 if (is_object($argument) && property_exists($argument, $this->name)) {
|
Chris@0
|
76 return $argument->{$this->name} === $this->value ? 8 : false;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 return false;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Returns false.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @return bool
|
Chris@0
|
86 */
|
Chris@0
|
87 public function isLast()
|
Chris@0
|
88 {
|
Chris@0
|
89 return false;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Returns string representation for token.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @return string
|
Chris@0
|
96 */
|
Chris@0
|
97 public function __toString()
|
Chris@0
|
98 {
|
Chris@0
|
99 return sprintf('state(%s(), %s)',
|
Chris@0
|
100 $this->name,
|
Chris@0
|
101 $this->util->stringify($this->value)
|
Chris@0
|
102 );
|
Chris@0
|
103 }
|
Chris@0
|
104 }
|