1: <?php
2:
3: /**
4: * This file is part of expect package.
5: *
6: * (c) Noritaka Horio <holy.shared.design@gmail.com>
7: *
8: * This source file is subject to the MIT license that is bundled
9: * with this source code in the file LICENSE.
10: */
11: namespace expect\matcher;
12:
13: use expect\FailedMessage;
14:
15: /**
16: * Verify the output in the expected value.
17: *
18: * <code>
19: * $matcher = new ToPrint('foo');
20: * $matcher->match(function() { echo 'foo' }); //return true
21: *
22: * $matcher->match(function() { echo 'bar' }); //return false
23: * <code>
24: *
25: * @author Noritaka Horio <holy.shared.design@gmail.com>
26: * @copyright Noritaka Horio <holy.shared.design@gmail.com>
27: */
28: final class ToPrint implements ReportableMatcher
29: {
30: /**
31: * @var callable
32: */
33: private $actual;
34:
35: /**
36: * @var string
37: */
38: private $expected;
39:
40: /**
41: * Create a new matcher.
42: *
43: * @param string $expected
44: */
45: public function __construct($expected)
46: {
47: $this->expected = $expected;
48: }
49:
50: /**
51: * {@inheritdoc}
52: */
53: public function match($actual)
54: {
55: ob_start();
56: $actual();
57: $this->actual = ob_get_clean();
58:
59: return $this->actual === $this->expected;
60: }
61:
62: /**
63: * {@inheritdoc}
64: */
65: public function reportFailed(FailedMessage $message)
66: {
67: $message->appendText('Expected ')
68: ->appendValue($this->expected)
69: ->appendText(', got ')
70: ->appendValue($this->actual);
71: }
72:
73: /**
74: * {@inheritdoc}
75: */
76: public function reportNegativeFailed(FailedMessage $message)
77: {
78: $message->appendText('Expected output other than ')
79: ->appendValue($this->expected);
80: }
81: }
82: