Added many unit tests

This commit is contained in:
Chris Kankiewicz
2019-12-27 17:29:44 -07:00
parent f084dac14d
commit 83cc516906
17 changed files with 399 additions and 8 deletions

View File

@@ -7,7 +7,7 @@
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<directory suffix=".php">app/</directory>
</whitelist>
</filter>
</phpunit>

View File

@@ -0,0 +1,24 @@
<?php
namespace Tests\Unit\Bootstrap;
use App\Bootstrap\ConfigComposer;
use DI\Container;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
class ConfigComposerTest extends TestCase
{
public function test_it_can_compose_the_config_component()
{
$container = new Container();
(new ConfigComposer($container))();
$config = $container->get(Config::class);
$this->assertInstanceOf(Config::class, $config);
$this->assertTrue($config->has('app'));
$this->assertTrue($config->has('icons'));
$this->assertTrue($config->has('view'));
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Tests\Unit\Bootstrap;
use App\Bootstrap\FinderComposer;
use DI\Container;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;
class FinderComposerTest extends TestCase
{
public function test_it_can_compose_the_finder_component()
{
$container = new Container();
(new FinderComposer($container, new Config))();
$finder = $container->get(Finder::class);
$this->assertInstanceOf(Finder::class, $finder);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Bootstrap\SortMethods;
use App\Bootstrap\SortMethods\Accessed;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;
class AccessedTest extends TestCase
{
public function test_it_can_sort_by_accessed_time(): void
{
$finder = $this->createMock(Finder::class);
$finder->expects($this->once())->method('sortByAccessedTime');
(new Accessed)($finder);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Bootstrap\SortMethods;
use App\Bootstrap\SortMethods\Changed;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;
class ChangedTest extends TestCase
{
public function test_it_can_sort_by_changed_time(): void
{
$finder = $this->createMock(Finder::class);
$finder->expects($this->once())->method('sortByChangedTime');
(new Changed)($finder);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Bootstrap\SortMethods;
use App\Bootstrap\SortMethods\Modified;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;
class ModifiedTest extends TestCase
{
public function test_it_can_sort_by_modified_time(): void
{
$finder = $this->createMock(Finder::class);
$finder->expects($this->once())->method('sortByModifiedTime');
(new Modified)($finder);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Bootstrap\SortMethods;
use App\Bootstrap\SortMethods\Name;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;
class NameTest extends TestCase
{
public function test_it_can_sort_by_file_name(): void
{
$finder = $this->createMock(Finder::class);
$finder->expects($this->once())->method('sortByName')->with(null);
(new Name)($finder);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Bootstrap\SortMethods;
use App\Bootstrap\SortMethods\Natural;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;
class NaturalTest extends TestCase
{
public function test_it_can_sort_by_natural_file_name(): void
{
$finder = $this->createMock(Finder::class);
$finder->expects($this->once())->method('sortByName')->with(true);
(new Natural)($finder);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Unit\Bootstrap\SortMethods;
use App\Bootstrap\SortMethods\Type;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;
class TypeTest extends TestCase
{
public function test_it_can_sort_by_file_type(): void
{
$finder = $this->createMock(Finder::class);
$finder->expects($this->once())->method('sortByType');
(new Type)($finder);
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Tests\Unit\Bootstrap;
use App\Bootstrap\ViewComposer;
use App\Bootstrap\ViewFunctions;
use DI\Container;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
use Slim\Views\Twig;
class ViewComposerTest extends TestCase
{
public function test_it_can_compose_the_view_component()
{
$container = new Container();
(new ViewComposer($container, new Config))();
$twig = $container->get(Twig::class);
$this->assertInstanceOf(Twig::class, $twig);
$this->assertEquals('app/cache/views', $twig->getEnvironment()->getCache());
$this->assertInstanceOf(
ViewFunctions\Asset::class,
$twig->getEnvironment()->getFunction('asset')->getCallable()
);
$this->assertInstanceOf(
ViewFunctions\Config::class,
$twig->getEnvironment()->getFunction('config')->getCallable()
);
$this->assertInstanceOf(
ViewFunctions\Icon::class,
$twig->getEnvironment()->getFunction('icon')->getCallable()
);
$this->assertInstanceOf(
ViewFunctions\SizeForHumans::class,
$twig->getEnvironment()->getFunction('sizeForHumans')->getCallable()
);
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Tests\Unit\Bootstrap\ViewFunctions;
use App\Bootstrap\ViewFunctions\Asset;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
class AssetTest extends TestCase
{
public function test_it_can_return_an_asset_path(): void
{
$asset = new Asset($this->createMock(Config::class));
$this->assertEquals('/app/dist/test.css', $asset('test.css'));
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Tests\Unit\Bootstrap\ViewFunctions;
use App\Bootstrap\ViewFunctions\Config;
use PHLAK\Config\Config as AppConfig;
use PHPUnit\Framework\TestCase;
class ConfigTest extends TestCase
{
/** @var AppConfig Application config */
protected $config;
public function setUp(): void
{
$this->config = new AppConfig([
'foo' => false,
'bar' => 'Red herring',
'view' => [
'foo' => 'Test value; please ignore'
],
]);
}
public function test_it_can_retrieve_a_config_item(): void
{
$config = new Config($this->config);
$this->assertEquals('Test value; please ignore', $config('foo'));
}
public function test_it_returns_a_default_value(): void
{
$config = new Config($this->config);
$this->assertEquals('Default value', $config('bar', 'Default value'));
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Tests\Unit\Bootstrap\ViewFunctions;
use App\Bootstrap\ViewFunctions\Icon;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\SplFileInfo;
class IconTest extends TestCase
{
/** @var Config Application config */
protected $config;
public function setUp(): void
{
$this->config = new Config([
'php' => false,
'icons' => [
'php' => 'fab fa-php',
],
]);
}
public function test_it_can_return_icon_markup_for_a_file(): void
{
$icon = new Icon($this->config);
$file = $this->createMock(SplFileInfo::class);
$file->method('isDir')->willReturn(false);
$file->method('getExtension')->willReturn('php');
$this->assertEquals('<i class="fab fa-php fa-fw fa-lg"></i>', $icon($file));
}
public function test_it_can_return_icon_markup_for_a_directory(): void
{
$icon = new Icon($this->config);
$file = $this->createMock(SplFileInfo::class);
$file->method('isDir')->willReturn(true);
$this->assertEquals('<i class="fas fa-folder fa-fw fa-lg"></i>', $icon($file));
}
public function test_it_can_return_the_default_icon_markup(): void
{
$icon = new Icon($this->config);
$file = $this->createMock(SplFileInfo::class);
$file->method('isDir')->willReturn(false);
$file->method('getExtension')->willReturn('default');
$this->assertEquals('<i class="fas fa-file fa-fw fa-lg"></i>', $icon($file));
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Unit\Bootstrap\ViewFunctions;
use App\Bootstrap\ViewFunctions\SizeForHumans;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
class SizeForHumansTest extends TestCase
{
public function test_it_can_convert_bytes_to_bytes(): void
{
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.00B', $sizeForHumans(13));
}
public function test_it_can_convert_bytes_to_kibibytes(): void
{
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.37KB', $sizeForHumans(13690));
}
public function test_it_can_convert_bytes_to_mebibytes(): void
{
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.37MB', $sizeForHumans(14019461));
}
public function test_it_can_convert_bytes_to_gibibytes(): void
{
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.37GB', $sizeForHumans(14355900000));
}
public function test_it_can_convert_bytes_to_tebibytes(): void
{
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.37TB', $sizeForHumans(14700500000000));
}
public function test_it_can_convert_bytes_to_pebibytes(): void
{
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('13.37PB', $sizeForHumans(15053300000000000));
}
public function test_it_can_convert_bytes_to_exbibytes(): void
{
$sizeForHumans = new SizeForHumans($this->createMock(Config::class));
$this->assertEquals('8.00EB', $sizeForHumans(PHP_INT_MAX));
}
}

View File

@@ -3,6 +3,7 @@
namespace Tests\Controllers;
use App\Controllers\DirectoryController;
use DI\Container;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
@@ -15,13 +16,14 @@ class DirectoryControllerTest extends TestCase
public function test_it_returns_a_response()
{
$controller = new DirectoryController(
$this->createMock(Container::class),
$this->createMock(Config::class),
$this->createMock(Twig::class)
);
$response = $controller(
$this->createMock(Finder::class),
$this->createMock(Response::class),
new Response(),
'tests/files'
);

View File

@@ -0,0 +1,23 @@
<?php
namespace Tests\Unit\Controllers;
use App\Controllers\FileInfoController;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Slim\Psr7\Response;
class FileInfoControllerTest extends TestCase
{
public function test_it_returns_a_response()
{
$controller = new FileInfoController(
$this->createMock(Config::class)
);
$response = $controller(new Response(), 'tests/files');
$this->assertInstanceOf(ResponseInterface::class, $response);
}
}

View File

@@ -2,6 +2,7 @@
namespace Tests\Unit\Support;
use App\Support\Helpers;
use PHPUnit\Framework\TestCase;
class HelpersTest extends TestCase
@@ -10,14 +11,14 @@ class HelpersTest extends TestCase
{
putenv('TEST_STRING=Test string; please ignore');
$env = env('TEST_STRING');
$env = Helpers::env('TEST_STRING');
$this->assertEquals('Test string; please ignore', $env);
}
public function test_it_can_return_a_default_value()
{
$env = env('DEFAULT_TEST', 'Test default; please ignore');
$env = Helpers::env('DEFAULT_TEST', 'Test default; please ignore');
$this->assertEquals('Test default; please ignore', $env);
}
@@ -27,22 +28,22 @@ class HelpersTest extends TestCase
putenv('TRUE_TEST=true');
putenv('FALSE_TEST=false');
$this->assertTrue(env('TRUE_TEST'));
$this->assertFalse(env('FALSE_TEST'));
$this->assertTrue(Helpers::env('TRUE_TEST'));
$this->assertFalse(Helpers::env('FALSE_TEST'));
}
public function test_it_can_retrieve_a_null_value()
{
putenv('NULL_TEST=null');
$this->assertNull(env('NULL_TEST'));
$this->assertNull(Helpers::env('NULL_TEST'));
}
public function test_it_can_be_surrounded_bys_quotation_marks()
{
putenv('QUOTES_TEST="Test charlie; please ignore"');
$env = env('QUOTES_TEST');
$env = Helpers::env('QUOTES_TEST');
$this->assertEquals('Test charlie; please ignore', $env);
}