From c1a97722125c646a47515cbc183c226667801da3 Mon Sep 17 00:00:00 2001
From: Gwendolen Lynch <gwendolen.lynch@gmail.com>
Date: Wed, 3 Aug 2022 16:53:58 +0200
Subject: [PATCH] Species name usage queries for UUID or short name require
 species/root/ URI prefix
---
 src/Species.php            |  2 +-
 tests/Unit/SpeciesTest.php | 30 +++++++++++++++++++++++++-----
 2 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/src/Species.php b/src/Species.php
index d0ce03d..0e03a87 100644
--- a/src/Species.php
+++ b/src/Species.php
@@ -128,7 +128,7 @@ class Species extends Gbif {
       if (!$uuid && !$short_name) {
         throw new GbifException('`uuid` and `short_name` cannot be both NULL if `data` equals "root"');
       }
-      $uri = $uuid ? 'species/' . $uuid : 'species/' . $short_name;
+      $uri = $uuid ? 'species/root/' . $uuid : 'species/root/' . $short_name;
     }
     else {
       $uri = 'species/' . $key . '/' . $data;
diff --git a/tests/Unit/SpeciesTest.php b/tests/Unit/SpeciesTest.php
index 0c2a7e5..ef19747 100644
--- a/tests/Unit/SpeciesTest.php
+++ b/tests/Unit/SpeciesTest.php
@@ -39,16 +39,36 @@ final class SpeciesTest extends TestCase
         $this->species->nameUsage([], 'root', '123');
     }
 
-    public function testShouldGetValidResponseWithValidArguments()
+    public function providerNameUsageList(): iterable
     {
-        $response = $this->species->nameUsage([]);
+        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]];
+    }
 
-        $this->assertEquals(0, $response['offset']);
-        $this->assertEquals(100, $response['limit']);
-        $this->assertFalse($response['endOfRecords']);
+    /** @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
+    {
+        $response = $this->species->nameUsage($args, $data, $key, $uuid, $short_name);
+
+        $this->assertSame($expected['canonicalName'], $response['canonicalName']);
+    }
+
     public function testShouldGetValidResponseFromNameUsageByKey()
     {
         $response = $this->species->nameUsageByKey(5231190);
-- 
GitLab