|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
|
5
|
|
|
* All rights reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
8
|
|
|
* modification, are permitted provided that the following conditions |
|
9
|
|
|
* are met: |
|
10
|
|
|
* |
|
11
|
|
|
* * Redistributions of source code must retain the above copyright |
|
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
|
16
|
|
|
* the documentation and/or other materials provided with the |
|
17
|
|
|
* distribution. |
|
18
|
|
|
* |
|
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
|
20
|
|
|
* contributors may be used to endorse or promote products derived |
|
21
|
|
|
* from this software without specific prior written permission. |
|
22
|
|
|
* |
|
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
35
|
|
|
* |
|
36
|
|
|
* @category Libraries |
|
37
|
|
|
* @package Storyplayer/Asserts |
|
38
|
|
|
* @author Stuart Herbert <[email protected]> |
|
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
41
|
|
|
* @link http://datasift.github.io/storyplayer |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
namespace Storyplayer\SPv2\Modules\Asserts; |
|
45
|
|
|
|
|
46
|
|
|
use DataSift\Storyplayer\PlayerLib\StoryTeller; |
|
47
|
|
|
use DataSift\Storyplayer\PlayerLib\Story_Checkpoint; |
|
48
|
|
|
use DataSift\Stone\DataLib\DataPrinter; |
|
49
|
|
|
use DataSift\Stone\TextLib\TextHelper; |
|
50
|
|
|
|
|
51
|
|
|
use Prose\E5xx_ExpectFailed; |
|
52
|
|
|
use Prose\Prose; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Base class used for all assertions |
|
56
|
|
|
* |
|
57
|
|
|
* @category Libraries |
|
58
|
|
|
* @package Storyplayer/Prose |
|
59
|
|
|
* @author Stuart Herbert <[email protected]> |
|
60
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
61
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
62
|
|
|
* @link http://datasift.github.io/storyplayer |
|
63
|
|
|
*/ |
|
64
|
|
|
class AssertionsBase extends Prose |
|
65
|
|
|
{ |
|
66
|
|
|
protected $comparitor = null; |
|
67
|
|
|
|
|
68
|
|
|
public function __construct(StoryTeller $st, $comparitor) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->comparitor = $comparitor; |
|
71
|
|
|
parent::__construct($st); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function __call($methodName, $params) |
|
75
|
|
|
{ |
|
76
|
|
|
// what are we doing? |
|
77
|
|
|
// |
|
78
|
|
|
// let's try and make it a bit more useful to the reader |
|
79
|
|
|
$msg = $this->getStartLogMessage($methodName, $params); |
|
80
|
|
|
$log = usingLog()->startAction($msg); |
|
81
|
|
|
$actual4Log = $this->getActualDataForLog(); |
|
82
|
|
|
usingLog()->writeToLog("checking data: " . $actual4Log); |
|
83
|
|
|
|
|
84
|
|
|
// is the user trying to call a method that exists in our comparitor? |
|
85
|
|
|
if (!method_exists($this->comparitor, $methodName)) { |
|
86
|
|
|
$log->endAction("unsupported comparison '{$methodName}'"); |
|
87
|
|
|
throw new E5xx_NotImplemented(get_class($this) . '::' . $methodName); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// if we get here, then there's a comparitor we can call |
|
91
|
|
|
$result = call_user_func_array(array($this->comparitor, $methodName), $params); |
|
92
|
|
|
|
|
93
|
|
|
// was the comparison successful? |
|
94
|
|
|
if ($result->hasPassed()) { |
|
95
|
|
|
$log->endAction("assertion passed!"); |
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// if we get here, then the comparison failed |
|
100
|
|
|
usingLog()->writeToLog("expected outcome: " . $result->getExpected()); |
|
101
|
|
|
usingLog()->writeToLog("actual outcome: " . $result->getActual()); |
|
102
|
|
|
$log->endAction("assertion failed!"); |
|
103
|
|
|
throw new E5xx_ExpectFailed(__CLASS__ . "::${methodName}", $result->getExpected(), $result->getActual()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
protected function getStartLogMessage($methodName, $params) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$className = preg_replace('/^.*[\\\\_]([A-Za-z0-9]+)$/', "$1", get_class($this)); |
|
109
|
|
|
$words = TextHelper::convertCamelCaseToWords($className); |
|
110
|
|
|
if (isset($words[1])) { |
|
111
|
|
|
$msg = "assert " . strtolower($words[1]) . ' ' . $methodName; |
|
112
|
|
|
} |
|
113
|
|
|
else { |
|
114
|
|
|
$msg = "check data using $className::$methodName"; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
foreach ($params as $expected) { |
|
118
|
|
|
if (is_string($expected)) { |
|
119
|
|
|
$msg .= " '" . $expected . "'"; |
|
120
|
|
|
} |
|
121
|
|
|
else { |
|
122
|
|
|
$printer = new DataPrinter; |
|
123
|
|
|
$msg .= ' ' . $printer->convertToStringWithTypeInformation($expected); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// all done |
|
128
|
|
|
return $msg; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
protected function getActualDataForLog() |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
// special case - our checkpoint object is a 'fake' object |
|
134
|
|
|
$actual = $this->comparitor->getValue(); |
|
135
|
|
|
|
|
136
|
|
|
if ($actual instanceof Story_Checkpoint) { |
|
137
|
|
|
$printer = new DataPrinter; |
|
138
|
|
|
$actualLogMsg = $printer->convertToStringWithTypeInformation($actual->getData()); |
|
139
|
|
|
} |
|
140
|
|
|
else { |
|
141
|
|
|
$actualLogMsg = $this->comparitor->getValueForLogMessage(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $actualLogMsg; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function getComparitor() |
|
|
|
|
|
|
148
|
|
|
{ |
|
149
|
|
|
return $this->comparitor; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.