97 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
include __DIR__ . '/../vendor/autoload.php';
 | 
						|
 | 
						|
use Nwb\MultiDownloaderClient\FileRequest;
 | 
						|
use Nwb\MultiDownloaderClient\MultiDownloaderClient;
 | 
						|
use PHPUnit\Framework\TestCase;
 | 
						|
 | 
						|
class MultiDownloaderClientTest extends TestCase
 | 
						|
{
 | 
						|
    private $testFiles = [
 | 
						|
        'https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/portrait.png',
 | 
						|
        'https://s3.eu-central-1.wasabisys.com/dev-data-nwb/multi-downloader-sat/tests/img/watermarks/paysage.png'
 | 
						|
    ];
 | 
						|
 | 
						|
    private function testFiles()
 | 
						|
    {
 | 
						|
        return array_map(function ($url) {
 | 
						|
            return new FileRequest($url);
 | 
						|
        }, $this->testFiles);
 | 
						|
    }
 | 
						|
 | 
						|
    public static function getProperty($object, $property)
 | 
						|
    {
 | 
						|
        $reflectedClass = new \ReflectionClass($object);
 | 
						|
        $reflection = $reflectedClass->getProperty($property);
 | 
						|
        $reflection->setAccessible(true);
 | 
						|
 | 
						|
        return $reflection->getValue($object);
 | 
						|
    }
 | 
						|
 | 
						|
    public function testDownloadAsString()
 | 
						|
    {
 | 
						|
        $client = new MultiDownloaderClient();
 | 
						|
 | 
						|
        $client->setFiles($this->testFiles());
 | 
						|
 | 
						|
        $response = $client->downloadAsString();
 | 
						|
 | 
						|
        $md5 = md5($response);
 | 
						|
 | 
						|
        $this->assertEquals('ea9726d2ecbe6b820899ba125bf0ae94', $md5);
 | 
						|
    }
 | 
						|
 | 
						|
    public function testDownloadTo()
 | 
						|
    {
 | 
						|
        $client = new MultiDownloaderClient();
 | 
						|
 | 
						|
        $path = sys_get_temp_dir() . '/testDownloadTo.zip';
 | 
						|
 | 
						|
        $client->setFiles($this->testFiles());
 | 
						|
        $client->downloadTo($path);
 | 
						|
 | 
						|
        $md5 = md5_file($path);
 | 
						|
 | 
						|
        $this->assertFileExists($path);
 | 
						|
        $this->assertEquals('ea9726d2ecbe6b820899ba125bf0ae94', $md5);
 | 
						|
    }
 | 
						|
 | 
						|
    public function testApiKeyEnv()
 | 
						|
    {
 | 
						|
        putenv('MULTI_DOWNLOADER_API_KEY=1234567890');
 | 
						|
 | 
						|
        $client = new MultiDownloaderClient();
 | 
						|
        $apiKey = self::getProperty($client, 'apiKey');
 | 
						|
        $this->assertEquals('1234567890', $apiKey);
 | 
						|
    }
 | 
						|
 | 
						|
    public function testForm()
 | 
						|
    {
 | 
						|
        $client = new MultiDownloaderClient();
 | 
						|
        $client->setFiles($this->testFiles());
 | 
						|
 | 
						|
        echo $client->htmlForm($this->testFiles());
 | 
						|
    }
 | 
						|
 | 
						|
    public function testApiKeyParam()
 | 
						|
    {
 | 
						|
        $client = new MultiDownloaderClient([
 | 
						|
            'apiKey' => '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();
 | 
						|
    }
 | 
						|
}
 |