Version 3 release. Major re-write

This commit is contained in:
David Wakelin 2021-08-09 17:33:50 +01:00
commit f9b49002c7
1507 changed files with 6592 additions and 94204 deletions

80
tests/FileTest.php Normal file
View file

@ -0,0 +1,80 @@
<?php
use Dotenv\Dotenv;
use PHPUnit\Framework\TestCase;
use SpacesAPI\Exceptions\FileDoesntExistException;
use SpacesAPI\Exceptions\SpaceDoesntExistException;
use SpacesAPI\Spaces;
class FileTest extends TestCase
{
private static $space;
private static $file;
public static function setUpBeforeClass(): void
{
$dotenv = Dotenv::createImmutable(__DIR__ . "/..");
$dotenv->load();
$dotenv->required(['SPACES_KEY', 'SPACES_SECRET']);
$spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']);
try {
$spaces->space('spaces-api-test')->destroySpace();
} catch (SpaceDoesntExistException $e) {
}
self::$space = $spaces->create('spaces-api-test');
self::$file = self::$space->uploadText('Lorem ipsum', 'lorem-ipsum.txt');
}
public static function tearDownAfterClass(): void
{
(new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace();
}
public function testCanUpdatePrivacy()
{
$this->assertFalse(self::$file->isPublic());
self::$file->makePublic();
$this->assertTrue(self::$file->isPublic());
self::$file->makePrivate();
$this->assertFalse(self::$file->isPublic());
}
public function testCanGetContents()
{
$this->assertEquals("Lorem ipsum", self::$file->getContents());
}
public function testCanDownloadFile()
{
$filename = sys_get_temp_dir() . "/lorem.txt";
self::$file->download($filename);
$this->assertEquals("Lorem ipsum", file_get_contents($filename));
}
public function testCanCopyFile()
{
$this->expectNotToPerformAssertions();
self::$file->copy('lorem-ipsum-2.txt');
self::$space->file('lorem-ipsum-2.txt');
}
public function testCanGetURL()
{
$this->assertStringContainsString('lorem-ipsum.txt', self::$file->getURL());
$this->assertStringContainsString('lorem-ipsum.txt', self::$file->getSignedURL());
}
public function testCanDeleteFile()
{
self::$file->delete();
$this->expectException(FileDoesntExistException::class);
self::$space->file('lorem-ipsum.txt');
}
}

164
tests/SpaceTest.php Normal file
View file

@ -0,0 +1,164 @@
<?php
use Dotenv\Dotenv;
use PHPUnit\Framework\TestCase;
use SpacesAPI\Exceptions\FileDoesntExistException;
use SpacesAPI\Exceptions\SpaceDoesntExistException;
use SpacesAPI\File;
use SpacesAPI\Spaces;
class SpaceTest extends TestCase
{
private static $space;
public static function setUpBeforeClass(): void
{
$dotenv = Dotenv::createImmutable(__DIR__ . "/..");
$dotenv->load();
$dotenv->required(['SPACES_KEY', 'SPACES_SECRET']);
$spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']);
try {
$spaces->space('spaces-api-test')->destroySpace();
} catch (SpaceDoesntExistException $e) {
}
self::$space = $spaces->create('spaces-api-test');
}
public static function tearDownAfterClass(): void
{
// (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace();
}
public function testCanUpdateSpacePrivacy()
{
$this->assertFalse(self::$space->isPublic());
self::$space->makePublic();
$this->assertTrue(self::$space->isPublic());
self::$space->makePrivate();
$this->assertFalse(self::$space->isPublic());
}
public function testCanAddCORSRule()
{
$this->assertNull(self::$space->getCORS());
self::$space->addCORSOrigin("http://example.com", ['GET', 'PUT'], 3200, ['custom-header']);
$cors = self::$space->getCORS();
$this->assertIsArray($cors);
$this->assertEquals('custom-header', $cors[0]['AllowedHeaders'][0]);
$this->assertEquals('GET', $cors[0]['AllowedMethods'][0]);
$this->assertEquals('PUT', $cors[0]['AllowedMethods'][1]);
$this->assertEquals('http://example.com', $cors[0]['AllowedOrigins'][0]);
$this->assertEquals(3200, $cors[0]['MaxAgeSeconds']);
self::$space->removeCORSOrigin('http://example.com');
$this->assertNull(self::$space->getCORS());
}
public function testFileDoesntExistException()
{
$this->expectException(FileDoesntExistException::class);
self::$space->file("non-existent.txt");
}
public function testCanUploadText()
{
$file = self::$space->uploadText("Lorem ipsum", "lorem-ipsum.txt");
$this->assertInstanceOf(File::class, $file);
}
public function testCanUploadFile()
{
$tmpFile = tempnam(sys_get_temp_dir(), 'spaces-test');
$file = self::$space->uploadFile($tmpFile, 'upload-test.txt');
$this->assertInstanceOf(File::class, $file);
}
/**
* @depends testCanUploadText
* @depends testCanUploadFile
*/
public function testFileExists()
{
$file = self::$space->file('lorem-ipsum.txt');
$this->assertInstanceOf(File::class, $file);
$file = self::$space->file('upload-test.txt');
$this->assertInstanceOf(File::class, $file);
}
/**
* @depends testCanUploadText
* @depends testCanUploadFile
*/
public function testCanListFiles()
{
$files = self::$space->listFiles()['files'];
$this->assertIsArray($files);
$this->assertCount(2, $files);
$this->assertInstanceOf(File::class, $files[0]);
foreach ($files as $file) {
$file->delete();
}
}
public function testCanUploadDirectory()
{
$localDirectory = sys_get_temp_dir() . "/spaces-upload-test";
@mkdir($localDirectory);
for($i=1; $i<=10; $i++) {
file_put_contents("$localDirectory/test-$i.txt", "Lorem ipsum $i");
}
self::$space->uploadDirectory($localDirectory, 'remote-dir');
$list = self::$space->listFiles()['files'];
$this->assertIsArray($list);
$this->assertCount(10, $list);
for ($i = 1; $i <= 10; $i++) {
unlink("$localDirectory/test-$i.txt");
}
$this->assertCount(2, scandir($localDirectory));
}
/**
* @depends testCanUploadDirectory
*/
public function testCanDownloadDirectory()
{
$localDirectory = sys_get_temp_dir() . "/spaces-upload-test";
$this->assertCount(2, scandir($localDirectory));
self::$space->downloadDirectory($localDirectory, 'remote-dir');
$this->assertCount(12, scandir($localDirectory));
for ($i = 1; $i <= 10; $i++) {
unlink("$localDirectory/test-$i.txt");
}
$this->assertCount(2, scandir($localDirectory));
}
public function testCanDeleteDirectory()
{
$list = self::$space->listFiles()['files'];
$this->assertIsArray($list);
$this->assertCount(10, $list);
self::$space->deleteDirectory('remote-dir');
$list = self::$space->listFiles()['files'];
$this->assertIsArray($list);
$this->assertCount(0, $list);
}
}

88
tests/SpacesTest.php Normal file
View file

@ -0,0 +1,88 @@
<?php
use Dotenv\Dotenv;
use PHPUnit\Framework\TestCase;
use SpacesAPI\Exceptions\AuthenticationException;
use SpacesAPI\Exceptions\SpaceDoesntExistException;
use SpacesAPI\Exceptions\SpaceExistsException;
use SpacesAPI\Space;
use SpacesAPI\Spaces;
class SpacesTest extends TestCase
{
public static function setUpBeforeClass(): void
{
$dotenv = Dotenv::createImmutable(__DIR__ . "/..");
$dotenv->load();
$dotenv->required(['SPACES_KEY', 'SPACES_SECRET']);
try {
(new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace();
} catch (SpaceDoesntExistException $e) {
}
}
public static function tearDownAfterClass(): void
{
(new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace();
}
public function testAuthenticationCanFail()
{
$this->expectException(AuthenticationException::class);
new Spaces("fake", "fake");
}
public function testCanAuthenticate()
{
$this->expectNotToPerformAssertions();
return new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']);
}
/**
* @depends testCanAuthenticate
*/
public function testCreateSpaceFailsWithExistingSpace(Spaces $spaces)
{
$this->expectException(SpaceExistsException::class);
$spaces->create('test');
}
/**
* @depends testCanAuthenticate
*/
public function testCanCreateSpace(Spaces $spaces)
{
$space = $spaces->create('spaces-api-test');
$this->assertInstanceOf(Space::class, $space);
return $space;
}
/**
* @depends testCanAuthenticate
*/
public function testCanListSpaces(Spaces $spaces)
{
$list = $spaces->list();
$this->assertIsArray($list);
$spaceFound = false;
foreach ($list as $space) {
if ($space->getName() == 'spaces-api-test') {
$spaceFound = true;
}
}
$this->assertTrue($spaceFound);
}
/**
* @depends testCanAuthenticate
*/
public function testUseSpaceFailsWithNonExistentSpace(Spaces $spaces)
{
$this->expectException(SpaceDoesntExistException::class);
$spaces->space(md5(time()));
}
}