function testDispatchStopPropagation() { $this->dispatcher->removeAll(); $this->dispatcher->bind(self::EVENT_FOOL3, function (Event $event) { $this->assertEquals(0, $event->getArgument('number')); $event->setArgument('number', 10); $event->stopPropagation(); }); $this->dispatcher->bind(self::EVENT_FOOL3, function (Event $event) { $event->setArgument('number', 100); }); $event = new Event(self::EVENT_FOOL3, $this, ['number' => 0]); $this->dispatcher->dispatch(self::EVENT_FOOL3, $event); $this->assertEquals(10, $event->getArgument('number')); }
/** * 执行测试 * @param Examination $examination * @return bool */ function executeExamination(Examination $examination) { //触发测试开始事件 $this->dispatcher->dispatch(self::EVENT_EXAMINATION_EXECUTE, new Event(self::EVENT_EXAMINATION_EXECUTE, $this, ['examination' => $examination])); //请求api try { $response = $this->requestApi($examination->getApi()); } catch (\Exception $e) { //如果接口请求的过程中出现异常,则终止测试过程 $examination->executed(Examination::STATUS_INTERRUPT); $examination->getReport()->write('exception', $e); return false; } $examination->getReport()->write('response', $response); //提取捕获参数 $this->extractArguments($examination, $response); //执行断言 $this->runAssertions($examination, $response); //触发测试结束事件 $this->dispatcher->dispatch(self::EVENT_EXAMINATION_EXECUTED, new Event(self::EVENT_EXAMINATION_EXECUTED, $this, ['examination' => $examination])); }
/** * 执行测试用例 * @param TestCase $testCase */ protected function runTestCase(TestCase $testCase) { //给测试用例传递Mechanic $testCase->setMechanic($this); $testMethods = $this->getTestCaseTestMethods($testCase); $this->dispatcher->dispatch(EventStore::TEST_CASE_EXECUTE, new Event(EventStore::TEST_CASE_EXECUTE, $this, ['testCase' => $testCase, 'testMethods' => $testMethods])); foreach ($testMethods as $testMethod) { try { //执行用例方法,如果方法没有明确返回false,则用例方法算执行成功 $result = $testMethod->invoke($testCase); $result = $result !== false; $message = ''; } catch (\Exception $e) { $result = false; $message = $e->getMessage(); } //记录用例方法的测试报告到用例报告 $testCase->getTestCaseReport()->addTestMethodReport(TestMethodReport::create($testMethod, $result, [$message])); } $this->dispatcher->dispatch(EventStore::TEST_CASE_EXECUTED, new Event(EventStore::TEST_CASE_EXECUTED, $this, ['testCase' => $testCase])); }
/** * 处理图片 * @param Image $image * @return ImageInterface */ protected function process(Image $image) { $this->dispatcher->dispatch(static::EVENT_PROCESS, new Event(static::EVENT_END, $this, ['image' => $image])); return $this->thumbnail($image); }
/** * 判断链接是否要继续执行 * @param Url $url * @return bool */ protected function filterUrl(Url $url) { //已经下载的链接不再处理 if (in_array($url->getRawUrl(), $this->downloadedUrls)) { return false; } $pass = true; //不在白名单里的链接要进行合法检查 if (!in_array($url->getRawUrl(), $this->whitelistUrls)) { if (in_array($url->getRawUrl(), $this->blacklistUrls) || preg_match("/^\\s*(?:#|mailto|javascript)/", $url->getRawUrl())) { $pass = false; } } //触发过滤结束url事件 $this->dispatcher->dispatch(self::EVENT_FILTERED_URL, new Event(self::EVENT_FILTERED_URL, $this, ['url' => $url, 'pass' => $pass])); return $pass; }