From ab255bf7e573ea9eed7a8be7e4894ed28f039b76 Mon Sep 17 00:00:00 2001 From: Daniel Wilkowski Date: Tue, 31 May 2022 10:08:32 +0200 Subject: [PATCH 1/5] Rename Path to BasePath --- .../Api/Setup/InstallController.php | 4 +- .../Http/Controllers/View/SetupController.php | 6 +- .../Http/Validation/Rules/LanguageRule.php | 6 +- includes/Install/EnvCreator.php | 6 +- includes/Install/MigrationFiles.php | 6 +- includes/Install/OldShop.php | 6 +- includes/Install/RequirementStore.php | 6 +- includes/Install/SetupManager.php | 6 +- includes/Install/UpdateInfo.php | 6 +- includes/Loggers/FileLogger.php | 6 +- .../Payment/General/PurchaseDataService.php | 6 +- includes/Providers/AppServiceProvider.php | 6 +- includes/Support/{Path.php => BasePath.php} | 6 +- includes/Support/Meta.php | 4 +- includes/System/Application.php | 8 +-- includes/System/CronExecutor.php | 6 +- includes/System/Settings.php | 6 +- includes/Theme/ThemeRepository.php | 6 +- includes/Translation/Translator.php | 6 +- includes/View/Pages/Admin/PageAdmin.php | 6 +- .../View/Pages/Admin/PageAdminSettings.php | 6 +- includes/View/Pages/Page.php | 6 +- includes/View/Pages/Shop/PagePurchase.php | 6 +- .../Http/View/Shop/SetupControllerTest.php | 6 +- tests/Psr4/Concerns/FixtureConcern.php | 6 +- tests/Psr4/Concerns/SystemConcern.php | 26 ++++++++ tests/Unit/Install/EnvCreatorTest.php | 6 +- tests/Unit/Install/RequirementStoreTest.php | 4 +- tests/Unit/Support/BasePathTest.php | 62 +++++++++++++++++++ tests/Unit/Support/MetaParserTest.php | 4 +- tests/Unit/Support/PathTest.php | 43 ------------- 31 files changed, 169 insertions(+), 124 deletions(-) rename includes/Support/{Path.php => BasePath.php} (77%) create mode 100644 tests/Psr4/Concerns/SystemConcern.php create mode 100644 tests/Unit/Support/BasePathTest.php delete mode 100644 tests/Unit/Support/PathTest.php diff --git a/includes/Http/Controllers/Api/Setup/InstallController.php b/includes/Http/Controllers/Api/Setup/InstallController.php index 3917482d9..79bc00006 100644 --- a/includes/Http/Controllers/Api/Setup/InstallController.php +++ b/includes/Http/Controllers/Api/Setup/InstallController.php @@ -11,7 +11,7 @@ use App\Install\SetupManager; use App\Support\Database; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; use App\System\Application; use Exception; use PDOException; @@ -25,7 +25,7 @@ public function post( RequirementStore $requirementStore, EnvCreator $envCreator, SetupManager $setupManager, - Path $path, + BasePath $path, FileSystemContract $fileSystem, Application $app ) { diff --git a/includes/Http/Controllers/View/SetupController.php b/includes/Http/Controllers/View/SetupController.php index dd7444d18..69d0714c5 100644 --- a/includes/Http/Controllers/View/SetupController.php +++ b/includes/Http/Controllers/View/SetupController.php @@ -7,7 +7,7 @@ use App\Install\ShopState; use App\Install\UpdateInfo; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; use App\Theme\Template; use Symfony\Component\HttpFoundation\Response; @@ -26,7 +26,7 @@ public function get( UpdateInfo $updateInfo, RequirementStore $requirementStore, FileSystemContract $fileSystem, - Path $path + BasePath $path ) { if ($oldShop->hasConfigFile()) { return new HtmlResponse($this->template->render("setup/missing_env")); @@ -45,7 +45,7 @@ public function get( private function install( RequirementStore $requirementStore, - Path $path, + BasePath $path, FileSystemContract $fileSystem ) { $modules = $requirementStore->getModules(); diff --git a/includes/Http/Validation/Rules/LanguageRule.php b/includes/Http/Validation/Rules/LanguageRule.php index 71c4c0358..aafc7d54c 100644 --- a/includes/Http/Validation/Rules/LanguageRule.php +++ b/includes/Http/Validation/Rules/LanguageRule.php @@ -4,18 +4,18 @@ use App\Exceptions\ValidationException; use App\Http\Validation\BaseRule; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; class LanguageRule extends BaseRule { private FileSystemContract $fileSystem; - private Path $path; + private BasePath $path; public function __construct() { parent::__construct(); $this->fileSystem = app()->make(FileSystemContract::class); - $this->path = app()->make(Path::class); + $this->path = app()->make(BasePath::class); } public function validate($attribute, $value, array $data): void diff --git a/includes/Install/EnvCreator.php b/includes/Install/EnvCreator.php index ee6c533b0..35191135a 100644 --- a/includes/Install/EnvCreator.php +++ b/includes/Install/EnvCreator.php @@ -2,14 +2,14 @@ namespace App\Install; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; class EnvCreator { - private Path $path; + private BasePath $path; private FileSystemContract $fileSystem; - public function __construct(Path $path, FileSystemContract $fileSystem) + public function __construct(BasePath $path, FileSystemContract $fileSystem) { $this->path = $path; $this->fileSystem = $fileSystem; diff --git a/includes/Install/MigrationFiles.php b/includes/Install/MigrationFiles.php index 5ffd0cf87..a63340967 100644 --- a/includes/Install/MigrationFiles.php +++ b/includes/Install/MigrationFiles.php @@ -1,14 +1,14 @@ path = $path; } diff --git a/includes/Install/OldShop.php b/includes/Install/OldShop.php index 640b7bad2..2cc2961dd 100644 --- a/includes/Install/OldShop.php +++ b/includes/Install/OldShop.php @@ -2,14 +2,14 @@ namespace App\Install; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; class OldShop { - private Path $path; + private BasePath $path; private FileSystemContract $fileSystem; - public function __construct(Path $path, FileSystemContract $fileSystem) + public function __construct(BasePath $path, FileSystemContract $fileSystem) { $this->path = $path; $this->fileSystem = $fileSystem; diff --git a/includes/Install/RequirementStore.php b/includes/Install/RequirementStore.php index 4b9a2f26a..a65505186 100644 --- a/includes/Install/RequirementStore.php +++ b/includes/Install/RequirementStore.php @@ -3,15 +3,15 @@ use App\Support\FileSystemContract; use App\Support\Meta; -use App\Support\Path; +use App\Support\BasePath; class RequirementStore { - private Path $path; + private BasePath $path; private FileSystemContract $fileSystem; private Meta $meta; - public function __construct(Path $path, Meta $meta, FileSystemContract $fileSystem) + public function __construct(BasePath $path, Meta $meta, FileSystemContract $fileSystem) { $this->path = $path; $this->fileSystem = $fileSystem; diff --git a/includes/Install/SetupManager.php b/includes/Install/SetupManager.php index 7518c6373..664208b2d 100644 --- a/includes/Install/SetupManager.php +++ b/includes/Install/SetupManager.php @@ -2,14 +2,14 @@ namespace App\Install; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; class SetupManager { - private Path $path; + private BasePath $path; private FileSystemContract $fileSystem; - public function __construct(Path $path, FileSystemContract $fileSystem) + public function __construct(BasePath $path, FileSystemContract $fileSystem) { $this->path = $path; $this->fileSystem = $fileSystem; diff --git a/includes/Install/UpdateInfo.php b/includes/Install/UpdateInfo.php index ec46810ab..979be281d 100644 --- a/includes/Install/UpdateInfo.php +++ b/includes/Install/UpdateInfo.php @@ -2,16 +2,16 @@ namespace App\Install; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; use App\Theme\Template; class UpdateInfo { - private Path $path; + private BasePath $path; private Template $template; private FileSystemContract $fileSystem; - public function __construct(Path $path, Template $template, FileSystemContract $fileSystem) + public function __construct(BasePath $path, Template $template, FileSystemContract $fileSystem) { $this->path = $path; $this->template = $template; diff --git a/includes/Loggers/FileLogger.php b/includes/Loggers/FileLogger.php index 10b87d30f..a6d78db05 100644 --- a/includes/Loggers/FileLogger.php +++ b/includes/Loggers/FileLogger.php @@ -2,7 +2,7 @@ namespace App\Loggers; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; use App\System\Settings; use Psr\Log\LoggerInterface; @@ -10,9 +10,9 @@ class FileLogger implements LoggerInterface { private Settings $settings; private FileSystemContract $fileSystem; - private Path $path; + private BasePath $path; - public function __construct(Settings $settings, FileSystemContract $fileSystem, Path $path) + public function __construct(Settings $settings, FileSystemContract $fileSystem, BasePath $path) { $this->settings = $settings; $this->fileSystem = $fileSystem; diff --git a/includes/Payment/General/PurchaseDataService.php b/includes/Payment/General/PurchaseDataService.php index 3e9e22d9f..d59ce12a2 100644 --- a/includes/Payment/General/PurchaseDataService.php +++ b/includes/Payment/General/PurchaseDataService.php @@ -3,17 +3,17 @@ use App\Models\Purchase; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; class PurchaseDataService { private PurchaseSerializer $purchaseSerializer; - private Path $path; + private BasePath $path; private FileSystemContract $fileSystem; public function __construct( PurchaseSerializer $purchaseSerializer, - Path $path, + BasePath $path, FileSystemContract $fileSystem ) { $this->purchaseSerializer = $purchaseSerializer; diff --git a/includes/Providers/AppServiceProvider.php b/includes/Providers/AppServiceProvider.php index c0040e8a9..949f84ddd 100644 --- a/includes/Providers/AppServiceProvider.php +++ b/includes/Providers/AppServiceProvider.php @@ -21,7 +21,7 @@ use App\Support\FileSystemContract; use App\Support\Mailer; use App\Support\Meta; -use App\Support\Path; +use App\Support\BasePath; use App\Theme\ContentEvaluator; use App\Theme\EditableTemplateRepository; use App\Theme\Template; @@ -91,8 +91,8 @@ private function registerDatabase(Application $app) private function registerCache(Application $app) { $app->bind(FileCache::class, function (Application $app) { - /** @var Path $path */ - $path = $app->make(Path::class); + /** @var BasePath $path */ + $path = $app->make(BasePath::class); return new FileCache($app->make(FileSystemContract::class), $path->to("data/cache")); }); $app->bind(CacheInterface::class, FileCache::class); diff --git a/includes/Support/Path.php b/includes/Support/BasePath.php similarity index 77% rename from includes/Support/Path.php rename to includes/Support/BasePath.php index 2e9d867ec..f73943dac 100644 --- a/includes/Support/Path.php +++ b/includes/Support/BasePath.php @@ -1,7 +1,7 @@ basePath = $basePath; } - public static function temporary(): Path + public static function temporary(): BasePath { - return new Path(sys_get_temp_dir()); + return new BasePath(sys_get_temp_dir()); } public function to(string $subpath): string diff --git a/includes/Support/Meta.php b/includes/Support/Meta.php index ec2071949..88f02e8e5 100644 --- a/includes/Support/Meta.php +++ b/includes/Support/Meta.php @@ -4,10 +4,10 @@ class Meta { private MetaParser $metaParser; - private Path $path; + private BasePath $path; private array $meta; - public function __construct(MetaParser $metaParser, Path $path) + public function __construct(MetaParser $metaParser, BasePath $path) { $this->metaParser = $metaParser; $this->path = $path; diff --git a/includes/System/Application.php b/includes/System/Application.php index 18c9930a1..0fee5933b 100644 --- a/includes/System/Application.php +++ b/includes/System/Application.php @@ -4,7 +4,7 @@ use App\Providers\AppServiceProvider; use App\Providers\HeartServiceProvider; use App\Providers\SentryServiceProvider; -use App\Support\Path; +use App\Support\BasePath; use DirectoryIterator; use Illuminate\Container\Container; use Sentry\SentrySdk; @@ -34,7 +34,7 @@ private function registerBindings(): void { $this->instance(Container::class, $this); $this->instance(Application::class, $this); - $this->bind(Path::class, fn() => new Path(realpath($this->basePath))); + $this->bind(BasePath::class, fn() => new BasePath(realpath($this->basePath))); } private function bootstrap(): void @@ -76,8 +76,8 @@ public function terminate(Request $request = null, Response $response = null): v private function getProviders(): array { if (!$this->providers) { - /** @var Path $path */ - $path = $this->make(Path::class); + /** @var BasePath $path */ + $path = $this->make(BasePath::class); $dir = new DirectoryIterator($path->to("/includes/Providers")); foreach ($dir as $fileInfo) { diff --git a/includes/System/CronExecutor.php b/includes/System/CronExecutor.php index b7df9fc66..6a62ba6e6 100644 --- a/includes/System/CronExecutor.php +++ b/includes/System/CronExecutor.php @@ -5,13 +5,13 @@ use App\Service\ExpiredUserServiceService; use App\Support\Database; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; class CronExecutor { private Database $db; private Settings $settings; - private Path $path; + private BasePath $path; private FileSystemContract $fileSystem; private ExpiredUserServiceService $expiredUserServiceService; private ExpiredSmsCodeService $expiredSmsCodeService; @@ -19,7 +19,7 @@ class CronExecutor public function __construct( Database $db, Settings $settings, - Path $path, + BasePath $path, FileSystemContract $fileSystem, ExpiredUserServiceService $expiredServiceService, ExpiredSmsCodeService $expiredSmsCodeService diff --git a/includes/System/Settings.php b/includes/System/Settings.php index 1fa0e633f..4ff69738a 100644 --- a/includes/System/Settings.php +++ b/includes/System/Settings.php @@ -3,7 +3,7 @@ use App\Support\Database; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; use App\Theme\TemplateRepository; use App\Theme\ThemeRepository; use ArrayAccess; @@ -12,12 +12,12 @@ class Settings implements ArrayAccess { private array $data; private Database $db; - private Path $path; + private BasePath $path; private FileSystemContract $fileSystem; private ThemeRepository $themeRepository; public function __construct( - Path $path, + BasePath $path, Database $database, FileSystemContract $fileSystem, ThemeRepository $themeRepository diff --git a/includes/Theme/ThemeRepository.php b/includes/Theme/ThemeRepository.php index 2cd0efc4c..bcc45dda1 100644 --- a/includes/Theme/ThemeRepository.php +++ b/includes/Theme/ThemeRepository.php @@ -3,19 +3,19 @@ use App\Support\Collection; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; use Generator; class ThemeRepository { private TemplateRepository $templateRepository; private FileSystemContract $fileSystem; - private Path $path; + private BasePath $path; public function __construct( TemplateRepository $templateRepository, FileSystemContract $fileSystem, - Path $path + BasePath $path ) { $this->templateRepository = $templateRepository; $this->fileSystem = $fileSystem; diff --git a/includes/Translation/Translator.php b/includes/Translation/Translator.php index 257d6f4cc..0e96cab47 100644 --- a/includes/Translation/Translator.php +++ b/includes/Translation/Translator.php @@ -2,11 +2,11 @@ namespace App\Translation; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; class Translator { - private Path $path; + private BasePath $path; private FileSystemContract $fileSystem; /** @@ -57,7 +57,7 @@ public static function getLanguageByShort(string $short): ?string public function __construct($lang = "polish") { - $this->path = app()->make(Path::class); + $this->path = app()->make(BasePath::class); $this->fileSystem = app()->make(FileSystemContract::class); $this->setLanguage($lang); } diff --git a/includes/View/Pages/Admin/PageAdmin.php b/includes/View/Pages/Admin/PageAdmin.php index 1e112766d..ac09c7998 100644 --- a/includes/View/Pages/Admin/PageAdmin.php +++ b/includes/View/Pages/Admin/PageAdmin.php @@ -4,7 +4,7 @@ use App\Managers\WebsiteHeader; use App\Routing\UrlGenerator; use App\Support\FileSystem; -use App\Support\Path; +use App\Support\BasePath; use App\User\Permission; use App\View\Interfaces\IBeLoggedMust; use App\View\Pages\Page; @@ -27,8 +27,8 @@ public function addScripts(Request $request): void /** @var FileSystem $fileSystem */ $fileSystem = app()->make(FileSystem::class); - /** @var Path $path */ - $path = app()->make(Path::class); + /** @var BasePath $path */ + $path = app()->make(BasePath::class); /** @var UrlGenerator $url */ $url = app()->make(UrlGenerator::class); diff --git a/includes/View/Pages/Admin/PageAdminSettings.php b/includes/View/Pages/Admin/PageAdminSettings.php index 363c97b98..dce230bf1 100644 --- a/includes/View/Pages/Admin/PageAdminSettings.php +++ b/includes/View/Pages/Admin/PageAdminSettings.php @@ -5,7 +5,7 @@ use App\Models\PaymentPlatform; use App\Repositories\PaymentPlatformRepository; use App\Support\FileSystem; -use App\Support\Path; +use App\Support\BasePath; use App\Theme\Template; use App\System\Settings; use App\Translation\TranslationManager; @@ -29,7 +29,7 @@ class PageAdminSettings extends PageAdmin private PaymentPlatformRepository $paymentPlatformRepository; private Translator $langShop; private FileSystem $fileSystem; - private Path $path; + private BasePath $path; private PaymentModuleManager $paymentModuleManager; private ThemeRepository $themeService; @@ -40,7 +40,7 @@ public function __construct( PaymentPlatformRepository $paymentPlatformRepository, PaymentModuleManager $paymentModuleManager, FileSystem $fileSystem, - Path $path, + BasePath $path, ThemeRepository $themeService ) { parent::__construct($template, $translationManager); diff --git a/includes/View/Pages/Page.php b/includes/View/Pages/Page.php index eaab9809d..dc4063aad 100644 --- a/includes/View/Pages/Page.php +++ b/includes/View/Pages/Page.php @@ -4,7 +4,7 @@ use App\Managers\WebsiteHeader; use App\Routing\UrlGenerator; use App\Support\FileSystem; -use App\Support\Path; +use App\Support\BasePath; use App\Theme\Template; use App\Translation\TranslationManager; use App\Translation\Translator; @@ -53,8 +53,8 @@ public function addScripts(Request $request): void /** @var FileSystem $fileSystem */ $fileSystem = app()->make(FileSystem::class); - /** @var Path $path */ - $path = app()->make(Path::class); + /** @var BasePath $path */ + $path = app()->make(BasePath::class); /** @var UrlGenerator $url */ $url = app()->make(UrlGenerator::class); diff --git a/includes/View/Pages/Shop/PagePurchase.php b/includes/View/Pages/Shop/PagePurchase.php index 2470e5afd..aa000be56 100644 --- a/includes/View/Pages/Shop/PagePurchase.php +++ b/includes/View/Pages/Shop/PagePurchase.php @@ -10,7 +10,7 @@ use App\ServiceModules\Interfaces\IServicePurchaseWeb; use App\ServiceModules\ServiceModule; use App\Support\FileSystem; -use App\Support\Path; +use App\Support\BasePath; use App\Theme\Template; use App\System\Auth; use App\Translation\TranslationManager; @@ -25,7 +25,7 @@ class PagePurchase extends Page private Auth $auth; private UserServiceAccessService $userServiceAccessService; private WebsiteHeader $websiteHeader; - private Path $path; + private BasePath $path; private FileSystem $fileSystem; private UrlGenerator $url; private ServiceModuleManager $serviceModuleManager; @@ -37,7 +37,7 @@ public function __construct( UserServiceAccessService $userServiceAccessService, WebsiteHeader $websiteHeader, ServiceModuleManager $serviceModuleManager, - Path $path, + BasePath $path, FileSystem $fileSystem, UrlGenerator $url ) { diff --git a/tests/Feature/Http/View/Shop/SetupControllerTest.php b/tests/Feature/Http/View/Shop/SetupControllerTest.php index f1d6736df..9a5cbd9ea 100644 --- a/tests/Feature/Http/View/Shop/SetupControllerTest.php +++ b/tests/Feature/Http/View/Shop/SetupControllerTest.php @@ -3,7 +3,7 @@ use App\Install\ShopState; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; use Mockery; use Mockery\MockInterface; use Symfony\Component\HttpFoundation\Response; @@ -42,8 +42,8 @@ public function shows_text_if_shop_is_up_to_date() public function missing_env_if_upgrading_from_old_shop() { // given - /** @var Path $path */ - $path = $this->app->make(Path::class); + /** @var BasePath $path */ + $path = $this->app->make(BasePath::class); $this->markAsNotInstalled(); $this->fileSystem->put($path->to("/includes/config.php"), ""); diff --git a/tests/Psr4/Concerns/FixtureConcern.php b/tests/Psr4/Concerns/FixtureConcern.php index 62b4e54ef..e3c6dceac 100644 --- a/tests/Psr4/Concerns/FixtureConcern.php +++ b/tests/Psr4/Concerns/FixtureConcern.php @@ -1,7 +1,7 @@ app->make(Path::class); + /** @var BasePath $pathBuilder */ + $pathBuilder = $this->app->make(BasePath::class); $path = $pathBuilder->to("tests/Fixtures/$name.json"); return file_get_contents($path); diff --git a/tests/Psr4/Concerns/SystemConcern.php b/tests/Psr4/Concerns/SystemConcern.php new file mode 100644 index 000000000..3fee50d54 --- /dev/null +++ b/tests/Psr4/Concerns/SystemConcern.php @@ -0,0 +1,26 @@ +isUnix()) { + Assert::assertSame($expected, $actual); + } + } + + public function assertSameUnix($expected, $actual): void + { + if ($this->isUnix()) { + Assert::assertSame($expected, $actual); + } + } + + public function isUnix(): bool + { + return \DIRECTORY_SEPARATOR === "/"; + } +} diff --git a/tests/Unit/Install/EnvCreatorTest.php b/tests/Unit/Install/EnvCreatorTest.php index ffa2ee63c..e03a270d6 100644 --- a/tests/Unit/Install/EnvCreatorTest.php +++ b/tests/Unit/Install/EnvCreatorTest.php @@ -3,21 +3,21 @@ use App\Install\EnvCreator; use App\Support\FileSystemContract; -use App\Support\Path; +use App\Support\BasePath; use Tests\Psr4\MemoryFileSystem; use Tests\Psr4\TestCases\UnitTestCase; class EnvCreatorTest extends UnitTestCase { private FileSystemContract $fileSystem; - private Path $basePath; + private BasePath $basePath; private EnvCreator $envCreator; protected function setUp(): void { parent::setUp(); $this->fileSystem = new MemoryFileSystem(); - $this->basePath = Path::temporary(); + $this->basePath = BasePath::temporary(); $this->envCreator = new EnvCreator($this->basePath, $this->fileSystem); } diff --git a/tests/Unit/Install/RequirementStoreTest.php b/tests/Unit/Install/RequirementStoreTest.php index a16b36043..aa631ba22 100644 --- a/tests/Unit/Install/RequirementStoreTest.php +++ b/tests/Unit/Install/RequirementStoreTest.php @@ -4,7 +4,7 @@ use App\Install\RequirementStore; use App\Support\Meta; use App\Support\MetaParser; -use App\Support\Path; +use App\Support\BasePath; use Tests\Psr4\MemoryFileSystem; use Tests\Psr4\TestCases\UnitTestCase; @@ -47,7 +47,7 @@ public function get_list_of_requirements() public function invalid_php_version() { // given - $path = Path::temporary(); + $path = BasePath::temporary(); $fileSystem = new MemoryFileSystem(); $meta = new Meta(new MetaParser($fileSystem), $path); $requirementStore = new RequirementStore($path, $meta, $fileSystem); diff --git a/tests/Unit/Support/BasePathTest.php b/tests/Unit/Support/BasePathTest.php new file mode 100644 index 000000000..f7d7d6d16 --- /dev/null +++ b/tests/Unit/Support/BasePathTest.php @@ -0,0 +1,62 @@ +path = new BasePath("/test"); + } + + /** @test */ + public function it_returns_absolute_path_for_absolute_path() + { + // when + $absolutePath = $this->path->to("/foo/bar.sql"); + + // then + $this->assertSame($this->path->to("/foo/bar.sql"), $absolutePath); + } + + /** @test */ + public function it_returns_absolute_path_for_relative_path() + { + // when + $absolutePath = $this->path->to("foo/bar.sql"); + + // then + $this->assertSame($this->path->to("/foo/bar.sql"), $absolutePath); + } + + public function testTemporary() + { + // given + $base = BasePath::temporary(); + // when + $path = $base->to(""); + // then + if ($this->isUnix()) { + $this->assertStringContainsString("tmp", $path); + } else { + $this->assertStringContainsString("Temp", $path); + } + } + + public function testSubpath() + { + // given + $path = new BasePath("/a/b/c"); + // when + $this->assertSameWindows("/a/b/c\d/e", $path->to("d/e")); + $this->assertSameUnix("/a/b/c/d/e", $path->to("d/e")); + } +} diff --git a/tests/Unit/Support/MetaParserTest.php b/tests/Unit/Support/MetaParserTest.php index 735af8578..bafcce283 100644 --- a/tests/Unit/Support/MetaParserTest.php +++ b/tests/Unit/Support/MetaParserTest.php @@ -4,7 +4,7 @@ use App\Support\FileSystemContract; use App\Support\MetaParser; -use App\Support\Path; +use App\Support\BasePath; use Tests\Psr4\MemoryFileSystem; use Tests\Psr4\TestCases\UnitTestCase; @@ -19,7 +19,7 @@ protected function setUp(): void parent::setUp(); $this->fileSystem = new MemoryFileSystem(); $this->parser = new MetaParser($this->fileSystem); - $this->path = Path::temporary()->to("meta.txt"); + $this->path = BasePath::temporary()->to("meta.txt"); } /** @test */ diff --git a/tests/Unit/Support/PathTest.php b/tests/Unit/Support/PathTest.php deleted file mode 100644 index 3b92b8926..000000000 --- a/tests/Unit/Support/PathTest.php +++ /dev/null @@ -1,43 +0,0 @@ -path = new Path("/test"); - } - - /** @test */ - public function it_returns_absolute_path_for_relative_path_prefixed_with_slash() - { - // given - $relativePath = "/foo/bar.sql"; - - // when - $absolutePath = $this->path->to($relativePath); - - // then - $this->assertEquals($this->path->to("/foo/bar.sql"), $absolutePath); - } - - /** @test */ - public function it_returns_absolute_path_for_relative_path_not_prefixed_with_slash() - { - // given - $relativePath = "foo/bar.sql"; - - // when - $absolutePath = $this->path->to($relativePath); - - // then - $this->assertEquals($this->path->to("/foo/bar.sql"), $absolutePath); - } -} From 6cc3dc0ecc5fe63707409e0c616ac7a6d8e5a01a Mon Sep 17 00:00:00 2001 From: Daniel Wilkowski Date: Tue, 31 May 2022 10:24:18 +0200 Subject: [PATCH 2/5] Remove FileSystemContract.isFile(), which was never used as an interface method --- includes/Support/FileSystem.php | 8 ++------ includes/Support/FileSystemContract.php | 8 -------- tests/Psr4/MemoryFileSystem.php | 2 +- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/includes/Support/FileSystem.php b/includes/Support/FileSystem.php index 32cc1572f..5e97db921 100644 --- a/includes/Support/FileSystem.php +++ b/includes/Support/FileSystem.php @@ -3,6 +3,7 @@ use ErrorException; use Exception; +use function is_file; class FileSystem implements FileSystemContract { @@ -39,7 +40,7 @@ public function put($path, $contents, $lock = false) public function get($path, $lock = false) { - if ($this->isFile($path)) { + if (is_file($path)) { return $lock ? $this->sharedGet($path) : file_get_contents($path); } throw new Exception("File does not exist at path {$path}"); @@ -63,11 +64,6 @@ public function sharedGet($path) return $contents; } - public function isFile($path) - { - return is_file($path); - } - public function isDirectory($path) { return is_dir($path); diff --git a/includes/Support/FileSystemContract.php b/includes/Support/FileSystemContract.php index d0d457b7b..e8a239419 100644 --- a/includes/Support/FileSystemContract.php +++ b/includes/Support/FileSystemContract.php @@ -57,14 +57,6 @@ public function get($path, $lock = false); */ public function sharedGet($path); - /** - * Determine if the given path is a file. - * - * @param string $path - * @return bool - */ - public function isFile($path); - /** * Determine if the given path is a directory. * diff --git a/tests/Psr4/MemoryFileSystem.php b/tests/Psr4/MemoryFileSystem.php index 56a4d1dc2..8f265bdb3 100644 --- a/tests/Psr4/MemoryFileSystem.php +++ b/tests/Psr4/MemoryFileSystem.php @@ -63,7 +63,7 @@ public function sharedGet($path) return $this->get($path); } - public function isFile($path) + private function isFile($path) { $formattedPath = $this->formatPath($path); return $this->exists($formattedPath) && $this->fileSystem[$formattedPath]["type"] === "f"; From dc3cb4f8cc67745db1be148e894ab88d32d870ec Mon Sep 17 00:00:00 2001 From: Daniel Wilkowski Date: Tue, 31 May 2022 11:51:12 +0200 Subject: [PATCH 3/5] Remove FileSystemContract.size(), which was never used as an interface method --- includes/Support/FileSystem.php | 8 ++------ includes/Support/FileSystemContract.php | 8 -------- tests/Psr4/MemoryFileSystem.php | 5 ----- 3 files changed, 2 insertions(+), 19 deletions(-) diff --git a/includes/Support/FileSystem.php b/includes/Support/FileSystem.php index 5e97db921..74d178299 100644 --- a/includes/Support/FileSystem.php +++ b/includes/Support/FileSystem.php @@ -3,6 +3,7 @@ use ErrorException; use Exception; +use function filesize; use function is_file; class FileSystem implements FileSystemContract @@ -54,7 +55,7 @@ public function sharedGet($path) try { if (flock($handle, LOCK_SH)) { clearstatcache(true, $path); - $contents = fread($handle, $this->size($path) ?: 1); + $contents = fread($handle, filesize($path) ?: 1); flock($handle, LOCK_UN); } } finally { @@ -69,11 +70,6 @@ public function isDirectory($path) return is_dir($path); } - public function size($path) - { - return filesize($path); - } - public function append($file, $text) { if ($this->exists($file) && strlen($this->get($file))) { diff --git a/includes/Support/FileSystemContract.php b/includes/Support/FileSystemContract.php index e8a239419..0bd0f5f45 100644 --- a/includes/Support/FileSystemContract.php +++ b/includes/Support/FileSystemContract.php @@ -65,14 +65,6 @@ public function sharedGet($path); */ public function isDirectory($path); - /** - * Get the file size of a given file. - * - * @param string $path - * @return int - */ - public function size($path); - /** * Add text to the end of the file * diff --git a/tests/Psr4/MemoryFileSystem.php b/tests/Psr4/MemoryFileSystem.php index 8f265bdb3..d8abb3011 100644 --- a/tests/Psr4/MemoryFileSystem.php +++ b/tests/Psr4/MemoryFileSystem.php @@ -75,11 +75,6 @@ public function isDirectory($path) return $this->exists($formattedPath) && $this->fileSystem[$formattedPath]["type"] === "d"; } - public function size($path) - { - return $this->exists($path) ? 1 : 0; - } - public function append($file, $text) { if ($this->exists($file) && strlen($this->get($file))) { From e3eb851abc0878ced164a0774e193e44e931101c Mon Sep 17 00:00:00 2001 From: Daniel Wilkowski Date: Tue, 31 May 2022 10:53:13 +0200 Subject: [PATCH 4/5] Add Path --- includes/Support/Path.php | 68 +++++++++ tests/Psr4/Concerns/PhpunitConcern.php | 23 ++++ tests/Psr4/PhpUnitPolyfill.php | 16 +++ tests/Unit/Support/BasePathTest.php | 3 +- tests/Unit/Support/PathTest.php | 184 +++++++++++++++++++++++++ 5 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 includes/Support/Path.php create mode 100644 tests/Psr4/Concerns/PhpunitConcern.php create mode 100644 tests/Psr4/PhpUnitPolyfill.php create mode 100644 tests/Unit/Support/PathTest.php diff --git a/includes/Support/Path.php b/includes/Support/Path.php new file mode 100644 index 000000000..fa8c04ddb --- /dev/null +++ b/includes/Support/Path.php @@ -0,0 +1,68 @@ +children = $path; + } + + public static function of(string $path): Path + { + return new Path(\preg_split("#[\\\\/]#", $path)); + } + + public function append(string $child): Path + { + return new Path([...$this->children, $child]); + } + + public function toString(): string + { + return \join(\DIRECTORY_SEPARATOR, \iterator_to_array($this->children())); + } + + private function children(): Iterator + { + if (empty($this->children)) { + return new EmptyIterator(); + } + return $this->normalizedChildren(); + } + + private function normalizedChildren(): Iterator + { + [$root, $children] = $this->rootAndChildren(); + yield \rTrim($root, "/\\"); + foreach ($children as $child) { + if ($child === "") { + continue; + } + yield \trim($child, "/\\"); + } + } + + private function rootAndChildren(): array + { + $root = \reset($this->children); + return [$root, \array_slice($this->children, 1)]; + } +} diff --git a/tests/Psr4/Concerns/PhpunitConcern.php b/tests/Psr4/Concerns/PhpunitConcern.php new file mode 100644 index 000000000..19daabe17 --- /dev/null +++ b/tests/Psr4/Concerns/PhpunitConcern.php @@ -0,0 +1,23 @@ +expectException(Exception::class); + throw new Exception($message); + } +} diff --git a/tests/Psr4/PhpUnitPolyfill.php b/tests/Psr4/PhpUnitPolyfill.php new file mode 100644 index 000000000..6e0d9f844 --- /dev/null +++ b/tests/Psr4/PhpUnitPolyfill.php @@ -0,0 +1,16 @@ +toString(); + // then + $this->assertSame("", $asString); + } + + /** + * @test + */ + public function shouldIgnoreEmptyChildren() + { + // given + $path = new Path(["string", "", "", "string"]); + // when + $asString = $path->toString(); + // then + $this->assertSameWindows("string\string", $asString); + $this->assertSameUnix("string/string", $asString); + } + + /** + * @test + * @dataProvider fileNames + */ + public function shouldGetSingleFile(string $filename) + { + // given + $path = new Path([$filename]); + // when + $asString = $path->toString(); + // then + $this->assertSame("file.txt", $asString); + } + + public function fileNames(): array + { + return [["file.txt"], ["file.txt/"], ["file.txt\\"]]; + } + + /** + * @test + * @dataProvider pathPieces + */ + public function shouldGetManyFiles(array $pathPieces) + { + // given + $path = new Path($pathPieces); + // when + $asString = $path->toString(); + // then + $this->assertSameWindows('first\second\third\file.txt', $asString); + $this->assertSameUnix("first/second/third/file.txt", $asString); + } + + public function pathPieces(): array + { + return [ + [["first", "second", "third", "file.txt"]], + + [["first", "/second", "/third", "/file.txt"]], + [["first", "\second", '\third', '\file.txt']], + + [["first/", "second/", "third/", "file.txt"]], + [["first\\", "second\\", "third\\", "file.txt"]], + + [["first/", "/second/", "/third/", "/file.txt"]], + [["first\\", "\\second\\", '\\third\\', '\\file.txt']], + ]; + } + + /** + * @test + * @dataProvider paths + */ + public function shouldRepresentPath(string $stringPath) + { + // given + $path = Path::of($stringPath); + // when + $asString = $path->toString(); + // then + $this->assertSameWindows('one\two\three\file.txt', $asString); + $this->assertSameUnix("one/two/three/file.txt", $asString); + } + + public function paths(): array + { + return [['one\two\three\file.txt'], ["one/two/three/file.txt"]]; + } + + /** + * @test + * @dataProvider children + */ + public function shouldAppendPath(Path $path, string $appendant) + { + // when + $childPath = $path->append($appendant); + // then + $this->assertSameWindows('uno\dos\tres', $childPath->toString()); + $this->assertSameUnix("uno/dos/tres", $childPath->toString()); + } + + public function children(): array + { + return [ + [Path::of("uno/dos"), "tres"], + [Path::of("uno/dos"), '\tres'], + [Path::of("uno/dos"), "/tres"], + [Path::of("uno/dos"), "tres/"], + [Path::of("uno/dos"), "tres\\"], + + [Path::of("uno/dos/"), "tres"], + [Path::of("uno/dos/"), '\tres'], + [Path::of("uno/dos/"), "/tres"], + + [Path::of("uno/dos\\"), "tres"], + [Path::of("uno/dos\\"), '\tres'], + [Path::of("uno/dos\\"), "/tres"], + ]; + } + + /** + * @test + */ + public function shouldAcceptPathWithDriveOnWindows() + { + if ($this->isUnix()) { + $this->markTestUnnecessary("There are no drives on Unix"); + } + // given + $path = Path::of("C:\directory"); + // when + $child = $path->append("file.txt"); + // then + $this->assertSame('C:\directory\file.txt', $child->toString()); + } + + /** + * @test + */ + public function shouldRemainAbsolutePathOnUnix() + { + if (!$this->isUnix()) { + $this->markTestUnnecessary("There are no leading separators on Windows"); + } + // given + $path = Path::of("/usr/bin"); + // when + $child = $path->append("local"); + // then + $this->assertSame("/usr/bin/local", $child->toString()); + } + + /** + * @test + */ + public function shouldBeImmutable() + { + // given + $path = Path::of("one/two/three"); + // when + $this->assertSame($path->toString(), $path->toString()); + } +} From 7bb2741fc4f41c018d7f2db889ff543fad8ae882 Mon Sep 17 00:00:00 2001 From: Daniel Wilkowski Date: Tue, 26 Jul 2022 12:13:34 +0200 Subject: [PATCH 5/5] Pass directory separator as argument to Path.toString(), so both architectures can be tested on either of them --- includes/Support/Path.php | 4 +- tests/Psr4/Concerns/SystemConcern.php | 16 ++++++++ tests/Unit/Support/PathTest.php | 58 +++++++++++---------------- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/includes/Support/Path.php b/includes/Support/Path.php index fa8c04ddb..af0efd15b 100644 --- a/includes/Support/Path.php +++ b/includes/Support/Path.php @@ -35,9 +35,9 @@ public function append(string $child): Path return new Path([...$this->children, $child]); } - public function toString(): string + public function toString(string $pathSeparator): string { - return \join(\DIRECTORY_SEPARATOR, \iterator_to_array($this->children())); + return \join($pathSeparator, \iterator_to_array($this->children())); } private function children(): Iterator diff --git a/tests/Psr4/Concerns/SystemConcern.php b/tests/Psr4/Concerns/SystemConcern.php index 3fee50d54..73764cb70 100644 --- a/tests/Psr4/Concerns/SystemConcern.php +++ b/tests/Psr4/Concerns/SystemConcern.php @@ -1,10 +1,26 @@ assertPath($expected, $actual, "\\"); + } + + public function assertPathUnix(string $expected, Path $actual): void + { + $this->assertPath($expected, $actual, "/"); + } + + private function assertPath(string $expected, Path $actual, string $separator): void + { + Assert::assertSame($expected, $actual->toString($separator)); + } + public function assertSameWindows($expected, $actual): void { if (!$this->isUnix()) { diff --git a/tests/Unit/Support/PathTest.php b/tests/Unit/Support/PathTest.php index a12e160ff..72df030c4 100644 --- a/tests/Unit/Support/PathTest.php +++ b/tests/Unit/Support/PathTest.php @@ -17,10 +17,8 @@ public function shouldGetEmptyPath() { // given $path = new Path([]); - // when - $asString = $path->toString(); - // then - $this->assertSame("", $asString); + // when, then + $this->assertPathUnix("", $path); } /** @@ -30,11 +28,9 @@ public function shouldIgnoreEmptyChildren() { // given $path = new Path(["string", "", "", "string"]); - // when - $asString = $path->toString(); - // then - $this->assertSameWindows("string\string", $asString); - $this->assertSameUnix("string/string", $asString); + // when, then + $this->assertPathWindows("string\string", $path); + $this->assertPathUnix("string/string", $path); } /** @@ -45,10 +41,9 @@ public function shouldGetSingleFile(string $filename) { // given $path = new Path([$filename]); - // when - $asString = $path->toString(); - // then - $this->assertSame("file.txt", $asString); + // when, then + $this->assertPathWindows("file.txt", $path); + $this->assertPathUnix("file.txt", $path); } public function fileNames(): array @@ -64,11 +59,9 @@ public function shouldGetManyFiles(array $pathPieces) { // given $path = new Path($pathPieces); - // when - $asString = $path->toString(); - // then - $this->assertSameWindows('first\second\third\file.txt', $asString); - $this->assertSameUnix("first/second/third/file.txt", $asString); + // when, then + $this->assertPathWindows('first\second\third\file.txt', $path); + $this->assertPathUnix("first/second/third/file.txt", $path); } public function pathPieces(): array @@ -95,11 +88,9 @@ public function shouldRepresentPath(string $stringPath) { // given $path = Path::of($stringPath); - // when - $asString = $path->toString(); - // then - $this->assertSameWindows('one\two\three\file.txt', $asString); - $this->assertSameUnix("one/two/three/file.txt", $asString); + // when, then + $this->assertPathWindows('one\two\three\file.txt', $path); + $this->assertPathUnix("one/two/three/file.txt", $path); } public function paths(): array @@ -116,8 +107,8 @@ public function shouldAppendPath(Path $path, string $appendant) // when $childPath = $path->append($appendant); // then - $this->assertSameWindows('uno\dos\tres', $childPath->toString()); - $this->assertSameUnix("uno/dos/tres", $childPath->toString()); + $this->assertPathWindows('uno\dos\tres', $childPath); + $this->assertPathUnix("uno/dos/tres", $childPath); } public function children(): array @@ -142,17 +133,15 @@ public function children(): array /** * @test */ - public function shouldAcceptPathWithDriveOnWindows() + public function shouldAcceptPathWithDriveForWindows() { - if ($this->isUnix()) { - $this->markTestUnnecessary("There are no drives on Unix"); - } // given $path = Path::of("C:\directory"); // when $child = $path->append("file.txt"); // then - $this->assertSame('C:\directory\file.txt', $child->toString()); + $this->assertPathWindows('C:\directory\file.txt', $child); + $this->assertPathUnix("C:/directory/file.txt", $child); } /** @@ -160,15 +149,13 @@ public function shouldAcceptPathWithDriveOnWindows() */ public function shouldRemainAbsolutePathOnUnix() { - if (!$this->isUnix()) { - $this->markTestUnnecessary("There are no leading separators on Windows"); - } // given $path = Path::of("/usr/bin"); // when $child = $path->append("local"); // then - $this->assertSame("/usr/bin/local", $child->toString()); + $this->assertPathWindows("\usr\bin\local", $child); + $this->assertPathUnix("/usr/bin/local", $child); } /** @@ -179,6 +166,7 @@ public function shouldBeImmutable() // given $path = Path::of("one/two/three"); // when - $this->assertSame($path->toString(), $path->toString()); + $this->assertSame($path->toString("/"), $path->toString("/")); + $this->assertSame($path->toString("\\"), $path->toString("\\")); } }