<commit_before><?php
/**
 * Test the API dispatcher.
 *
 * @author <NAME>
 * @since  1.0
 */
use ITELIC\API\Contracts\Authenticatable;

/**
 * Class ITELIC_Test_HTTP_API
 */
class ITELIC_Test_HTTP_API extends ITELIC_UnitTestCase {

	public function test_unknown_response_generates_404() {

		$dispatch = new \ITELIC\API\Dispatch();

		$mock_wp_query = $this->getMockBuilder( '\WP_Query' )->disableOriginalConstructor()->getMock();
		$mock_wp_query->expects( $this->once() )->method( 'get' )->with( 'itelic_api' )->willReturn( 'garbage' );

		$response = $dispatch->process( $mock_wp_query );
		$data     = $response->get_data();

		$this->assertEquals( 404, $response->get_status() );
		$this->assertFalse( $data['success'] );
		$this->assertArrayHasKey( 'error', $data );
		$this->assertEquals( 404, $data['error']['code'] );
	}

	public function test_dispatch_calls_endpoint_serve_method_and_returns_given_response() {

		$response = new \ITELIC\API\Response();

		$mock_endpoint = $this->getMockBuilder( '\ITELIC\API\Contracts\Endpoint' )->getMock();
		$mock_endpoint->expects( $this->once() )->method( 'serve' )->willReturn( $response );

		$mock_wp_query = $this->getMockBuilder( '\WP_Query' )->disableOriginalConstructor()->getMock();
		$mock_wp_query->expects( $this->once() )->method( 'get' )->with( 'itelic_api' )->willReturn( 'mock' );

		$dispatch = new \ITELIC\API\Dispatch();
		\ITELIC\API\Dispatch::register_endpoint( $mock_endpoint, 'mock' );

		$this->assertEquals( $response, $dispatch->process( $mock_wp_query ) );
	}

	public function test_error_response_generated_if_api_exception_thrown() {

		$exception = new \ITELIC\API\Exception( 'Error', 5 );

		$mock_endpoint = $this->getMockBuilder( '\ITELIC\API\Contracts\Endpoint' )->getMock();
		$mock_endpoint->expects( $this->once() )->method( 'serve' )->will( $this->throwException( $exception ) );

		$mock_wp_query = $this->getMockBuilder( '\WP_Query' )->disableOriginalConstructor()->getMock();
		$mock_wp_query->expects( $this->once() )->method( 'get' )->with( 'itelic_api' )->willReturn( 'mock' );

		$dispatch = new \ITELIC\API\Dispatch();
		\ITELIC\API\Dispatch::register_endpoint( $mock_endpoint, 'mock' );

		$response = $dispatch->process( $mock_wp_query );
		$data     = $response->get_data();

		$this->assertFalse( $data['success'] );
		$this->assertArrayHasKey( 'error', $data );
		$this->assertArrayHasKey( 'message', $data['error'] );
		$this->assertArrayHasKey( 'code', $data['error'] );
		$this->assertEquals( $exception->getMessage(), $data['error']['message'] );
		$this->assertEquals( $exception->getCode(), $data['error']['code'] );
	}

	public function test_existing_license_key_auth_mode_rejects_request_without_key() {

		$mock_endpoint = $this->getMockBuilder( '\ITELIC\API\Contracts\Authenticatable' )->getMock();
		$mock_endpoint->expects( $this->never() )->method( 'serve' );
		$mock_endpoint->expects( $this->once() )->method( 'get_auth_mode' )->willReturn( Authenticatable::MODE_EXISTS );
		$mock_endpoint->expects( $this->once() )->method( 'get_auth_error_code' )->willReturn( \ITELIC\API\Endpoint::CODE_INVALID_KEY );

		WP_Mock::wpFunction( 'itelic_get_key', array(
			'times'  => 1,
			'args'   => array( 'abcd-1234' ),
			'return' => false<commit_msg>Add test for non api exceptions<commit_after><?php
/**
 * Test the API dispatcher.
 *
 * @author <NAME>
 * @since  1.0
 */
use ITELIC\API\Contracts\Authenticatable;

/**
 * Class ITELIC_Test_HTTP_API
 */
class ITELIC_Test_HTTP_API extends ITELIC_UnitTestCase {

	public function test_unknown_response_generates_404() {

		$dispatch = new \ITELIC\API\Dispatch();

		$mock_wp_query = $this->getMockBuilder( '\WP_Query' )->disableOriginalConstructor()->getMock();
		$mock_wp_query->expects( $this->once() )->method( 'get' )->with( 'itelic_api' )->willReturn( 'garbage' );

		$response = $dispatch->process( $mock_wp_query );
		$data     = $response->get_data();

		$this->assertEquals( 404, $response->get_status() );
		$this->assertFalse( $data['success'] );
		$this->assertArrayHasKey( 'error', $data );
		$this->assertEquals( 404, $data['error']['code'] );
	}

	public function test_dispatch_calls_endpoint_serve_method_and_returns_given_response() {

		$response = new \ITELIC\API\Response();

		$mock_endpoint = $this->getMockBuilder( '\ITELIC\API\Contracts\Endpoint' )->getMock();
		$mock_endpoint->expects( $this->once() )->method( 'serve' )->willReturn( $response );

		$mock_wp_query = $this->getMockBuilder( '\WP_Query' )->disableOriginalConstructor()->getMock();
		$mock_wp_query->expects( $this->once() )->method( 'get' )->with( 'itelic_api' )->willReturn( 'mock' );

		$dispatch = new \ITELIC\API\Dispatch();
		\ITELIC\API\Dispatch::register_endpoint( $mock_endpoint, 'mock' );

		$this->assertEquals( $response, $dispatch->process( $mock_wp_query ) );
	}

	public function test_error_response_generated_if_api_exception_thrown() {

		$exception = new \ITELIC\API\Exception( 'Error', 5 );

		$mock_endpoint = $this->getMockBuilder( '\ITELIC\API\Contracts\Endpoint' )->getMock();
		$mock_endpoint->expects( $this->once() )->method( 'serve' )->will( $this->throwException( $exception ) );

		$mock_wp_query = $this->getMockBuilder( '\WP_Query' )->disableOriginalConstructor()->getMock();
		$mock_wp_query->expects( $this->once() )->method( 'get' )->with( 'itelic_api' )->willReturn( 'mock' );

		$dispatch = new \ITELIC\API\Dispatch();
		\ITELIC\API\Dispatch::register_endpoint( $mock_endpoint, 'mock' );

		$response = $dispatch->process( $mock_wp_query );
		$data     = $response->get_data();

		$this->assertFalse( $data['success'] );
		$this->assertArrayHasKey( 'error', $data );
		$this->assertArrayHasKey( 'message', $data['error'] );
		$this->assertArrayHasKey( 'code', $data['error'] );
		$this->assertEquals( $exception->getMessage(), $data['error']['message'] );
		$this->assertEquals( $exception->getCode(), $data['error']['code'] );
		$this->assertEquals( 400, $response->get_status() );
	}

	public function test_error_response_generate_if_non_api_exception_thrown() {

		$exception = new Exception( 'Error', 5 );

		$mock_endpoint = $this->getMockBuilder( '\ITELIC\API\Contracts\Endpoint' )->getMock();
		$mock_endpoint->expects( $this->once() )->method( 'serve' )->will( $this->throwException( $exception ) );

		$mock_wp_query = $this->getMockBuilder( '\WP_Query' )->disableOriginalConstructor()->getMock();
		$mock_wp_query->expects( $this->once() )->method( 'get' )->with( 'itelic_api' )->willReturn( 'mock' );

		$dispatch = new \ITELIC\API\Dispatch();
		\ITELIC\API\Dispatch::register_endpoint( $mock_endpoint, 'mock' );

		$response = $dispatch->process( $mock_wp_query );
		$data     = $response->get_data();

		$this->assertFalse( $data['success'] );
		$this->assertArrayHasKey( 'error', $data );
		$this->assertArrayHasKey( 'message', $data['error'] );
		$this->assertArrayHasKey( 'code', $data['error'] );
		$this->assertEquals( 0, $data['error']['code'] );
		$this->assertEquals( 500, $response->get_status() );
	}

	public function test_existing_license_key_auth_mode_rejects_request_without_key() {

		$mock_endpoint = $this->getMockBuilder( '\ITELIC\API\Contracts\Authenticatable' )->getMock();
		$mock_endpoint->expects( $this->never() )->method( 'serve' );
		$mock_endpoint->expects( $this->once() )->method( 'get_auth_mode' )->willReturn( Authenticatable::MODE_EXISTS );
		$mock_endpoint->expects( $this->once() )->method( 'get_auth_error_code' )->willReturn( \ITELIC\API\Endpoint::CODE_INVALID_KEY );

		WP_Mock::wpFunction( 'itelic_get_key', array(
			'times'  => 1,
			'args'   => array( 'abcd-1234' ),
			'return' => false
{'fraction_non_alphanumeric': 0.19826794383939117, 'fraction_numerical': 0.005511087783755413, 'mean_word_length': 5.610581092801388, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 175, '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': '2993224', 'n_tokens_mistral': 2791, 'n_tokens_neox': 2607, 'n_words': 428}