ajax_response = $this->mock_ajax_response(); } public function tearDown() { // Clean up AJAX response $this->ajax_response->initResponseArray(); $ref = new \ReflectionProperty('\\EGroupware\\Api\\Json\\Response', 'response'); $ref->setAccessible(true); $ref->setValue(null, null); } /** * Use a known, public callback so we can hook into it, if needed */ public static function public_callback($content) { static::$mocked_exec_result = $content; } /** * Mocks what is needed to fake a call to exec, and catch its output. * The resulting array of information, which would normally be sent to the * client as JSON, is returned for evaluation. * * @param Etemplate $etemplate * @param array $content * @param array $sel_options * @param array $readonlys * @param array $preserv */ protected function mockedExec(Etemplate $etemplate, array $content,array $sel_options=null,array $readonlys=null,array $preserv=null) { ob_start(); // Exec the template $etemplate->exec(__CLASS__ . '::public_callback', $content, $sel_options, $readonlys, $preserv, 4); $result = $this->ajax_response->returnResult(); // Store & clean the request //$etemplate->destroy_request(); ob_end_clean(); return $result; } protected function mockedRoundTrip(\EGroupware\Api\Etemplate $etemplate, array $content,array $sel_options=null,array $readonlys=null,array $preserv=null) { $result = $this->mockedExec($etemplate, $content, $sel_options, $readonlys, $preserv); // Check for the load $data = array(); foreach($result as $command) { if($command['type'] == 'et2_load') { $data = $command['data']; break; } } Etemplate::ajax_process_content($data['data']['etemplate_exec_id'], $data['data']['content'], false); $content = static::$mocked_exec_result; static::$mocked_exec_result = array(); return $content; } /** * Mock the ajax response and override it so it doesn't try to send headers, * which generates errors since PHPUnit has already done that */ protected function mock_ajax_response() { $response = $this->getMockBuilder('\\EGroupware\\Api\\Json\\Response') ->disableOriginalConstructor() ->setMethods(['get'/*,'generic'*/]) ->getMock(); // Replace protected self reference with mock object $ref = new \ReflectionProperty('\\EGroupware\\Api\\Json\\Response', 'response'); $ref->setAccessible(true); $ref->setValue(null, $response); $response ->method('get') ->with(function() { // Don't send headers, like the real one does return self::$response; }); return $response; } }