Skip to main content
Sign in
Snippets Groups Projects
Select Git revision
1 result Searching

SpeciesTest.php

Blame
  • SpeciesTest.php 4.63 KiB
    <?php declare(strict_types=1);
    
    namespace ResTelae\Gbif\Tests\Unit;
    
    use PHPUnit\Framework\TestCase;
    use ResTelae\Gbif\GbifException;
    use ResTelae\Gbif\Species;
    
    /**
     * @covers \ResTelae\Gbif\Species
     * @internal
     */
    final class SpeciesTest extends TestCase
    {
        /** @var Species */
        private $species;
    
        protected function setUp(): void
        {
            parent::setUp();
    
            $this->species = new Species();
        }
    
        public function testShouldThrowExceptionIfInvalidDataChoice(): void
        {
            $this->expectException(GbifException::class);
            $this->expectExceptionMessage('Illegal choice for `data`');
            $this->species->nameUsage([], 'invalid');
        }
    
        public function testShouldThrowExceptionIfNoDataKeyProvided(): void
        {
            $this->expectException(GbifException::class);
            $this->expectExceptionMessage('You must specify a key if `data` does not equal `all`');
            $this->species->nameUsage([], 'name');
        }
    
        public function testShouldThrowExceptionOnRootDataIfNoUuidOrShortNameProvided(): void
        {
            $this->expectException(GbifException::class);
            $this->expectExceptionMessage('`uuid` and `short_name` cannot be both NULL if `data` equals "root"');
            $this->species->nameUsage([], 'root', '123');
        }
    
        public function providerNameUsageList(): iterable
        {
            yield 'Empty args' => [[], 'all', null, null, null, ['offset' => 0, 'limit' => 100, 'endOfRecords' => false]];
            yield 'Root with UUID args' => [[], 'root', 63306619, 'd7dddbf4-2cf0-4f39-9b2a-bb099caae36c', null, ['offset' => 0, 'limit' => 100, 'endOfRecords' => true]];
        }
    
        /** @dataProvider providerNameUsageList */
        public function testShouldGetValidListResponseWithValidArguments(array $args, ?string $data, ?int $key, ?string $uuid, ?string $short_name, array $expected): void
        {
            $response = $this->species->nameUsage($args, $data, $key, $uuid, $short_name);
    
            $this->assertEquals($expected['offset'], $response['offset']);
            $this->assertEquals($expected['limit'], $response['limit']);
            $this->assertSame($expected['endOfRecords'], $response['endOfRecords']);
            $this->assertIsArray($response['results']);
        }
    
        public function providerNameUsageRecord(): iterable
        {
            yield '' => [[], 'name', 2435098, null, null, ['canonicalName' => 'Puma']];
        }
    
        /** @dataProvider providerNameUsageRecord */
        public function testShouldGetValidRecordResponseWithValidArguments(array $args, ?string $data, ?int $key, ?string $uuid, ?string $short_name, array $expected): void
        {