Skip to content

Commit

Permalink
Clean up some testing
Browse files Browse the repository at this point in the history
  • Loading branch information
whikloj committed Nov 21, 2024
1 parent 6be903b commit 8871310
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/CurlInstance.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools;

use CurlHandle;
Expand Down
2 changes: 2 additions & 0 deletions src/DownloadFile.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools;

use whikloj\BagItTools\Exceptions\BagItException;
Expand Down
2 changes: 2 additions & 0 deletions tests/BagItWebserverFramework.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools\Test;

use donatj\MockWebServer\MockWebServer;
Expand Down
31 changes: 31 additions & 0 deletions tests/CurlInstanceTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools\Test;

use whikloj\BagItTools\CurlInstance;

/**
* Class CurlInstanceTest
* @package whikloj\BagItTools\Test
* @coversDefaultClass \whikloj\BagItTools\CurlInstance
* @since 5.0.0
* @author whikloj
*/
class CurlInstanceTest extends BagItTestFramework
{
/**
* @covers ::createCurl
* @covers ::setupCurl
*/
public function testCreateCurl(): void
{
$mock = new class
Expand All @@ -17,6 +30,24 @@ public function testCreateCurl(): void
$this->assertInstanceOf(\CurlHandle::class, $handle);
}

/**
* @covers ::createCurl
* @covers ::setupCurl
*/
public function testCreateSingleCurl(): void
{
$mock = new class
{
use CurlInstance;
}; // anonymous class

$handle = $mock->createCurl('http://example.com', true);
$this->assertInstanceOf(\CurlHandle::class, $handle);
}

/**
* @covers ::createMultiCurl
*/
public function testCurlMulti(): void
{
$mock = new class
Expand Down
90 changes: 90 additions & 0 deletions tests/DownloadFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools\Test;

use whikloj\BagItTools\DownloadFile;
use whikloj\BagItTools\Exceptions\BagItException;

/**
* Class DownloadFileTest
* @package whikloj\BagItTools\Test
* @coversDefaultClass \whikloj\BagItTools\DownloadFile
* @since 5.0.0
* @author whikloj
*/
class DownloadFileTest extends BagItTestFramework
{
/**
* @covers ::__construct
* @covers ::getUrl
* @covers ::getDestination
* @covers ::getSize
* @covers ::getSizeString
*/
public function testCreateAndGet(): void
{
$file = new DownloadFile('uri', 'data/destination.txt');
$this->assertInstanceOf(DownloadFile::class, $file);
$this->assertEquals('uri', $file->getUrl());
$this->assertEquals('data/destination.txt', $file->getDestination());
$this->assertNull($file->getSize());
$this->assertEquals("-", $file->getSizeString());
}

/**
* @covers ::__construct
* @covers ::getUrl
* @covers ::getDestination
* @covers ::getSize
* @covers ::getSizeString
*/
public function testCreateAndGetSize(): void
{
$file = new DownloadFile('uri', 'data/destination.txt', 1024);
$this->assertInstanceOf(DownloadFile::class, $file);
$this->assertEquals('uri', $file->getUrl());
$this->assertEquals('data/destination.txt', $file->getDestination());
$this->assertEquals(1024, $file->getSize());
$this->assertEquals("1024", $file->getSizeString());
}

/**
* @covers ::__construct
* @covers ::validateDownload
* @covers ::validateUrl
* @covers ::internalValidateUrl
*/
public function testValidate(): void
{
$file = new DownloadFile('http://somehost.io/filename', 'data/destination.txt');
$file->validateDownload();
$this->assertTrue(true);
}

/**
* @covers ::__construct
* @covers ::validateDownload
* @covers ::validateUrl
*/
public function testInvalidUrl(): void
{
$file = new DownloadFile('//somehost.io/filename', 'data/destination.txt');
$this->expectException(BagItException::class);
$file->validateDownload();
}

/**
* @covers ::__construct
* @covers ::validateDownload
* @covers ::validateUrl
* @covers ::internalValidateUrl
*/
public function testInvalidUrlScheme(): void
{
$file = new DownloadFile('ftp://somehost.io/filename', 'data/destination.txt');
$this->expectException(BagItException::class);
$file->validateDownload();
}
}
2 changes: 2 additions & 0 deletions tests/Profiles/BagItProfileBarTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools\Test\Profiles;

use whikloj\BagItTools\Profiles\BagItProfile;
Expand Down
2 changes: 2 additions & 0 deletions tests/Profiles/BagItProfileFooTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools\Test\Profiles;

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Profiles/BagProfileTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools\Test\Profiles;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions tests/Profiles/InternalProfileTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools\Test\Profiles;

use ReflectionException;
Expand Down
117 changes: 117 additions & 0 deletions tests/Profiles/ProfileTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace whikloj\BagItTools\Test\Profiles;

use whikloj\BagItTools\Profiles\ProfileTags;
use whikloj\BagItTools\Test\BagItTestFramework;

/**
* Class ProfileTagTest
* @package whikloj\BagItTools\Test\Profiles
* @coversDefaultClass \whikloj\BagItTools\Profiles\ProfileTags
* @since 5.0.0
* @group Profiles
* @author whikloj
*/
class ProfileTagTest extends BagItTestFramework
{
/**
* @covers ::__construct
* @covers ::getTag
* @covers ::isRequired
* @covers ::getValues
* @covers ::isRepeatable
* @covers ::getDescription
*/
public function testBasicTag(): void
{
$tag = new ProfileTags(
'Bag-Size',
true,
[
'some value',
],
false,
'The size of the bag in bytes'
);
$this->assertEquals('Bag-Size', $tag->getTag());
$this->assertTrue($tag->isRequired());
$this->assertArrayEquals(['some value'], $tag->getValues());
$this->assertFalse($tag->isRepeatable());
$this->assertEquals('The size of the bag in bytes', $tag->getDescription());
}

/**
* @covers ::setOtherTagOptions
* @covers ::getOtherTagOptions
*/
public function testOtherTags(): void
{
$tag = new ProfileTags(
'Bag-Size',
true,
[
'some value',
],
false,
'The size of the bag in bytes'
);

$otherOptions = [
'some' => 'value',
'another' => 'thing',
];

$method = $this->getReflectionMethod('\whikloj\BagItTools\Profiles\ProfileTags', 'setOtherTagOptions');

$method->invoke($tag, $otherOptions);

$this->assertArrayEquals($otherOptions, $tag->getOtherTagOptions());
}

/**
* @covers ::fromJson
* @covers ::getTag
* @covers ::isRequired
* @covers ::getValues
* @covers ::isRepeatable
* @covers ::getDescription
*/
public function testFromJsonBasic(): void
{
$json = '{"required": true, "values": ["some value"], "repeatable": false, '
. '"description": "The size of the bag in bytes"}';
$decoded = json_decode($json, true);
$tag = ProfileTags::fromJson("Bag-Size", $decoded);
$this->assertEquals('Bag-Size', $tag->getTag());
$this->assertTrue($tag->isRequired());
$this->assertArrayEquals(['some value'], $tag->getValues());
$this->assertFalse($tag->isRepeatable());
$this->assertEquals('The size of the bag in bytes', $tag->getDescription());
}

/**
* @covers ::fromJson
* @covers ::getTag
* @covers ::isRequired
* @covers ::getValues
* @covers ::isRepeatable
* @covers ::getDescription
* @covers ::getOtherTagOptions
*/
public function testFromJsonExtended(): void
{
$json = '{"required": true, "values": ["some value"], "repeatable": false, '
. '"description": "The size of the bag in bytes", "other": {"some": "value", "another": "thing"}}';
$decoded = json_decode($json, true);
$tag = ProfileTags::fromJson("Bag-Size", $decoded);
$this->assertEquals('Bag-Size', $tag->getTag());
$this->assertTrue($tag->isRequired());
$this->assertArrayEquals(['some value'], $tag->getValues());
$this->assertFalse($tag->isRepeatable());
$this->assertEquals('The size of the bag in bytes', $tag->getDescription());
$other = $tag->getOtherTagOptions();
$this->assertArrayHasKey('other', $other);
$this->assertArrayEquals(['some' => 'value', 'another' => 'thing'], $other['other']);
}
}
2 changes: 2 additions & 0 deletions tests/Profiles/ProfileTestFramework.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools\Test\Profiles;

use whikloj\BagItTools\Bag;
Expand Down
2 changes: 2 additions & 0 deletions tests/Profiles/ProfileWebTests.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace whikloj\BagItTools\Test\Profiles;

use whikloj\BagItTools\Bag;
Expand Down

0 comments on commit 8871310

Please sign in to comment.