Hoa central
AssertionChecker.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\Core;
40 use Hoa\Praspel;
41 
50 abstract class AssertionChecker
51 {
57  protected $_specification = null;
58 
64  protected $_data = null;
65 
71  protected $_generateData = false;
72 
78  protected $_callable = null;
79 
80 
81 
90  public function __construct(
91  Praspel\Model\Specification $specification,
92  Core\Consistency\Xcallable $callable,
93  $generateData = false
94  ) {
95  $this->setSpecification($specification);
96  $this->setCallable($callable);
97  $this->automaticallyGenerateData($generateData);
98 
99  return;
100  }
101 
109  public function preamble($preamble)
110  {
111  $preambler = new Praspel\Preambler\Handler($this->getCallable());
112  $preamble($preambler);
113  $this->setCallable($preambler->__getCallable());
114 
115  return;
116  }
117 
125  abstract public function evaluate();
126 
134  public static function generateData(Praspel\Model\Specification $specification)
135  {
136  $data = [];
137  $behavior = $specification;
138 
139  do {
140  if (true === $behavior->clauseExists('requires')) {
141  foreach ($behavior->getClause('requires') as $name => $variable) {
142  $data[$name] = $variable->sample();
143  }
144  }
145 
146  if (false === $behavior->clauseExists('behavior')) {
147  break;
148  }
149 
150  $behaviors = $behavior->getClause('behavior');
151  $count = count($behaviors);
152  $i = mt_rand(0, $count);
153 
154  if ($i === $count) {
155  if (true === $behavior->clauseExists('default')) {
156  $behavior = $behavior->getClause('default');
157  }
158  } else {
159  $behavior = $behaviors->getNth($i);
160  }
161  } while (true);
162 
163  return $data;
164  }
165 
172  protected function setSpecification(Praspel\Model\Specification $specification)
173  {
174  $old = $this->_specification;
175  $this->_specification = $specification;
176 
177  return $old;
178  }
179 
185  public function getSpecification()
186  {
187  return $this->_specification;
188  }
189 
196  public function automaticallyGenerateData($generateData)
197  {
198  $old = $this->_generateData;
199  $this->_generateData = $generateData;
200 
201  return $old;
202  }
203 
209  public function canGenerateData()
210  {
211  return $this->_generateData;
212  }
213 
220  public function setData(Array $data)
221  {
222  $old = $this->_data;
223  $this->_data = $data;
224 
225  return $old;
226  }
227 
233  public function getData()
234  {
235  return $this->_data;
236  }
237 
244  protected function setCallable(Core\Consistency\Xcallable $callable)
245  {
246  $old = $this->_callable;
247  $this->_callable = $callable;
248 
249  return $old;
250  }
251 
257  public function getCallable()
258  {
259  return $this->_callable;
260  }
261 
267  protected function getVisitorPraspel()
268  {
269  if (null === $this->_visitorPraspel) {
270  $this->_visitorPraspel = new Praspel\Visitor\Praspel();
271  }
272 
273  return $this->_visitorPraspel;
274  }
275 }
276 
280 Core\Consistency::flexEntity('Hoa\Praspel\AssertionChecker\AssertionChecker');
static generateData(Praspel\Model\Specification $specification)
setCallable(Core\Consistency\Xcallable $callable)
__construct(Praspel\Model\Specification $specification, Core\Consistency\Xcallable $callable, $generateData=false)
setSpecification(Praspel\Model\Specification $specification)