mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-09-02 02:12:37 +02:00
Updated tests due to the application structure refactor
This commit is contained in:
@@ -60,6 +60,8 @@ class DirectoryController
|
||||
Response $response,
|
||||
string $path = '.'
|
||||
) {
|
||||
$path = realpath($this->container->get('base_path') . '/' . $path);
|
||||
|
||||
try {
|
||||
$files = $files->in($path);
|
||||
} catch (DirectoryNotFoundException $exception) {
|
||||
@@ -100,7 +102,9 @@ class DirectoryController
|
||||
*/
|
||||
protected function breadcrumbs(string $path): array
|
||||
{
|
||||
$breadcrumbs = Collection::make(array_filter(explode('/', $path)));
|
||||
$breadcrumbs = Collection::make(explode('/', $path))->diff(
|
||||
explode('/', $this->container->get('base_path'))
|
||||
)->filter();
|
||||
|
||||
return $breadcrumbs->filter(function (string $crumb) {
|
||||
return $crumb !== '.';
|
||||
|
@@ -2,22 +2,28 @@
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use DI\Container;
|
||||
use PHLAK\Config\Config;
|
||||
use Slim\Psr7\Response;
|
||||
use SplFileInfo;
|
||||
|
||||
class FileInfoController
|
||||
{
|
||||
/** @var Container The application container */
|
||||
protected $container;
|
||||
|
||||
/** @var Config App configuration component */
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Create a new FileInfoController object.
|
||||
*
|
||||
* @param \DI\Container $container
|
||||
* @param \PHLAK\Config\Config $config
|
||||
*/
|
||||
public function __construct(Config $config)
|
||||
public function __construct(Container $container, Config $config)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
@@ -29,7 +35,9 @@ class FileInfoController
|
||||
*/
|
||||
public function __invoke(Response $response, string $path = '.')
|
||||
{
|
||||
$file = new SplFileInfo($path);
|
||||
$file = new SplFileInfo(
|
||||
realpath($this->container->get('base_path') . '/' . $path)
|
||||
);
|
||||
|
||||
if (! $file->isFile()) {
|
||||
return $response->withStatus(404, 'File not found');
|
||||
|
19
tests/Bootstrap/AppManangerTest.php
Normal file
19
tests/Bootstrap/AppManangerTest.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Bootstrap;
|
||||
|
||||
use App\Bootstrap\AppManager;
|
||||
use Invoker\CallableResolver;
|
||||
use Slim\App;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AppManangerTest extends TestCase
|
||||
{
|
||||
public function test_it_returns_an_app_instance()
|
||||
{
|
||||
$callableResolver = $this->container->get(CallableResolver::class);
|
||||
$app = (new AppManager($this->container, $this->config, $callableResolver))();
|
||||
|
||||
$this->assertInstanceOf(App::class, $app);
|
||||
}
|
||||
}
|
@@ -2,8 +2,9 @@
|
||||
|
||||
namespace Tests\Controllers;
|
||||
|
||||
use App\Bootstrap\ViewComposer;
|
||||
use App\Controllers\DirectoryController;
|
||||
use App\Providers\TwigProvider;
|
||||
use Parsedown;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
@@ -13,13 +14,35 @@ use Tests\TestCase;
|
||||
|
||||
class DirectoryControllerTest extends TestCase
|
||||
{
|
||||
public function test_it_returns_a_response(): void
|
||||
public function test_it_returns_a_successful_response(): void
|
||||
{
|
||||
$this->container->call(ViewComposer::class);
|
||||
$this->container->call(TwigProvider::class);
|
||||
|
||||
$controller = new DirectoryController(
|
||||
$this->container,
|
||||
$this->config,
|
||||
new Parsedown(),
|
||||
$this->container->get(Twig::class)
|
||||
);
|
||||
|
||||
$response = $controller(
|
||||
new Finder(),
|
||||
$this->createMock(Request::class),
|
||||
new Response()
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(ResponseInterface::class, $response);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function test_it_returns_a_successful_response_when_listing_a_subdirectory(): void
|
||||
{
|
||||
$this->container->call(TwigProvider::class);
|
||||
|
||||
$controller = new DirectoryController(
|
||||
$this->container,
|
||||
$this->config,
|
||||
new Parsedown(),
|
||||
$this->container->get(Twig::class)
|
||||
);
|
||||
|
||||
@@ -27,7 +50,7 @@ class DirectoryControllerTest extends TestCase
|
||||
new Finder(),
|
||||
$this->createMock(Request::class),
|
||||
new Response(),
|
||||
'tests/files'
|
||||
'subdir'
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(ResponseInterface::class, $response);
|
||||
@@ -36,11 +59,12 @@ class DirectoryControllerTest extends TestCase
|
||||
|
||||
public function test_it_returns_a_404_error_when_not_found()
|
||||
{
|
||||
$this->container->call(ViewComposer::class);
|
||||
$this->container->call(TwigProvider::class);
|
||||
|
||||
$controller = new DirectoryController(
|
||||
$this->container,
|
||||
$this->config,
|
||||
new Parsedown(),
|
||||
$this->container->get(Twig::class)
|
||||
);
|
||||
|
||||
@@ -57,11 +81,12 @@ class DirectoryControllerTest extends TestCase
|
||||
|
||||
public function test_it_returns_a_successful_response_for_a_search()
|
||||
{
|
||||
$this->container->call(ViewComposer::class);
|
||||
$this->container->call(TwigProvider::class);
|
||||
|
||||
$controller = new DirectoryController(
|
||||
$this->container,
|
||||
$this->config,
|
||||
new Parsedown(),
|
||||
$this->container->get(Twig::class)
|
||||
);
|
||||
|
||||
@@ -73,8 +98,7 @@ class DirectoryControllerTest extends TestCase
|
||||
$response = $controller(
|
||||
new Finder(),
|
||||
$request,
|
||||
new Response(),
|
||||
'tests/files'
|
||||
new Response()
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(ResponseInterface::class, $response);
|
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Controllers;
|
||||
namespace Tests\Controllers;
|
||||
|
||||
use App\Controllers\FileInfoController;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
@@ -11,9 +11,9 @@ class FileInfoControllerTest extends TestCase
|
||||
{
|
||||
public function test_it_can_return_a_successful_response(): void
|
||||
{
|
||||
$controller = new FileInfoController($this->config);
|
||||
$controller = new FileInfoController($this->container, $this->config);
|
||||
|
||||
$response = $controller(new Response(), __DIR__ . '/../../files/alpha.scss');
|
||||
$response = $controller(new Response(), 'alpha.scss');
|
||||
|
||||
$this->assertInstanceOf(ResponseInterface::class, $response);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
@@ -21,7 +21,7 @@ class FileInfoControllerTest extends TestCase
|
||||
|
||||
public function test_it_can_return_a_not_found_response(): void
|
||||
{
|
||||
$controller = new FileInfoController($this->config);
|
||||
$controller = new FileInfoController($this->container, $this->config);
|
||||
|
||||
$response = $controller(new Response(), 'not_a_file.test');
|
||||
|
||||
@@ -32,9 +32,9 @@ class FileInfoControllerTest extends TestCase
|
||||
public function test_it_returns_an_error_when_file_size_is_too_large(): void
|
||||
{
|
||||
$this->config->set('app.max_hash_size', 10);
|
||||
$controller = new FileInfoController($this->config);
|
||||
$controller = new FileInfoController($this->container, $this->config);
|
||||
|
||||
$response = $controller(new Response(), __DIR__ . '/../../files/alpha.scss');
|
||||
$response = $controller(new Response(), 'alpha.scss');
|
||||
|
||||
$this->assertInstanceOf(ResponseInterface::class, $response);
|
||||
$this->assertEquals(500, $response->getStatusCode());
|
@@ -1,16 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap;
|
||||
namespace Tests\Providers;
|
||||
|
||||
use App\Bootstrap\ConfigComposer;
|
||||
use App\Providers\ConfigProvider;
|
||||
use PHLAK\Config\Config;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ConfigComposerTest extends TestCase
|
||||
class ConfigProviderTest extends TestCase
|
||||
{
|
||||
public function test_it_can_compose_the_config_component(): void
|
||||
{
|
||||
(new ConfigComposer($this->container))();
|
||||
(new ConfigProvider($this->container))();
|
||||
|
||||
$config = $this->container->get(Config::class);
|
||||
|
@@ -1,26 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap;
|
||||
namespace Tests\Providers;
|
||||
|
||||
use App\Bootstrap\FinderProvider;
|
||||
use App\Providers\FinderProvider;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\Finder\SplFileInfo;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FinderComposerTest extends TestCase
|
||||
class FinderProviderTest extends TestCase
|
||||
{
|
||||
public function test_it_can_compose_the_finder_component(): void
|
||||
{
|
||||
(new FinderProvider($this->container, $this->config))();
|
||||
|
||||
$finder = $this->container->get(Finder::class);
|
||||
$finder->in($this->container->get('base_path'));
|
||||
$finder->in($this->container->get('base_path'))->depth(0);
|
||||
|
||||
$this->assertInstanceOf(Finder::class, $finder);
|
||||
foreach ($finder as $file) {
|
||||
$this->assertInstanceOf(SplFileInfo::class, $file);
|
||||
}
|
||||
$this->assertEquals([
|
||||
'subdir',
|
||||
'alpha.scss',
|
||||
'bravo.js',
|
||||
'charlie.bash',
|
||||
'delta.html',
|
||||
'echo.yaml',
|
||||
], $this->getFilesArray($finder));
|
||||
}
|
||||
|
||||
public function test_it_can_sort_by_a_user_provided_closure(): void
|
||||
@@ -32,7 +37,7 @@ class FinderComposerTest extends TestCase
|
||||
(new FinderProvider($this->container, $this->config))();
|
||||
|
||||
$finder = $this->container->get(Finder::class);
|
||||
$finder->in($this->container->get('base_path'));
|
||||
$finder->in($this->container->get('base_path'))->depth(0);
|
||||
|
||||
$this->assertEquals([
|
||||
'alpha.scss',
|
||||
@@ -40,6 +45,7 @@ class FinderComposerTest extends TestCase
|
||||
'echo.yaml',
|
||||
'charlie.bash',
|
||||
'delta.html',
|
||||
'subdir',
|
||||
], $this->getFilesArray($finder));
|
||||
}
|
||||
|
||||
@@ -50,7 +56,7 @@ class FinderComposerTest extends TestCase
|
||||
(new FinderProvider($this->container, $this->config))();
|
||||
|
||||
$finder = $this->container->get(Finder::class);
|
||||
$finder->in($this->container->get('base_path'));
|
||||
$finder->in($this->container->get('base_path'))->depth(0);
|
||||
|
||||
$this->assertEquals([
|
||||
'echo.yaml',
|
||||
@@ -58,6 +64,7 @@ class FinderComposerTest extends TestCase
|
||||
'charlie.bash',
|
||||
'bravo.js',
|
||||
'alpha.scss',
|
||||
'subdir',
|
||||
], $this->getFilesArray($finder));
|
||||
}
|
||||
|
@@ -1,18 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap;
|
||||
namespace Tests\Providers;
|
||||
|
||||
use App\Bootstrap\ViewComposer;
|
||||
use App\Bootstrap\ViewFunctions;
|
||||
use App\Providers\TwigProvider;
|
||||
use App\ViewFunctions;
|
||||
use PHLAK\Config\Config;
|
||||
use Slim\Views\Twig;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ViewComposerTest extends TestCase
|
||||
class TwigProviderTest extends TestCase
|
||||
{
|
||||
public function test_it_can_compose_the_view_component(): void
|
||||
{
|
||||
(new ViewComposer($this->container, new Config))();
|
||||
(new TwigProvider($this->container, new Config))();
|
||||
|
||||
$twig = $this->container->get(Twig::class);
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap\SortMethods;
|
||||
namespace Tests\SortMethods;
|
||||
|
||||
use App\Bootstrap\SortMethods\Accessed;
|
||||
use App\SortMethods\Accessed;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap\SortMethods;
|
||||
namespace Tests\SortMethods;
|
||||
|
||||
use App\Bootstrap\SortMethods\Changed;
|
||||
use App\SortMethods\Changed;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap\SortMethods;
|
||||
namespace Tests\SortMethods;
|
||||
|
||||
use App\Bootstrap\SortMethods\Modified;
|
||||
use App\SortMethods\Modified;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap\SortMethods;
|
||||
namespace Tests\SortMethods;
|
||||
|
||||
use App\Bootstrap\SortMethods\Name;
|
||||
use App\SortMethods\Name;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap\SortMethods;
|
||||
namespace Tests\SortMethods;
|
||||
|
||||
use App\Bootstrap\SortMethods\Natural;
|
||||
use App\SortMethods\Natural;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap\SortMethods;
|
||||
namespace Tests\SortMethods;
|
||||
|
||||
use App\Bootstrap\SortMethods\Type;
|
||||
use App\SortMethods\Type;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Support;
|
||||
namespace Tests\Support;
|
||||
|
||||
use App\Support\Helpers;
|
||||
use PHPUnit\Framework\TestCase;
|
@@ -15,7 +15,7 @@ class TestCase extends PHPUnitTestCase
|
||||
protected $config;
|
||||
|
||||
/** @var string Path to test files directory */
|
||||
protected $testFilesPath = __DIR__ . '/files';
|
||||
protected $testFilesPath = __DIR__ . '/_files';
|
||||
|
||||
/**
|
||||
* This method is called before each test.
|
||||
@@ -41,4 +41,16 @@ class TestCase extends PHPUnitTestCase
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file path to a test file.
|
||||
*
|
||||
* @param string $filePath
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function filePath(string $filePath): string
|
||||
{
|
||||
return realpath($this->testFilesPath . '/' . $filePath);
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap\ViewFunctions;
|
||||
namespace Tests\ViewFunctions;
|
||||
|
||||
use App\Bootstrap\ViewFunctions\Asset;
|
||||
use App\ViewFunctions\Asset;
|
||||
use PHLAK\Config\Config;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap\ViewFunctions;
|
||||
namespace Tests\ViewFunctions;
|
||||
|
||||
use App\Bootstrap\ViewFunctions\Config;
|
||||
use App\ViewFunctions\Config;
|
||||
use PHLAK\Config\Config as AppConfig;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap\ViewFunctions;
|
||||
namespace Tests\ViewFunctions;
|
||||
|
||||
use App\Bootstrap\ViewFunctions\Icon;
|
||||
use App\ViewFunctions\Icon;
|
||||
use PHLAK\Config\Config;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Finder\SplFileInfo;
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Bootstrap\ViewFunctions;
|
||||
namespace Tests\ViewFunctions;
|
||||
|
||||
use App\Bootstrap\ViewFunctions\SizeForHumans;
|
||||
use App\ViewFunctions\SizeForHumans;
|
||||
use PHLAK\Config\Config;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Finder\SplFileInfo;
|
3
tests/_files/subdir/foxtrot.json
Normal file
3
tests/_files/subdir/foxtrot.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"contents": "Test file; please ignore"
|
||||
}
|
1
tests/_files/subdir/golf.txt
Normal file
1
tests/_files/subdir/golf.txt
Normal file
@@ -0,0 +1 @@
|
||||
Test file; please ignore
|
2
tests/_files/subdir/hotel.md
Normal file
2
tests/_files/subdir/hotel.md
Normal file
@@ -0,0 +1,2 @@
|
||||
Test file; please ignore
|
||||
------------------------
|
Reference in New Issue
Block a user