/** * @param Title $title * @param string $submodule * @param array $request * @param bool $requiredBlock * @return array * @throws MWException */ public function flowApi(Title $title, $submodule, array $request, $requiredBlock = false) { $request = new FauxRequest($request + array('action' => 'flow', 'submodule' => $submodule, 'page' => $title->getPrefixedText())); $api = new ApiMain($request); $api->execute(); $flowData = $api->getResult()->getResultData(array('flow', $submodule, 'result')); if ($flowData === null) { throw new MWException("API response has no Flow data"); } $flowData = ApiResult::stripMetadata($flowData); if ($requiredBlock !== false && !isset($flowData[$requiredBlock])) { throw new MWException("No {$requiredBlock} block in API response"); } return $flowData; }
/** * @covers ApiResult */ public function testUtilityFunctions() { $arr = array('foo' => array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), 'foo2' => (object) array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), ApiResult::META_SUBELEMENTS => array('foo', 'bar'), ApiResult::META_INDEXED_TAG_NAME => 'itn', ApiResult::META_PRESERVE_KEYS => array('foo', 'bar', '_dummy2', 0), ApiResult::META_TYPE => 'array', '_dummy' => 'foobaz', '_dummy2' => 'foobaz!'); $this->assertEquals(array('foo' => array('bar' => array(), 'bar2' => (object) array(), 'x' => 'ok'), 'foo2' => (object) array('bar' => array(), 'bar2' => (object) array(), 'x' => 'ok'), '_dummy2' => 'foobaz!'), ApiResult::stripMetadata($arr), 'ApiResult::stripMetadata'); $metadata = array(); $data = ApiResult::stripMetadataNonRecursive($arr, $metadata); $this->assertEquals(array('foo' => array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), 'foo2' => (object) array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), '_dummy2' => 'foobaz!'), $data, 'ApiResult::stripMetadataNonRecursive ($data)'); $this->assertEquals(array(ApiResult::META_SUBELEMENTS => array('foo', 'bar'), ApiResult::META_INDEXED_TAG_NAME => 'itn', ApiResult::META_PRESERVE_KEYS => array('foo', 'bar', '_dummy2', 0), ApiResult::META_TYPE => 'array', '_dummy' => 'foobaz'), $metadata, 'ApiResult::stripMetadataNonRecursive ($metadata)'); $metadata = null; $data = ApiResult::stripMetadataNonRecursive((object) $arr, $metadata); $this->assertEquals((object) array('foo' => array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), 'foo2' => (object) array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), '_dummy2' => 'foobaz!'), $data, 'ApiResult::stripMetadataNonRecursive on object ($data)'); $this->assertEquals(array(ApiResult::META_SUBELEMENTS => array('foo', 'bar'), ApiResult::META_INDEXED_TAG_NAME => 'itn', ApiResult::META_PRESERVE_KEYS => array('foo', 'bar', '_dummy2', 0), ApiResult::META_TYPE => 'array', '_dummy' => 'foobaz'), $metadata, 'ApiResult::stripMetadataNonRecursive on object ($metadata)'); }