<commit_before><?php

class CM_Http_Response_RPCTest extends CMTest_TestCase {

    public function testProcessExceptionCatching() {
        CM_Config::get()->CM_Http_Response_RPC->catchPublicExceptions = true;
        CM_Config::get()->CM_Http_Response_RPC->exceptionsToCatch = ['CM_Exception_Nonexistent' => []];
        $request = $this->mockObject('CM_Http_Request_Abstract', ['/rpc/' . CM_Site_Abstract::factory()->getType() . '/foo']);
        $request->mockMethod('getQuery')->set(function () {
            throw new CM_Exception_Invalid('foo', null, ['messagePublic' => 'bar']);
        });
        /** @var CM_Http_Request_Abstract $response */
        $response = new CM_Http_Response_RPC($request, CMTest_TH::getServiceManager());

        CMTest_TH::callProtectedMethod($response, '_process');
        $responseData = CM_Params::jsonDecode($response->getContent());

        $this->assertSame(
            ['error' =>
                 ['type'     => 'CM_Exception_Invalid',
                  'msg'      => 'bar',
                  'isPublic' => true
                 ]
            ], $responseData);

        $request->mockMethod('getQuery')->set(function () {
            throw new CM_Exception_Nonexistent('foo');
        });
        $response = new CM_Http_Response_RPC($request, CMTest_TH::getServiceManager());

        CMTest_TH::callProtectedMethod($response, '_process');
        $responseData = CM_Params::jsonDecode($response->getContent());

        $this->assertSame(
            ['error' =>
                 ['type'     => 'CM_Exception_Nonexistent',
                  'msg'      => 'Internal server error',
                  'isPublic' => false
                 ]
            ], $responseData);
    }

    public function testProcessingWithoutMethod() {
        $request = $this->mockObject('CM_Http_Request_Abstract', ['/rpc/' . CM_Site_Abstract::factory()->getType() . '/foo']);
        /** @var CM_Http_Request_Abstract $request */

        $response = new CM_Http_Response_RPC($request, $this->getServiceManager());
        $response->process();

        $result = CM_Params::jsonDecode($response->getContent());
        $this->assertEquals([
            'error' => ['type' => 'CM_Exception_InvalidParam', 'msg' => 'Internal server error', 'isPublic' => false]
        ], $result);
    }
}<commit_msg>Streamline RPC tests<commit_after><?php

class CM_Http_Response_RPCTest extends CMTest_TestCase {

    protected function tearDown() {
        CMTest_TH::clearEnv();
    }

    public function testProcessExceptionCatching() {
        CM_Config::get()->CM_Http_Response_RPC->catchPublicExceptions = true;
        CM_Config::get()->CM_Http_Response_RPC->exceptionsToCatch = ['CM_Exception_Nonexistent' => []];
        $request = $this->mockObject('CM_Http_Request_Abstract', ['/rpc/' . CM_Site_Abstract::factory()->getType() . '/foo']);
        $request->mockMethod('getQuery')->set(function () {
            throw new CM_Exception_Invalid('foo', null, ['messagePublic' => 'bar']);
        });
        /** @var CM_Http_Request_Abstract|\Mocka\AbstractClassTrait $request */
        $response = new CM_Http_Response_RPC($request, $this->getServiceManager());

        $response->process();
        $responseData = CM_Params::jsonDecode($response->getContent());

        $this->assertSame([
            'error' => [
                'type'     => 'CM_Exception_Invalid',
                'msg'      => 'bar',
                'isPublic' => true,
            ]
        ], $responseData);

        $request->mockMethod('getQuery')->set(function () {
            throw new CM_Exception_Nonexistent('foo');
        });
        $response = new CM_Http_Response_RPC($request, CMTest_TH::getServiceManager());

        $response->process();
        $responseData = CM_Params::jsonDecode($response->getContent());

        $this->assertSame(
            ['error' =>
                 ['type'     => 'CM_Exception_Nonexistent',
                  'msg'      => 'Internal server error',
                  'isPublic' => false
                 ]
            ], $responseData);
    }

    public function testProcessingWithoutMethod() {
        $request = $this->mockObject('CM_Http_Request_Abstract', ['/rpc/' . CM_Site_Abstract::factory()->getType() . '/foo']);
        /** @var CM_Http_Request_Abstract|\Mocka\AbstractClassTrait $request */

        $response = new CM_Http_Response_RPC($request, $this->getServiceManager());
        $response->process();

        $responseData = CM_Params::jsonDecode($response->getContent());
        $this->assertSame([
            'error' => [
                'type'     => 'CM_Exception_InvalidParam',
                'msg'      => 'Internal server error',
                'isPublic' => false,
            ]
        ], $responseData);
    }
}
{'fraction_non_alphanumeric': 0.1547768043387568, 'fraction_numerical': 0.0, 'mean_word_length': 2.5492227979274613, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5205896', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1287, 'n_words': 196}