<?php include __DIR__ . '/../vendor/autoload.php'; use Slince\Runner\Runner; use Slince\Runner\ExaminationChain; use Slince\Runner\Examination; use Slince\Runner\Api; use Slince\Runner\Assertion; use Slince\Runner\Assertion\ResponseAssertion; $api = new Api('http://www.qimuyu.com', 'GET', ['foo', 'bar']); $examination = new Examination($api); $responseAssertion = new ResponseAssertion(); $assertion = new Assertion($responseAssertion, 'isOk'); $examination->addAssertion($assertion); $chain = new ExaminationChain(); $chain->enqueue($examination); $runner = new Runner($chain); $runner->run(); if ($runner->getStatus() == Runner::STATUS_WAITING) { }
/** * 从响应中提取需要catch的参数 * @param Examination $examination * @param Response $response * @return bool * @throws InvalidArgumentException */ protected function extractArguments(Examination $examination, Response $response) { $catch = $examination->getCatch(); if (empty($catch)) { return true; } //从header里面提取 if (isset($catch['header']) && is_array($catch['header'])) { foreach ($catch['header'] as $parameter => $name) { if (is_numeric($parameter)) { $newArgumentName = $name; $oldArgumentName = $name; } else { $newArgumentName = $name; $oldArgumentName = $parameter; } $this->arguments->set($newArgumentName, $response->getHeaderLine($oldArgumentName)); } } //从body里面提取 if (isset($catch['body']) && is_array($catch['body'])) { $json = json_decode($response->getBody(), true); if (json_last_error() != JSON_ERROR_NONE) { throw new InvalidArgumentException(sprintf("Invalid Json Format")); } foreach ($catch['body'] as $parameter => $name) { if (is_numeric($parameter)) { $newArgumentName = $name; $oldArgumentName = $name; } else { $newArgumentName = $name; $oldArgumentName = $parameter; } $this->arguments->set($newArgumentName, Hash::get($json, $oldArgumentName)); } } return true; }