<commit_before><?php

use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
use Stub\PaginatedEndpointStub;

class PaginatedEndpointTest extends TestCase {
    protected function getPaginatedEndpoint( $maxPageSize = 10 ) {
        return new PaginatedEndpointStub( $this->api()->getClient(), $maxPageSize );
    }

    public function testBasicPage() {
        $this->mockResponse('[]');

        $this->getPaginatedEndpoint()->page( 1, 2 );

        $request = $this->getLastRequest();

        $this->assertTrue( $request->getQuery()->hasKey('page'),
            'PaginatedEndpoint sets page query parameter' );
        $this->assertEquals( 1, $request->getQuery()->get('page'),
            'PaginatedEndpoint sets correct page query parameter value' );

        $this->assertTrue( $request->getQuery()->hasKey('page_size'),
            'PaginatedEndpoint sets page_size query parameter' );
        $this->assertEquals( 2, $request->getQuery()->get('page_size'),
            'PaginatedEndpoint sets correct page_size query parameter value' );
    }

    public function testAll() {
        $firstResponse = new Response(
            200,
            [ 'X-Result-Total' => 10, 'Content-Type' => 'application/json; charset=utf-8' ],
            Stream::factory( '[1,2,3]' )
        );

        $this->mockResponse( $firstResponse );
        $this->mockResponse( '[4,5,6]' );
        $this->mockResponse( '[7,8,9]' );
        $this->mockResponse( '[10]' );

        $result = $this->getPaginatedEndpoint( 3 )->all();
        $this->assertCount( 10, $result, 'PaginatedEndpoint gets all results' );

        $requests = $this->history->getRequests();
        $this->assertCount( 4, $requests, 'PaginatedEndpoint makes exactly as many requests as pages exist' );

        $this->assertEquals( 0, $requests[0]->getQuery()->get('page') );
        $this->assertEquals( 1, $requests[1]->getQuery()->get('page') );
        $this->assertEquals( 2, $requests[2]->getQuery()->get('page') );
        $this->assertEquals( 3, $requests[3]->getQuery()->get('page') );
    }
}
<commit_msg>add OutOfRange tests for PaginatedEndpoint<commit_after><?php

use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
use Stub\PaginatedEndpointStub;

class PaginatedEndpointTest extends TestCase {
    protected function getPaginatedEndpoint( $maxPageSize = 10 ) {
        return new PaginatedEndpointStub( $this->api()->getClient(), $maxPageSize );
    }

    public function testBasicPage() {
        $this->mockResponse('[]');

        $this->getPaginatedEndpoint()->page( 1, 2 );

        $request = $this->getLastRequest();

        $this->assertTrue( $request->getQuery()->hasKey('page'),
            'PaginatedEndpoint sets page query parameter' );
        $this->assertEquals( 1, $request->getQuery()->get('page'),
            'PaginatedEndpoint sets correct page query parameter value' );

        $this->assertTrue( $request->getQuery()->hasKey('page_size'),
            'PaginatedEndpoint sets page_size query parameter' );
        $this->assertEquals( 2, $request->getQuery()->get('page_size'),
            'PaginatedEndpoint sets correct page_size query parameter value' );
    }

    public function testAll() {
        $firstResponse = new Response(
            200,
            [ 'X-Result-Total' => 10, 'Content-Type' => 'application/json; charset=utf-8' ],
            Stream::factory( '[1,2,3]' )
        );

        $this->mockResponse( $firstResponse );
        $this->mockResponse( '[4,5,6]' );
        $this->mockResponse( '[7,8,9]' );
        $this->mockResponse( '[10]' );

        $result = $this->getPaginatedEndpoint( 3 )->all();
        $this->assertCount( 10, $result, 'PaginatedEndpoint gets all results' );

        $requests = $this->history->getRequests();
        $this->assertCount( 4, $requests, 'PaginatedEndpoint makes exactly as many requests as pages exist' );

        $this->assertEquals( 0, $requests[0]->getQuery()->get('page') );
        $this->assertEquals( 1, $requests[1]->getQuery()->get('page') );
        $this->assertEquals( 2, $requests[2]->getQuery()->get('page') );
        $this->assertEquals( 3, $requests[3]->getQuery()->get('page') );
    }


    /** @expectedException \OutOfRangeException */
    public function testPageOutOfRangeLower() {
        $this->getPaginatedEndpoint()->page( -1 );
    }

    /**
     * @expectedException \GW2Treasures\GW2Api\Exception\ApiException
     * this should be a PageOutOfRangeException, but we need better error handling from within traits for that.
     */
    public function testPageOutOfRangeUpper() {
        $this->mockResponse( new Response(
            400, [ 'Content-Type' => 'application/json; charset=utf-8' ],
            Stream::factory( '{"text":"page out of range. Use page values 0 - 1."}' )
        ));

        $this->getPaginatedEndpoint()->page( 5 );
    }

    /** @expectedException \OutOfRangeException */
    public function testPageSizeOutOfRangeLower() {
        $this->getPaginatedEndpoint()->page( 0, 0 );
    }

    /** @expectedException \OutOfRangeException */
    public function testPageSizeOutOfRangeUpper() {
        $this->getPaginatedEndpoint( 1 )->page( 0, 2 );
    }
}

{'fraction_non_alphanumeric': 0.1625312560107713, 'fraction_numerical': 0.016349297941911905, 'mean_word_length': 3.059328649492584, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 97, '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': '1761731', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1470, 'n_words': 315}