<?php

declare(strict_types=1);

namespace KupShop\CatalogBundle\Tests;

class GenericObjectsIndexTest extends \DatabaseTestCase
{
    private GenericObjectsIndex $indexUtil;

    protected function setUp(): void
    {
        parent::setUp();

        $this->indexUtil = $this->autowire(GenericObjectsIndex::class);
    }

    private const BATCH_CHUNK_SIZE = 2;

    public function testBatching()
    {
        $batches = [];
        foreach ($this->indexUtil->getBatchedObjects(self::BATCH_CHUNK_SIZE) as $batch) {
            $batches[] = $batch;
        }

        $this->assertCount(2, $batches);
        $this->assertEquals(iterator_to_array($this->indexUtil->getPages()), array_merge(...$batches));

        $last = array_pop($batches);
        foreach ($batches as $batch) {
            $this->assertCount(self::BATCH_CHUNK_SIZE, $batch);
        }

        $this->assertTrue(count($last) <= self::BATCH_CHUNK_SIZE, 'Last batch should be smaller or equal to 3. Actual: '.var_export($last, true));
    }

    protected function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }
}

class GenericObjectsIndex extends \KupShop\CatalogBundle\Search\GenericObjectsIndex
{
    public function getPages(): \Generator
    {
        return $this->pages();
    }
}
