This commit is contained in:
Mathieu 2024-01-11 10:33:29 +00:00
parent d42e2a5dd4
commit 3d17d2ff64
4 changed files with 58 additions and 15 deletions

View File

@ -2,7 +2,7 @@
"name": "multi-downloader-client",
"image": "rg.fr-par.scw.cloud/kubernetes/devcontainer:php-8.3",
"containerEnv": {
"MULTI_DOWNLOADER_API_KEY": "YOUR_API_KEY"
},
"customizations": {
"vscode": {

View File

@ -4,4 +4,16 @@ namespace Nwb\MultiDownloaderClient;
class MultiDownloaderClient
{
private string $apiKey;
public function __construct(string $apiKey = null)
{
$apiKey = $apiKey ?? getenv('MULTI_DOWNLOADER_API_KEY');
if (!$apiKey) {
throw new \InvalidArgumentException('API key is required');
}
$this->apiKey = $apiKey;
}
}

View File

@ -1,14 +0,0 @@
<?php
include __DIR__ . '/../vendor/autoload.php';
use Nwb\MultiDownloaderClient\MultiDownloaderClient;
use PHPUnit\Framework\TestCase;
class MultiDownloaderClienTest extends TestCase
{
public function testHello()
{
$client = new MultiDownloaderClient();
}
}

View File

@ -0,0 +1,45 @@
<?php
include __DIR__ . '/../vendor/autoload.php';
use Nwb\MultiDownloaderClient\MultiDownloaderClient;
use PHPUnit\Framework\TestCase;
class MultiDownloaderClientTest extends TestCase
{
public static function getProperty($object, $property)
{
$reflectedClass = new \ReflectionClass($object);
$reflection = $reflectedClass->getProperty($property);
$reflection->setAccessible(true);
return $reflection->getValue($object);
}
public function testApiKeyEnv()
{
putenv('MULTI_DOWNLOADER_API_KEY=1234567890');
$client = new MultiDownloaderClient();
$apiKey = self::getProperty($client, 'apiKey');
$this->assertEquals('1234567890', $apiKey);
}
public function testApiKeyParam()
{
$client = new MultiDownloaderClient('test_key');
$apiKey = self::getProperty($client, 'apiKey');
$this->assertEquals('test_key', $apiKey);
}
public function testApiKeyMissing()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('API key is required');
putenv('MULTI_DOWNLOADER_API_KEY');
new MultiDownloaderClient();
}
}