<?php

namespace NoahVet\Reef\Test\A_Unit\Business\Enumerator;

use NoahVet\Reef\Business\Enumerator\LegalStructureEnumerator;
use NoahVet\Reef\Jane\Client;
use NoahVet\Reef\Jane\Model\LegalStructureResponseItem;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\NetworkExceptionInterface;

class LegalStructureEnumeratorTest extends TestCase
{
    public function testEnumerateApiDown(): void
    {
        $client = $this->getMockBuilder(Client::class)
            ->disableOriginalConstructor()
            ->getMock()
        ;

        $client
            ->method('getAllLegalStructure')
            ->withAnyParameters()
            ->willThrowException(
                $this
                    ->getMockBuilder(NetworkExceptionInterface::class)
                    ->disableOriginalConstructor()
                    ->getMock(),
            )
        ;

        $enumerator = new LegalStructureEnumerator($client);

        self::expectException(NetworkExceptionInterface::class);

        \iterator_to_array($enumerator->enumerateAll());
    }

    public function testEnumerateAll(): void
    {
        $client = $this->getMockBuilder(Client::class)
            ->disableOriginalConstructor()
            ->getMock()
        ;

        $structure = new LegalStructureResponseItem();

        $client
            ->expects(self::exactly(1))
            ->method('getAllLegalStructure')
            ->willReturnCallback(static function (array $parameters) use ($structure): array {
                self::assertSame(['page' => 0, 'limit' => 50], $parameters);

                return [$structure];
            })
        ;

        $enumerator = new LegalStructureEnumerator($client);

        $structures = \iterator_to_array($enumerator->enumerateAll());
        self::assertCount(1, $structures);
        self::assertSame($structure, $structures[0]);
    }
}
