From 20e6adbc9a5f45907ee704d42ab4a8a2472652f9 Mon Sep 17 00:00:00 2001
From: Francesco Abeni <francesco.abeni@gmail.com>
Date: Sun, 1 May 2022 14:49:05 +0200
Subject: [PATCH] Add unit tests for Species class

---
 .gitignore                 |   3 +
 composer.json              |   9 +++
 tests/Unit/SpeciesTest.php | 112 +++++++++++++++++++++++++++++++++++++
 3 files changed, 124 insertions(+)
 create mode 100644 tests/Unit/SpeciesTest.php

diff --git a/.gitignore b/.gitignore
index 2b039a2..7a68906 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,6 @@
 /composer.lock
 /public
 /docs
+
+# IDE stuff
+.idea
diff --git a/composer.json b/composer.json
index 19401fb..337c98d 100644
--- a/composer.json
+++ b/composer.json
@@ -17,10 +17,19 @@
         "php": ">=7.2",
         "guzzlehttp/guzzle": "^6.0|^7.0"
     },
+    "require-dev": {
+        "phpunit/phpunit": "8.5.x-dev"
+    },
     "support": {
         "source": "https://gitlab.res-telae.cat/restelae/php-gbif"
     },
     "autoload": {
         "psr-4": {"ResTelae\\Gbif\\": "src"}
+    },
+    "autoload-dev": {
+        "psr-4": {"ResTelae\\Gbif\\Tests\\": "tests/"}
+    },
+    "scripts": {
+        "test": "vendor/bin/phpunit --colors tests/"
     }
 }
diff --git a/tests/Unit/SpeciesTest.php b/tests/Unit/SpeciesTest.php
new file mode 100644
index 0000000..07eab49
--- /dev/null
+++ b/tests/Unit/SpeciesTest.php
@@ -0,0 +1,112 @@
+<?php declare(strict_types=1);
+
+namespace ResTelae\Gbif\Tests\Unit;
+
+use PHPUnit\Framework\TestCase;
+use ResTelae\Gbif\Species;
+
+final class SpeciesTest extends TestCase
+{
+    /** @var Species */
+    private $species;
+
+    protected function setUp(): void
+    {
+        parent::setUp();
+
+        $this->species = new Species();
+    }
+
+    public function testShouldThrowExceptionIfInvalidDataChoice()
+    {
+        $this->expectExceptionMessage('Illegal choice for `data`');
+        $this->species->nameUsage([], 'invalid');
+
+        $this->fail("No exception raised");
+    }
+
+    public function testShouldThrowExceptionIfNoDataKeyProvided()
+    {
+        $this->expectExceptionMessage('You must specify a key if `data` does not equal `all`');
+        $this->species->nameUsage([], 'name');
+
+        $this->fail("No exception raised");
+    }
+
+    public function testShouldThrowExceptionOnRootDataIfNoUuidOrShortNameProvided()
+    {
+        $this->expectExceptionMessage('`uuid` and `short_name` cannot be both NULL if `data` equals "root"');
+        $this->species->nameUsage([], 'root', '123');
+
+        $this->fail("No exception raised");
+    }
+
+    public function testShouldGetValidResponseWithValidArguments()
+    {
+        $response = $this->species->nameUsage([]);
+
+        $this->assertEquals(0, $response['offset']);
+        $this->assertEquals(100, $response['limit']);
+        $this->assertFalse($response['endOfRecords']);
+        $this->assertIsArray($response['results']);
+    }
+
+    public function testShouldGetValidResponseFromNameUsageByKey()
+    {
+        $response = $this->species->nameUsageByKey(5231190);
+
+        $this->assertEquals(5231190, $response['key']);
+        $this->assertEquals('Animalia', $response['kingdom']);
+        $this->assertEquals('Passer domesticus', $response['species']);
+    }
+
+    public function testShouldGetValidResponseFromNameBackbone()
+    {
+        $response = $this->species->nameBackbone(
+            [
+                'verbose' => true,
+                'kingdom' => 'Plantae',
+                'name'    => 'Oenante',
+            ]
+        );
+
+        $this->assertEquals(3034893, $response['usageKey']);
+        $this->assertEquals('Oenanthe', $response['canonicalName']);
+        $this->assertEquals('GENUS', $response['rank']);
+    }
+
+    public function testShouldGetValidResponseFromNameLookup()
+    {
+        $response = $this->species->nameLookup(
+            [
+                'q'      => 'Puma',
+                'rank'   => 'GENUS',
+                'offset' => 0,
+                'limit'  => 20,
+            ]
+        );
+
+        $this->assertEquals(0, $response['offset']);
+        $this->assertEquals(20, $response['limit']);
+        $this->assertIsArray($response['results']);
+        $this->assertEquals('Puma', $response['results'][0]['genus']);
+        $this->assertEquals('Puma', $response['results'][0]['canonicalName']);
+    }
+
+    public function testShouldGetValidResponseFromNameSuggest()
+    {
+        $response = $this->species->nameSuggest(
+            [
+                'q'      => 'Pum',
+                'rank'   => 'GENUS',
+                'offset' => 0,
+                'limit'  => 20,
+            ]
+        );
+
+        $this->assertIsArray($response);
+        $this->assertCount(20, $response);
+        $this->assertEquals('Pumililema', $response[0]['genus']);
+        $this->assertEquals('Pumililema', $response[0]['canonicalName']);
+    }
+}
-- 
GitLab