<commit_before><?php

use Recurly\Pager;
use Recurly\Logger;
use Psr\Log\LogLevel;

final class PagerTest extends RecurlyTestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        // Using LogLevel::EMERGENCY to minimize output when running tests
        $logger = new Logger('Recurly', LogLevel::EMERGENCY);
        $this->client = new MockClient($logger);
        $this->setUpRequest();

        $this->count = 3;
        $client_stub = $this->createMock(\Recurly\BaseClient::class);
        $client_stub->method('nextPage')->will($this->returnCallback(function($name, $params) {
            if (is_array($params) && array_key_exists('limit', $params) && $params['limit'] == 1) {
                $name = 'page_limit_1';
            }
            $json_string = $this->fixtures->loadJsonFixture($name, ['type' => 'string']);
            $response = new \Recurly\Response($json_string, $this->request);
            $response->setHeaders([
                'HTTP/1.1 200 OK',
                "Recurly-Total-Records: {$this->count}"
            ]);
            return $response->toResource();
        }));
        $client_stub->method('pagerCount')->will($this->returnCallback(function($name, $params) {
            $response = new \Recurly\Response('', $this->request);
            $response->setHeaders([
                'HTTP/1.1 200 OK',
                "Recurly-Total-Records: {$this->count}"
            ]);
            return $response;
        }));

        $this->pager = new \Recurly\Pager($client_stub, 'page_one');
    }

    public function testMapArrayParams(): void
    {
        $ids = ['id-1', 'id-2'];
        $idsCsv = join('%2C', $ids);
        $url = "https://v3.recurly.com/resources?ids=$idsCsv";
        $result = '{"object":"list","has_more":true,"next":"page_two","data":[{"object":"test_resource","name":"resource one"}]}';
        $this->client->addScenario("GET", $url, NULL, $result, "200 OK");

        $pager = new \Recurly\Pager($this->client, '/resources', [ 'params' => [ 'ids' => $ids ] ]);
        $pager->rewind();
        $this->assertInstanceOf(\Recurly\Response::class, $pager->getResponse());
    }

    public function testGetResponse(): void
    {
        $url = "https://v3.recurly.com/resources";
        $result = '{"object":"list","has_more":false,"next":null,"data":[{"object":"test_resource","name":"resource one"}]}';
        $this->client->addScenario("GET", $url, NULL, $result, "200 OK");

        $pager = new \Recurly\Pager($this->client, '/resources');
        $pager->rewind();
        $this->assertInstanceOf(\Recurly\Response::class, $pager->getResponse());
    }

    public function testGetFirst(): void
    {
        $url = "https://v3.recurly.com/resources?limit=1";
        $result = '{"object":"list","has_more":true,"next":"page_two","data":[{"object":"test_resource","name":"resource one"}]}';
        $this->client->addScenario("GET", $url, NULL, $result, "200 OK");

        $pager = new \Recurly\Pager($this->client, '/resources', [ 'params' => [ 'limit' => 5 ] ]);
        $this->assertInstanceOf(
            \Recurly\Resources\TestResource::class,
            $pager->getFirst()
        );
    }

    public function testGetFirstNoResults(): void
    {
        $url = "https://v3.recurly.com/resources?limit=1";
        $result = '{"object":"list","has_more":false,"next":null,"data":[]}';
        $this->client->addScenario("GET", $url, NULL, $result, "200 OK");

        $pager = new \Recurly\Pager($this->client, '/resources');
        $this->assertNull($pager->getFirst());
    }

    public function testTake(): void
    {
        $url = "https://v3.recurly.com/resources?limit=2";
        $result = '{"object":"list","has_more":true,"next":"page_two","data":[
          {"object":"test_resource","name":"resource one"},
          {"object":"test_resource","name":"resource two"}
        ]}';        
        $this->client->addScenario("GET", $url, NULL, $result, "200 OK");

        $pager = new \Recurly\Pager($this->client, '/resources', [ 'params' => [ 'limit' => 200 ] ]);
        $this->assertEquals(2, count($pager->take(2))
        );
    }

    public function testGetCount(): void
    {
        $url = "https://v3.recurly.com/resources";
        $this->client->addScenario("HEAD", $url, NULL, '', "200 OK", ['Recurly-Total-Records' => 3]);

        $pager = new \Recurly\Pager($this->client, '/resources');

        $this->assertEquals($this->count, $pager->getCount());
    }

    public function testGetResponseChanges(): void
    {
        $this->pager->rewind(); // Loads first page (2 items)
        $response_one = $this->pager->getResponse();
        $this->pager->current(); // Steps page cursor
        $this->assertSame($response_one, $this->pager->getResponse());
        $this->pager->current(); // Steps page cursor
        $this->pager->valid(); // Page one complete so page two is requested
        $response_two = $this->pager->getResponse();
        $this->assertNotSame($response_one, $response_two);
    }

    public function testIteration(): void
    {
        $count = 0;
        foreach($this->pager as $resource) {<commit_msg>Updating getFirst Pager test to ensure that other params are preserved<commit_after><?php

use Recurly\Pager;
use Recurly\Logger;
use Psr\Log\LogLevel;

final class PagerTest extends RecurlyTestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        // Using LogLevel::EMERGENCY to minimize output when running tests
        $logger = new Logger('Recurly', LogLevel::EMERGENCY);
        $this->client = new MockClient($logger);
        $this->setUpRequest();

        $this->count = 3;
        $client_stub = $this->createMock(\Recurly\BaseClient::class);
        $client_stub->method('nextPage')->will($this->returnCallback(function($name, $params) {
            if (is_array($params) && array_key_exists('limit', $params) && $params['limit'] == 1) {
                $name = 'page_limit_1';
            }
            $json_string = $this->fixtures->loadJsonFixture($name, ['type' => 'string']);
            $response = new \Recurly\Response($json_string, $this->request);
            $response->setHeaders([
                'HTTP/1.1 200 OK',
                "Recurly-Total-Records: {$this->count}"
            ]);
            return $response->toResource();
        }));
        $client_stub->method('pagerCount')->will($this->returnCallback(function($name, $params) {
            $response = new \Recurly\Response('', $this->request);
            $response->setHeaders([
                'HTTP/1.1 200 OK',
                "Recurly-Total-Records: {$this->count}"
            ]);
            return $response;
        }));

        $this->pager = new \Recurly\Pager($client_stub, 'page_one');
    }

    public function testMapArrayParams(): void
    {
        $ids = ['id-1', 'id-2'];
        $idsCsv = join('%2C', $ids);
        $url = "https://v3.recurly.com/resources?ids=$idsCsv";
        $result = '{"object":"list","has_more":true,"next":"page_two","data":[{"object":"test_resource","name":"resource one"}]}';
        $this->client->addScenario("GET", $url, NULL, $result, "200 OK");

        $pager = new \Recurly\Pager($this->client, '/resources', [ 'params' => [ 'ids' => $ids ] ]);
        $pager->rewind();
        $this->assertInstanceOf(\Recurly\Response::class, $pager->getResponse());
    }

    public function testGetResponse(): void
    {
        $url = "https://v3.recurly.com/resources";
        $result = '{"object":"list","has_more":false,"next":null,"data":[{"object":"test_resource","name":"resource one"}]}';
        $this->client->addScenario("GET", $url, NULL, $result, "200 OK");

        $pager = new \Recurly\Pager($this->client, '/resources');
        $pager->rewind();
        $this->assertInstanceOf(\Recurly\Response::class, $pager->getResponse());
    }

    public function testGetFirst(): void
    {
        $url = "https://v3.recurly.com/resources?limit=1&another=param";
        $result = '{"object":"list","has_more":true,"next":"page_two","data":[{"object":"test_resource","name":"resource one"}]}';
        $this->client->addScenario("GET", $url, NULL, $result, "200 OK");

        $pager = new \Recurly\Pager($this->client, '/resources', [ 'params' => [ 'limit' => 5, 'another' => 'param' ] ]);
        $this->assertInstanceOf(
            \Recurly\Resources\TestResource::class,
            $pager->getFirst()
        );
    }

    public function testGetFirstNoResults(): void
    {
        $url = "https://v3.recurly.com/resources?limit=1";
        $result = '{"object":"list","has_more":false,"next":null,"data":[]}';
        $this->client->addScenario("GET", $url, NULL, $result, "200 OK");

        $pager = new \Recurly\Pager($this->client, '/resources');
        $this->assertNull($pager->getFirst());
    }

    public function testTake(): void
    {
        $url = "https://v3.recurly.com/resources?limit=2&another=param";
        $result = '{"object":"list","has_more":true,"next":"page_two","data":[
          {"object":"test_resource","name":"resource one"},
          {"object":"test_resource","name":"resource two"}
        ]}';        
        $this->client->addScenario("GET", $url, NULL, $result, "200 OK");

        $pager = new \Recurly\Pager($this->client, '/resources', [ 'params' => [ 'limit' => 200, 'another' => 'param' ] ]);
        $this->assertEquals(2, count($pager->take(2))
        );
    }

    public function testGetCount(): void
    {
        $url = "https://v3.recurly.com/resources";
        $this->client->addScenario("HEAD", $url, NULL, '', "200 OK", ['Recurly-Total-Records' => 3]);

        $pager = new \Recurly\Pager($this->client, '/resources');

        $this->assertEquals($this->count, $pager->getCount());
    }

    public function testGetResponseChanges(): void
    {
        $this->pager->rewind(); // Loads first page (2 items)
        $response_one = $this->pager->getResponse();
        $this->pager->current(); // Steps page cursor
        $this->assertSame($response_one, $this->pager->getResponse());
        $this->pager->current(); // Steps page cursor
        $this->pager->valid(); // Page one complete so page two is requested
        $response_two = $this->pager->getResponse();
        $this->assertNotSame($response_one, $response_two);
    }

    public function testIteration(): void
    {
        $count = 0;
        foreach($this->pager as $resource) {
{'fraction_non_alphanumeric': 0.19948359950272546, 'fraction_numerical': 0.00994549105862102, 'mean_word_length': 2.801526717557252, 'pattern_counts': {'":': 60, '<': 5, '<?xml version=': 0, '>': 161, 'https://': 12, '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': '313125', 'n_tokens_mistral': 3320, 'n_tokens_neox': 3221, 'n_words': 550}