From c57967ce478b1b067769d528298418bebb92b782 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 17:49:56 +0200 Subject: [PATCH 01/55] Create Filesystem.php --- packages/framework/src/Foundation/Filesystem.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 packages/framework/src/Foundation/Filesystem.php diff --git a/packages/framework/src/Foundation/Filesystem.php b/packages/framework/src/Foundation/Filesystem.php new file mode 100644 index 00000000000..44c9dc4463b --- /dev/null +++ b/packages/framework/src/Foundation/Filesystem.php @@ -0,0 +1,13 @@ +basePath = $basePath; + } +} From d061e5aae2d0f07b527916cef658bec9ff8d25c1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 17:55:24 +0200 Subject: [PATCH 02/55] Bind Filesystem class in Kernel constructor --- packages/framework/src/HydeKernel.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index 7195b05e6bb..a6ed9167e1f 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -6,6 +6,7 @@ use Hyde\Framework\Contracts\HydeKernelContract; use Hyde\Framework\Contracts\RouteContract; use Hyde\Framework\Exceptions\BaseUrlNotSetException; +use Hyde\Framework\Foundation\Filesystem; use Hyde\Framework\Helpers\Features; use Hyde\Framework\Models\Pages\BladePage; use Hyde\Framework\Models\Pages\DocumentationPage; @@ -32,10 +33,12 @@ class HydeKernel implements HydeKernelContract use Macroable; protected string $basePath; + protected Filesystem $filesystem; public function __construct(?string $basePath = null) { $this->setBasePath($basePath ?? getcwd()); + $this->filesystem = new Filesystem($this->basePath); } public static function getInstance(): HydeKernelContract From 086b60c9b263e47dee07b120080c07b495379584 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 18:19:50 +0200 Subject: [PATCH 03/55] Move filesystem logic from Kernel to Filesystem class --- .../framework/src/Foundation/Filesystem.php | 140 ++++++++++++++++++ packages/framework/src/HydeKernel.php | 97 ++---------- 2 files changed, 150 insertions(+), 87 deletions(-) diff --git a/packages/framework/src/Foundation/Filesystem.php b/packages/framework/src/Foundation/Filesystem.php index 44c9dc4463b..39539c036ab 100644 --- a/packages/framework/src/Foundation/Filesystem.php +++ b/packages/framework/src/Foundation/Filesystem.php @@ -2,6 +2,20 @@ namespace Hyde\Framework\Foundation; +use Hyde\Framework\HydeKernel; +use Hyde\Framework\Models\Pages\BladePage; +use Hyde\Framework\Models\Pages\DocumentationPage; +use Hyde\Framework\Models\Pages\MarkdownPage; +use Hyde\Framework\Models\Pages\MarkdownPost; +use Hyde\Framework\Services\DiscoveryService; +use Hyde\Framework\StaticPageBuilder; + +/** + * File helper methods. + * + * If a method uses the name `path` it refers to an internal file path. + * if a method uses the name `link` it refers to a web link used in Blade templates. + */ class Filesystem { protected string $basePath; @@ -10,4 +24,130 @@ public function __construct(string $basePath) { $this->basePath = $basePath; } + + public function getBasePath(): string + { + return $this->basePath; + } + + /** + * Get an absolute file path from a supplied relative path. + * + * The function returns the fully qualified path to your site's root directory. + * + * You may also use the function to generate a fully qualified path to a given file + * relative to the project root directory when supplying the path argument. + * + * @param string $path + * @return string + */ + public function path(string $path = ''): string + { + if (empty($path)) { + return $this->getBasePath(); + } + + $path = unslash($path); + + return $this->getBasePath().DIRECTORY_SEPARATOR.$path; + } + + /** + * Works similarly to the path() function, but returns a file in the Framework package. + * + * @param string $path + * @return string + */ + public function vendorPath(string $path = ''): string + { + return $this->path('vendor/hyde/framework/'.unslash($path)); + } + + + /** + * Wrapper for the copy function, but allows choosing if files may be overwritten. + * + * @param string $from The source file path. + * @param string $to The destination file path. + * @param bool $force If true, existing files will be overwritten. + * @return bool|int Returns true|false on copy() success|failure, or an error code on failure + */ + public function copy(string $from, string $to, bool $force = false): bool|int + { + if (! file_exists($from)) { + return 404; + } + + if (file_exists($to) && ! $force) { + return 409; + } + + return copy($from, $to); + } + + /** + * Fluent file helper methods. + * + * Provides a more fluent way of getting either the absolute path + * to a model's source directory, or an absolute path to a file within it. + * + * These are intended to be used as a dynamic alternative to legacy code + * Hyde::path('_pages/foo') becomes Hyde::getBladePagePath('foo') + */ + public function getModelSourcePath(string $model, string $path = ''): string + { + if (empty($path)) { + return $this->path(DiscoveryService::getFilePathForModelClassFiles($model)); + } + + $path = unslash($path); + + return $this->path(DiscoveryService::getFilePathForModelClassFiles($model).DIRECTORY_SEPARATOR.$path); + } + + public function getBladePagePath(string $path = ''): string + { + return $this->getModelSourcePath(BladePage::class, $path); + } + + public function getMarkdownPagePath(string $path = ''): string + { + return $this->getModelSourcePath(MarkdownPage::class, $path); + } + + public function getMarkdownPostPath(string $path = ''): string + { + return $this->getModelSourcePath(MarkdownPost::class, $path); + } + + public function getDocumentationPagePath(string $path = ''): string + { + return $this->getModelSourcePath(DocumentationPage::class, $path); + } + + /** + * Get the absolute path to the compiled site directory, or a file within it. + */ + public function getSiteOutputPath(string $path = ''): string + { + if (empty($path)) { + return StaticPageBuilder::$outputPath; + } + + $path = unslash($path); + + return StaticPageBuilder::$outputPath.DIRECTORY_SEPARATOR.$path; + } + + /** + * Decode an absolute path created with a Hyde::path() helper into its relative counterpart. + */ + public function pathToRelative(string $path): string + { + return str_starts_with($path, $this->path()) ? unslash(str_replace( + $this->path(), + '', + $path + )) : $path; + } } diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index a6ed9167e1f..584bc826ec8 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -84,44 +84,14 @@ public function makeTitle(string $slug): string )); } - /** - * File helper methods. - * - * If a method uses the name `path` it refers to an internal file path. - * if a method uses the name `link` it refers to a web link used in Blade templates. - */ - - /** - * Get an absolute file path from a supplied relative path. - * - * The function returns the fully qualified path to your site's root directory. - * - * You may also use the function to generate a fully qualified path to a given file - * relative to the project root directory when supplying the path argument. - * - * @param string $path - * @return string - */ public function path(string $path = ''): string { - if (empty($path)) { - return $this->getBasePath(); - } - - $path = unslash($path); - - return $this->getBasePath().DIRECTORY_SEPARATOR.$path; + return $this->filesystem->path($path); } - /** - * Works similarly to the path() function, but returns a file in the Framework package. - * - * @param string $path - * @return string - */ public function vendorPath(string $path = ''): string { - return $this->path('vendor/hyde/framework/'.unslash($path)); + return $this->filesystem->vendorPath($path); } /** @@ -248,90 +218,43 @@ public function url(string $path = '', ?string $default = null): string throw new BaseUrlNotSetException(); } - /** - * Wrapper for the copy function, but allows choosing if files may be overwritten. - * - * @param string $from The source file path. - * @param string $to The destination file path. - * @param bool $force If true, existing files will be overwritten. - * @return bool|int Returns true|false on copy() success|failure, or an error code on failure - */ public function copy(string $from, string $to, bool $force = false): bool|int { - if (! file_exists($from)) { - return 404; - } - - if (file_exists($to) && ! $force) { - return 409; - } - - return copy($from, $to); + return $this->filesystem->copy($from, $to, $force); } - /** - * Fluent file helper methods. - * - * Provides a more fluent way of getting either the absolute path - * to a model's source directory, or an absolute path to a file within it. - * - * These are intended to be used as a dynamic alternative to legacy code - * Hyde::path('_pages/foo') becomes Hyde::getBladePagePath('foo') - */ public function getModelSourcePath(string $model, string $path = ''): string { - if (empty($path)) { - return $this->path(DiscoveryService::getFilePathForModelClassFiles($model)); - } - - $path = unslash($path); - - return $this->path(DiscoveryService::getFilePathForModelClassFiles($model).DIRECTORY_SEPARATOR.$path); + return $this->filesystem->getModelSourcePath($model, $path); } public function getBladePagePath(string $path = ''): string { - return $this->getModelSourcePath(BladePage::class, $path); + return $this->filesystem->getBladePagePath($path); } public function getMarkdownPagePath(string $path = ''): string { - return $this->getModelSourcePath(MarkdownPage::class, $path); + return $this->filesystem->getMarkdownPagePath($path); } public function getMarkdownPostPath(string $path = ''): string { - return $this->getModelSourcePath(MarkdownPost::class, $path); + return $this->filesystem->getMarkdownPostPath($path); } public function getDocumentationPagePath(string $path = ''): string { - return $this->getModelSourcePath(DocumentationPage::class, $path); + return $this->filesystem->getDocumentationPagePath($path); } - /** - * Get the absolute path to the compiled site directory, or a file within it. - */ public function getSiteOutputPath(string $path = ''): string { - if (empty($path)) { - return StaticPageBuilder::$outputPath; - } - - $path = unslash($path); - - return StaticPageBuilder::$outputPath.DIRECTORY_SEPARATOR.$path; + return $this->filesystem->getSiteOutputPath($path); } - /** - * Decode an absolute path created with a Hyde::path() helper into its relative counterpart. - */ public function pathToRelative(string $path): string { - return str_starts_with($path, $this->path()) ? unslash(str_replace( - $this->path(), - '', - $path - )) : $path; + return $this->filesystem->pathToRelative($path); } } From cd04298fcfbe097d06bb02f217e3d35b3437f0e7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 18:21:32 +0200 Subject: [PATCH 04/55] Group together filesystem helpers --- packages/framework/src/HydeKernel.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index 584bc826ec8..b9231287c52 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -84,16 +84,6 @@ public function makeTitle(string $slug): string )); } - public function path(string $path = ''): string - { - return $this->filesystem->path($path); - } - - public function vendorPath(string $path = ''): string - { - return $this->filesystem->vendorPath($path); - } - /** * Format a link to an HTML file, allowing for pretty URLs, if enabled. * @@ -218,6 +208,16 @@ public function url(string $path = '', ?string $default = null): string throw new BaseUrlNotSetException(); } + public function path(string $path = ''): string + { + return $this->filesystem->path($path); + } + + public function vendorPath(string $path = ''): string + { + return $this->filesystem->vendorPath($path); + } + public function copy(string $from, string $to, bool $force = false): bool|int { return $this->filesystem->copy($from, $to, $force); From f00f2bf34f55a87f144591ffef7f1a5be304203c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 18:21:44 +0200 Subject: [PATCH 05/55] Remove unused imports --- packages/framework/src/Foundation/Filesystem.php | 1 - packages/framework/src/HydeKernel.php | 4 ---- 2 files changed, 5 deletions(-) diff --git a/packages/framework/src/Foundation/Filesystem.php b/packages/framework/src/Foundation/Filesystem.php index 39539c036ab..656d10b8949 100644 --- a/packages/framework/src/Foundation/Filesystem.php +++ b/packages/framework/src/Foundation/Filesystem.php @@ -2,7 +2,6 @@ namespace Hyde\Framework\Foundation; -use Hyde\Framework\HydeKernel; use Hyde\Framework\Models\Pages\BladePage; use Hyde\Framework\Models\Pages\DocumentationPage; use Hyde\Framework\Models\Pages\MarkdownPage; diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index b9231287c52..0af67e114d1 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -8,11 +8,7 @@ use Hyde\Framework\Exceptions\BaseUrlNotSetException; use Hyde\Framework\Foundation\Filesystem; use Hyde\Framework\Helpers\Features; -use Hyde\Framework\Models\Pages\BladePage; use Hyde\Framework\Models\Pages\DocumentationPage; -use Hyde\Framework\Models\Pages\MarkdownPage; -use Hyde\Framework\Models\Pages\MarkdownPost; -use Hyde\Framework\Services\DiscoveryService; use Illuminate\Support\Facades\View; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; From e8d743ea4f52fb6b60d0318d806cc2b99ea7854f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 30 Jul 2022 16:23:09 +0000 Subject: [PATCH 06/55] Apply fixes from StyleCI --- packages/framework/src/Foundation/Filesystem.php | 1 - packages/framework/src/HydeKernel.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Foundation/Filesystem.php b/packages/framework/src/Foundation/Filesystem.php index 656d10b8949..3a3d75d3ded 100644 --- a/packages/framework/src/Foundation/Filesystem.php +++ b/packages/framework/src/Foundation/Filesystem.php @@ -62,7 +62,6 @@ public function vendorPath(string $path = ''): string return $this->path('vendor/hyde/framework/'.unslash($path)); } - /** * Wrapper for the copy function, but allows choosing if files may be overwritten. * diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index 0af67e114d1..e461f68f6d7 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -246,7 +246,7 @@ public function getDocumentationPagePath(string $path = ''): string public function getSiteOutputPath(string $path = ''): string { - return $this->filesystem->getSiteOutputPath($path); + return $this->filesystem->getSiteOutputPath($path); } public function pathToRelative(string $path): string From 7def015e5bb55e5acd9c7525b46ee521c8c97887 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 18:28:21 +0200 Subject: [PATCH 07/55] Base path should only accept strings --- packages/framework/src/HydeKernel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index 0af67e114d1..45aab3015ce 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -52,7 +52,7 @@ public function getBasePath(): string return $this->basePath; } - public function setBasePath($basePath) + public function setBasePath(string $basePath) { $this->basePath = rtrim($basePath, '/\\'); } From 23bb44bde18e9843f0a3eb232f8fed9172370f73 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 18:30:49 +0200 Subject: [PATCH 08/55] Pass the Kernel to Filesystem constructor instead of the path --- packages/framework/src/Foundation/Filesystem.php | 7 ++++--- packages/framework/src/HydeKernel.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Foundation/Filesystem.php b/packages/framework/src/Foundation/Filesystem.php index 656d10b8949..59a61296799 100644 --- a/packages/framework/src/Foundation/Filesystem.php +++ b/packages/framework/src/Foundation/Filesystem.php @@ -2,6 +2,7 @@ namespace Hyde\Framework\Foundation; +use Hyde\Framework\Contracts\HydeKernelContract; use Hyde\Framework\Models\Pages\BladePage; use Hyde\Framework\Models\Pages\DocumentationPage; use Hyde\Framework\Models\Pages\MarkdownPage; @@ -17,11 +18,11 @@ */ class Filesystem { - protected string $basePath; + protected HydeKernelContract $kernel; - public function __construct(string $basePath) + public function __construct(HydeKernelContract $kernel) { - $this->basePath = $basePath; + $this->kernel = $kernel; } public function getBasePath(): string diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index 45aab3015ce..d1ed0cbc408 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -34,7 +34,7 @@ class HydeKernel implements HydeKernelContract public function __construct(?string $basePath = null) { $this->setBasePath($basePath ?? getcwd()); - $this->filesystem = new Filesystem($this->basePath); + $this->filesystem = new Filesystem($this); } public static function getInstance(): HydeKernelContract From abf7f52f0d73dd6cae4e32c732c1532eaab7e0f8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 18:31:27 +0200 Subject: [PATCH 09/55] Get the base path from the Kernel --- packages/framework/src/Foundation/Filesystem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Foundation/Filesystem.php b/packages/framework/src/Foundation/Filesystem.php index 59a61296799..2a3fec2e180 100644 --- a/packages/framework/src/Foundation/Filesystem.php +++ b/packages/framework/src/Foundation/Filesystem.php @@ -27,7 +27,7 @@ public function __construct(HydeKernelContract $kernel) public function getBasePath(): string { - return $this->basePath; + return $this->kernel->getBasePath(); } /** From 9c45062cc4329586d6d4b887de576b3349550821 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 18:45:18 +0200 Subject: [PATCH 10/55] Create Hyperlinks.php --- .../framework/src/Foundation/Hyperlinks.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 packages/framework/src/Foundation/Hyperlinks.php diff --git a/packages/framework/src/Foundation/Hyperlinks.php b/packages/framework/src/Foundation/Hyperlinks.php new file mode 100644 index 00000000000..1f5de7a800a --- /dev/null +++ b/packages/framework/src/Foundation/Hyperlinks.php @@ -0,0 +1,18 @@ +kernel = $kernel; + } +} From 9bdd960cceb47b4882fe077d447a73f919d1b28d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 18:56:13 +0200 Subject: [PATCH 11/55] Bind Hyperlinks class in Kernel constructor --- packages/framework/src/HydeKernel.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index b47aa703fca..d6472e80f4e 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -7,6 +7,7 @@ use Hyde\Framework\Contracts\RouteContract; use Hyde\Framework\Exceptions\BaseUrlNotSetException; use Hyde\Framework\Foundation\Filesystem; +use Hyde\Framework\Foundation\Hyperlinks; use Hyde\Framework\Helpers\Features; use Hyde\Framework\Models\Pages\DocumentationPage; use Illuminate\Support\Facades\View; @@ -30,11 +31,13 @@ class HydeKernel implements HydeKernelContract protected string $basePath; protected Filesystem $filesystem; + protected Hyperlinks $hyperlinks; public function __construct(?string $basePath = null) { $this->setBasePath($basePath ?? getcwd()); $this->filesystem = new Filesystem($this); + $this->hyperlinks = new Hyperlinks($this); } public static function getInstance(): HydeKernelContract From d0f9528265be373b6e4dfdd04ea937e03462fd23 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 19:26:10 +0200 Subject: [PATCH 12/55] Move web links logic from Kernel to Hyperlinks class --- .../framework/src/Foundation/Hyperlinks.php | 111 ++++++++++++++++++ packages/framework/src/HydeKernel.php | 92 +-------------- 2 files changed, 117 insertions(+), 86 deletions(-) diff --git a/packages/framework/src/Foundation/Hyperlinks.php b/packages/framework/src/Foundation/Hyperlinks.php index 1f5de7a800a..e3f14efe113 100644 --- a/packages/framework/src/Foundation/Hyperlinks.php +++ b/packages/framework/src/Foundation/Hyperlinks.php @@ -3,6 +3,8 @@ namespace Hyde\Framework\Foundation; use Hyde\Framework\Contracts\HydeKernelContract; +use Hyde\Framework\Exceptions\BaseUrlNotSetException; +use Hyde\Framework\Models\Pages\DocumentationPage; /** * Contains helpers and logic for resolving web paths for compiled files. @@ -15,4 +17,113 @@ public function __construct(HydeKernelContract $kernel) { $this->kernel = $kernel; } + + /** + * Format a link to an HTML file, allowing for pretty URLs, if enabled. + * + * @see \Hyde\Framework\Testing\Unit\FileHelperPageLinkPrettyUrlTest + */ + public function formatHtmlPath(string $destination): string + { + if (config('site.pretty_urls', false) === true) { + if (str_ends_with($destination, '.html')) { + if ($destination === 'index.html') { + return '/'; + } + if ($destination === DocumentationPage::getOutputDirectory().'/index.html') { + return DocumentationPage::getOutputDirectory().'/'; + } + + return substr($destination, 0, -5); + } + } + + return $destination; + } + + + /** + * Inject the proper number of `../` before the links in Blade templates. + * + * @param string $destination relative to output directory on compiled site + * @return string + * + * @see \Hyde\Framework\Testing\Unit\FileHelperRelativeLinkTest + */ + public function relativeLink(string $destination): string + { + if (str_starts_with($destination, '../')) { + return $destination; + } + + $nestCount = substr_count($this->kernel->currentPage(), '/'); + $route = ''; + if ($nestCount > 0) { + $route .= str_repeat('../', $nestCount); + } + $route .= $this->formatHtmlPath($destination); + + return str_replace('//', '/', $route); + } + + /** + * Gets a relative web link to the given image stored in the _site/media folder. + */ + public function image(string $name): string + { + if (str_starts_with($name, 'http')) { + return $name; + } + + return $this->relativeLink('media/'.basename($name)); + } + + /** + * Return a qualified URI path, if SITE_URL is set in .env, else return false. + * + * @deprecated v0.53.0-beta - Use Hyde::url() or Hyde::hasSiteUrl() instead. + * + * @param string $path optional relative path suffix. Omit to return base url. + * @return string|false + */ + public function uriPath(string $path = ''): string|false + { + if (config('site.url', false)) { + return rtrim(config('site.url'), '/').'/'.(trim($path, '/') ?? ''); + } + + return false; + } + + /** + * Check if a site base URL has been set in config (or .env). + */ + public function hasSiteUrl(): bool + { + return ! blank(config('site.url')); + } + + /** + * Return a qualified URI path to the supplied path if a base URL is set. + * + * @param string $path optional relative path suffix. Omit to return base url. + * @param string|null $default optional default value to return if no site url is set. + * @return string + * + * @throws BaseUrlNotSetException If no site URL is set and no default is provided + */ + public function url(string $path = '', ?string $default = null): string + { + $path = $this->formatHtmlPath(trim($path, '/')); + + if ($this->hasSiteUrl()) { + return rtrim(rtrim(config('site.url'), '/').'/'.($path ?? ''), '/'); + } + + if ($default !== null) { + return $default.'/'.($path ?? ''); + } + + throw new BaseUrlNotSetException(); + } } diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index d6472e80f4e..64a11fcb2a1 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -5,11 +5,9 @@ use Composer\InstalledVersions; use Hyde\Framework\Contracts\HydeKernelContract; use Hyde\Framework\Contracts\RouteContract; -use Hyde\Framework\Exceptions\BaseUrlNotSetException; use Hyde\Framework\Foundation\Filesystem; use Hyde\Framework\Foundation\Hyperlinks; use Hyde\Framework\Helpers\Features; -use Hyde\Framework\Models\Pages\DocumentationPage; use Illuminate\Support\Facades\View; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; @@ -83,51 +81,14 @@ public function makeTitle(string $slug): string )); } - /** - * Format a link to an HTML file, allowing for pretty URLs, if enabled. - * - * @see \Hyde\Framework\Testing\Unit\FileHelperPageLinkPrettyUrlTest - */ public function formatHtmlPath(string $destination): string { - if (config('site.pretty_urls', false) === true) { - if (str_ends_with($destination, '.html')) { - if ($destination === 'index.html') { - return '/'; - } - if ($destination === DocumentationPage::getOutputDirectory().'/index.html') { - return DocumentationPage::getOutputDirectory().'/'; - } - - return substr($destination, 0, -5); - } - } - - return $destination; + return $this->hyperlinks->formatHtmlPath($destination); } - /** - * Inject the proper number of `../` before the links in Blade templates. - * - * @param string $destination relative to output directory on compiled site - * @return string - * - * @see \Hyde\Framework\Testing\Unit\FileHelperRelativeLinkTest - */ public function relativeLink(string $destination): string { - if (str_starts_with($destination, '../')) { - return $destination; - } - - $nestCount = substr_count($this->currentPage(), '/'); - $route = ''; - if ($nestCount > 0) { - $route .= str_repeat('../', $nestCount); - } - $route .= $this->formatHtmlPath($destination); - - return str_replace('//', '/', $route); + return $this->hyperlinks->relativeLink($destination); } /** @@ -146,65 +107,24 @@ public function currentRoute(): ?RouteContract return View::shared('currentRoute'); } - /** - * Gets a relative web link to the given image stored in the _site/media folder. - */ public function image(string $name): string { - if (str_starts_with($name, 'http')) { - return $name; - } - - return $this->relativeLink('media/'.basename($name)); + return $this->hyperlinks->image($name); } - /** - * Return a qualified URI path, if SITE_URL is set in .env, else return false. - * - * @deprecated v0.53.0-beta - Use Hyde::url() or Hyde::hasSiteUrl() instead. - * - * @param string $path optional relative path suffix. Omit to return base url. - * @return string|false - */ public function uriPath(string $path = ''): string|false { - if (config('site.url', false)) { - return rtrim(config('site.url'), '/').'/'.(trim($path, '/') ?? ''); - } - - return false; + return $this->hyperlinks->uriPath($path); } - /** - * Check if a site base URL has been set in config (or .env). - */ public function hasSiteUrl(): bool { - return ! blank(config('site.url')); + return $this->hyperlinks->hasSiteUrl(); } - /** - * Return a qualified URI path to the supplied path if a base URL is set. - * - * @param string $path optional relative path suffix. Omit to return base url. - * @param string|null $default optional default value to return if no site url is set. - * @return string - * - * @throws BaseUrlNotSetException If no site URL is set and no default is provided - */ public function url(string $path = '', ?string $default = null): string { - $path = $this->formatHtmlPath(trim($path, '/')); - - if ($this->hasSiteUrl()) { - return rtrim(rtrim(config('site.url'), '/').'/'.($path ?? ''), '/'); - } - - if ($default !== null) { - return $default.'/'.($path ?? ''); - } - - throw new BaseUrlNotSetException(); + return $this->hyperlinks->url($path, $default); } public function path(string $path = ''): string From adb27c3f2a657e8c31e4af544c8dcd74cacbe330 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 19:27:21 +0200 Subject: [PATCH 13/55] Reorder methods --- packages/framework/src/HydeKernel.php | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index 64a11fcb2a1..3b79b467dcf 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -70,6 +70,22 @@ public function hasFeature(string $feature): bool return Features::enabled($feature); } + /** + * Get the current page path, or fall back to the root path. + */ + public function currentPage(): string + { + return View::shared('currentPage', ''); + } + + /** + * Get the current page route, or fall back to null. + */ + public function currentRoute(): ?RouteContract + { + return View::shared('currentRoute'); + } + public function makeTitle(string $slug): string { $alwaysLowercase = ['a', 'an', 'the', 'in', 'on', 'by', 'with', 'of', 'and', 'or', 'but']; @@ -91,22 +107,6 @@ public function relativeLink(string $destination): string return $this->hyperlinks->relativeLink($destination); } - /** - * Get the current page path, or fall back to the root path. - */ - public function currentPage(): string - { - return View::shared('currentPage', ''); - } - - /** - * Get the current page route, or fall back to null. - */ - public function currentRoute(): ?RouteContract - { - return View::shared('currentRoute'); - } - public function image(string $name): string { return $this->hyperlinks->image($name); From c4785b4d7a31001be7a8e38281c53c72581320d4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 19:34:58 +0200 Subject: [PATCH 14/55] Remove unnecessary comments --- packages/framework/src/HydeKernel.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index 3b79b467dcf..cb7ef11e637 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -58,8 +58,6 @@ public function setBasePath(string $basePath) $this->basePath = rtrim($basePath, '/\\'); } - // HydeHelperFacade - public function features(): Features { return new Features; @@ -70,17 +68,11 @@ public function hasFeature(string $feature): bool return Features::enabled($feature); } - /** - * Get the current page path, or fall back to the root path. - */ public function currentPage(): string { return View::shared('currentPage', ''); } - /** - * Get the current page route, or fall back to null. - */ public function currentRoute(): ?RouteContract { return View::shared('currentRoute'); From 48ec6b7ec0bcdcc60483bbb955fae5d5d2cd4893 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 30 Jul 2022 17:35:08 +0000 Subject: [PATCH 15/55] Apply fixes from StyleCI --- packages/framework/src/Foundation/Hyperlinks.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Foundation/Hyperlinks.php b/packages/framework/src/Foundation/Hyperlinks.php index e3f14efe113..cef8bd94338 100644 --- a/packages/framework/src/Foundation/Hyperlinks.php +++ b/packages/framework/src/Foundation/Hyperlinks.php @@ -41,7 +41,6 @@ public function formatHtmlPath(string $destination): string return $destination; } - /** * Inject the proper number of `../` before the links in Blade templates. * From 9fe6c0a9359bd1164a94c8c0456bed31fe4491b4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 20:30:49 +0200 Subject: [PATCH 16/55] Change contract type to concrete --- packages/framework/src/Foundation/Hyperlinks.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Foundation/Hyperlinks.php b/packages/framework/src/Foundation/Hyperlinks.php index e3f14efe113..c366da3c34e 100644 --- a/packages/framework/src/Foundation/Hyperlinks.php +++ b/packages/framework/src/Foundation/Hyperlinks.php @@ -2,18 +2,18 @@ namespace Hyde\Framework\Foundation; -use Hyde\Framework\Contracts\HydeKernelContract; -use Hyde\Framework\Exceptions\BaseUrlNotSetException; +use Hyde\Framework\HydeKernel; use Hyde\Framework\Models\Pages\DocumentationPage; +use Hyde\Framework\Exceptions\BaseUrlNotSetException; /** * Contains helpers and logic for resolving web paths for compiled files. */ class Hyperlinks { - protected HydeKernelContract $kernel; + protected HydeKernel $kernel; - public function __construct(HydeKernelContract $kernel) + public function __construct(HydeKernel $kernel) { $this->kernel = $kernel; } From 1cc5b9f76969a999c1dcc64065b90ac628495618 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 30 Jul 2022 18:31:13 +0000 Subject: [PATCH 17/55] Apply fixes from StyleCI --- packages/framework/src/Foundation/Hyperlinks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Foundation/Hyperlinks.php b/packages/framework/src/Foundation/Hyperlinks.php index 4e70726912d..877b097a72f 100644 --- a/packages/framework/src/Foundation/Hyperlinks.php +++ b/packages/framework/src/Foundation/Hyperlinks.php @@ -2,9 +2,9 @@ namespace Hyde\Framework\Foundation; +use Hyde\Framework\Exceptions\BaseUrlNotSetException; use Hyde\Framework\HydeKernel; use Hyde\Framework\Models\Pages\DocumentationPage; -use Hyde\Framework\Exceptions\BaseUrlNotSetException; /** * Contains helpers and logic for resolving web paths for compiled files. From 84bb345162bff7ecb93a58414a4e3339ef2374e9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 20:46:58 +0200 Subject: [PATCH 18/55] Add class annotations --- packages/framework/src/Foundation/Filesystem.php | 2 +- packages/framework/src/Foundation/Hyperlinks.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Foundation/Filesystem.php b/packages/framework/src/Foundation/Filesystem.php index c106b746efc..e22002e3f54 100644 --- a/packages/framework/src/Foundation/Filesystem.php +++ b/packages/framework/src/Foundation/Filesystem.php @@ -11,7 +11,7 @@ use Hyde\Framework\StaticPageBuilder; /** - * File helper methods. + * File helper methods, bound to the HydeKernel instance, and is an integral part of the framework. * * If a method uses the name `path` it refers to an internal file path. * if a method uses the name `link` it refers to a web link used in Blade templates. diff --git a/packages/framework/src/Foundation/Hyperlinks.php b/packages/framework/src/Foundation/Hyperlinks.php index 877b097a72f..0a514f35fb2 100644 --- a/packages/framework/src/Foundation/Hyperlinks.php +++ b/packages/framework/src/Foundation/Hyperlinks.php @@ -8,6 +8,8 @@ /** * Contains helpers and logic for resolving web paths for compiled files. + * + * It's bound to the HydeKernel instance, and is an integral part of the framework. */ class Hyperlinks { From b172a3360b5d8a7ec221e3f9a06aedf58b755daf Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 20:49:14 +0200 Subject: [PATCH 19/55] Create FilesystemTest.php --- .../Feature/Foundation/FilesystemTest.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 packages/framework/tests/Feature/Foundation/FilesystemTest.php diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php new file mode 100644 index 00000000000..5487161a1c5 --- /dev/null +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -0,0 +1,67 @@ + Date: Sat, 30 Jul 2022 20:49:33 +0200 Subject: [PATCH 20/55] Link to the test --- packages/framework/src/Foundation/Filesystem.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Foundation/Filesystem.php b/packages/framework/src/Foundation/Filesystem.php index e22002e3f54..d9f874d2cfe 100644 --- a/packages/framework/src/Foundation/Filesystem.php +++ b/packages/framework/src/Foundation/Filesystem.php @@ -15,6 +15,8 @@ * * If a method uses the name `path` it refers to an internal file path. * if a method uses the name `link` it refers to a web link used in Blade templates. + * + * @see \Hyde\Framework\Testing\Feature\Foundation\FilesystemTest */ class Filesystem { From 49d7e9b1ad41ca8803ef0f15bfb94fd7bf35c167 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 20:55:08 +0200 Subject: [PATCH 21/55] Rearrange methods to match actual class --- .../Feature/Foundation/FilesystemTest.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 5487161a1c5..698fbabf91e 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -10,57 +10,57 @@ */ class FilesystemTest extends TestCase { - public function testGetDocumentationPagePath() + public function test_get_base_path() { } - public function testGetModelSourcePath() + public function test_path() { } - public function testPathToRelative() + public function test_vendor_path() { } - public function testGetMarkdownPagePath() + public function test_copy() { } - public function testGetBasePath() + public function test_get_model_source_path() { } - public function testVendorPath() + public function test_get_blade_page_path() { } - public function testGetBladePagePath() + public function test_get_markdown_page_path() { } - public function testCopy() + public function test_get_markdown_post_path() { } - public function testGetMarkdownPostPath() + public function test_get_documentation_page_path() { } - public function testGetSiteOutputPath() + public function test_get_site_output_path() { } - public function testPath() + public function test_path_to_relative() { } From 84beb33754326fe9c4eae7bc5e0bc205041dcfde Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 20:58:49 +0200 Subject: [PATCH 22/55] Test base path method --- .../framework/tests/Feature/Foundation/FilesystemTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 698fbabf91e..178a458641d 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -2,6 +2,7 @@ namespace Hyde\Framework\Testing\Feature\Foundation; +use Hyde\Framework\HydeKernel; use Hyde\Testing\TestCase; use Hyde\Framework\Foundation\Filesystem; @@ -10,9 +11,12 @@ */ class FilesystemTest extends TestCase { - public function test_get_base_path() + public function test_get_base_path_returns_kernels_base_path() { - + $kernel = $this->mock(HydeKernel::class); + $kernel->shouldReceive('getBasePath')->andReturn('/path/to/project'); + $filesystem = new Filesystem($kernel); + $this->assertEquals('/path/to/project', $filesystem->getBasePath()); } public function test_path() From 00d4e4a5d403122845d354926c09791c50e73f54 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 21:01:39 +0200 Subject: [PATCH 23/55] Set up Filesystem instance in setUp method --- .../tests/Feature/Foundation/FilesystemTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 178a458641d..c0e23da081e 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -11,6 +11,15 @@ */ class FilesystemTest extends TestCase { + protected Filesystem $filesystem; + + protected function setUp(): void + { + parent::setUp(); + + $this->filesystem = new Filesystem(new HydeKernel()); + } + public function test_get_base_path_returns_kernels_base_path() { $kernel = $this->mock(HydeKernel::class); From cc5110b419990e07285f6193562aa6a21feaf0d8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 21:01:55 +0200 Subject: [PATCH 24/55] Move path method tests to new logic location --- .../Feature/Foundation/FilesystemTest.php | 24 ++++++++++++++++++- .../tests/Unit/HydePathHelperTest.php | 22 ----------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index c0e23da081e..15f5c42d8e5 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -28,9 +28,31 @@ public function test_get_base_path_returns_kernels_base_path() $this->assertEquals('/path/to/project', $filesystem->getBasePath()); } - public function test_path() + public function test_path_method() { + $this->assertTrue(method_exists(Filesystem::class, 'path')); + } + + public function test_path_method_returns_qualified_file_path_when_supplied_with_argument() + { + $this->assertEquals($this->filesystem->path('file.php'), $this->filesystem->path().DIRECTORY_SEPARATOR.'file.php'); + } + public function test_path_method_strips_trailing_directory_separators_from_argument() + { + $this->assertEquals($this->filesystem->path('\\/file.php/'), $this->filesystem->path().DIRECTORY_SEPARATOR.'file.php'); + } + + public function test_path_method_returns_expected_value_for_nested_path_arguments() + { + $this->assertEquals($this->filesystem->path('directory/file.php'), $this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php'); + } + + public function test_path_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument() + { + $this->assertEquals($this->filesystem->path('directory/file.php/'), $this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php'); + $this->assertEquals($this->filesystem->path('/directory/file.php/'), $this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php'); + $this->assertEquals($this->filesystem->path('\\/directory/file.php/'), $this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php'); } public function test_vendor_path() diff --git a/packages/framework/tests/Unit/HydePathHelperTest.php b/packages/framework/tests/Unit/HydePathHelperTest.php index 609368d0c87..db502d27233 100644 --- a/packages/framework/tests/Unit/HydePathHelperTest.php +++ b/packages/framework/tests/Unit/HydePathHelperTest.php @@ -32,26 +32,4 @@ public function test_returned_directory_contains_content_expected_to_be_in_the_p file_exists(Hyde::path().DIRECTORY_SEPARATOR.'_site') ); } - - public function test_method_returns_qualified_file_path_when_supplied_with_argument() - { - $this->assertEquals(Hyde::path('file.php'), Hyde::path().DIRECTORY_SEPARATOR.'file.php'); - } - - public function test_method_strips_trailing_directory_separators_from_argument() - { - $this->assertEquals(Hyde::path('\\/file.php/'), Hyde::path().DIRECTORY_SEPARATOR.'file.php'); - } - - public function test_method_returns_expected_value_for_nested_path_arguments() - { - $this->assertEquals(Hyde::path('directory/file.php'), Hyde::path().DIRECTORY_SEPARATOR.'directory/file.php'); - } - - public function test_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument() - { - $this->assertEquals(Hyde::path('directory/file.php/'), Hyde::path().DIRECTORY_SEPARATOR.'directory/file.php'); - $this->assertEquals(Hyde::path('/directory/file.php/'), Hyde::path().DIRECTORY_SEPARATOR.'directory/file.php'); - $this->assertEquals(Hyde::path('\\/directory/file.php/'), Hyde::path().DIRECTORY_SEPARATOR.'directory/file.php'); - } } From 7f86af0ef0b2a88d6ef0d9d5588c76468ab8519d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 21:06:22 +0200 Subject: [PATCH 25/55] Add few more path tests --- .../Feature/Foundation/FilesystemTest.php | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 15f5c42d8e5..8654e09b0c9 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -28,11 +28,32 @@ public function test_get_base_path_returns_kernels_base_path() $this->assertEquals('/path/to/project', $filesystem->getBasePath()); } - public function test_path_method() + public function test_path_method_exists() { $this->assertTrue(method_exists(Filesystem::class, 'path')); } + public function test_path_method_returns_string() + { + $this->assertIsString($this->filesystem->path()); + } + + public function test_path_method_returns_base_path_when_not_supplied_with_argument() + { + $kernel = $this->mock(HydeKernel::class); + $kernel->shouldReceive('getBasePath')->andReturn('/path/to/project'); + $filesystem = new Filesystem($kernel); + $this->assertEquals('/path/to/project', $filesystem->path()); + } + + public function test_path_method_returns_path_relative_to_base_path_when_supplied_with_argument() + { + $kernel = $this->mock(HydeKernel::class); + $kernel->shouldReceive('getBasePath')->andReturn('/path/to/project'); + $filesystem = new Filesystem($kernel); + $this->assertEquals('/path/to/project'.DIRECTORY_SEPARATOR.'foo/bar.php', $filesystem->path('foo/bar.php')); + } + public function test_path_method_returns_qualified_file_path_when_supplied_with_argument() { $this->assertEquals($this->filesystem->path('file.php'), $this->filesystem->path().DIRECTORY_SEPARATOR.'file.php'); From 51f485ac4cb49d76071a9980773b83e1b8ae887d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 21:08:15 +0200 Subject: [PATCH 26/55] Fix mismatched expected/actual values --- .../tests/Feature/Foundation/FilesystemTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 8654e09b0c9..3b0c687e2c2 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -56,24 +56,24 @@ public function test_path_method_returns_path_relative_to_base_path_when_supplie public function test_path_method_returns_qualified_file_path_when_supplied_with_argument() { - $this->assertEquals($this->filesystem->path('file.php'), $this->filesystem->path().DIRECTORY_SEPARATOR.'file.php'); + $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('file.php')); } public function test_path_method_strips_trailing_directory_separators_from_argument() { - $this->assertEquals($this->filesystem->path('\\/file.php/'), $this->filesystem->path().DIRECTORY_SEPARATOR.'file.php'); + $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('\\/file.php/')); } public function test_path_method_returns_expected_value_for_nested_path_arguments() { - $this->assertEquals($this->filesystem->path('directory/file.php'), $this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php'); + $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('directory/file.php')); } public function test_path_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument() { - $this->assertEquals($this->filesystem->path('directory/file.php/'), $this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php'); - $this->assertEquals($this->filesystem->path('/directory/file.php/'), $this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php'); - $this->assertEquals($this->filesystem->path('\\/directory/file.php/'), $this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php'); + $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('directory/file.php/')); + $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('/directory/file.php/')); + $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('\\/directory/file.php/')); } public function test_vendor_path() From 44066fd0b7ffad7deda354bae7ec5dd4b04030f9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 21:11:07 +0200 Subject: [PATCH 27/55] Clean up tests --- .../tests/Feature/Foundation/FilesystemTest.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 3b0c687e2c2..8504acd1915 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -59,21 +59,22 @@ public function test_path_method_returns_qualified_file_path_when_supplied_with_ $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('file.php')); } - public function test_path_method_strips_trailing_directory_separators_from_argument() + public function test_path_method_returns_expected_value_for_nested_path_arguments() { - $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('\\/file.php/')); + $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('directory/file.php')); } - public function test_path_method_returns_expected_value_for_nested_path_arguments() + public function test_path_method_strips_trailing_directory_separators_from_argument() { - $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('directory/file.php')); + $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('\\/file.php/')); } public function test_path_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument() { - $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('directory/file.php/')); - $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('/directory/file.php/')); - $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('\\/directory/file.php/')); + $expected = $this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php'; + $this->assertEquals($expected, $this->filesystem->path('directory/file.php/')); + $this->assertEquals($expected, $this->filesystem->path('/directory/file.php/')); + $this->assertEquals($expected, $this->filesystem->path('\\/directory/file.php/')); } public function test_vendor_path() From 160dffa8ff8c3b3056679a55e30b7e44482d2425 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 21:14:30 +0200 Subject: [PATCH 28/55] Move vendorPath method tests to new logic location --- .../Feature/Foundation/FilesystemTest.php | 30 ++++++++++++++++++- .../tests/Unit/HydeVendorPathHelperTest.php | 23 -------------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 8504acd1915..ad6e74face7 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -77,9 +77,37 @@ public function test_path_method_returns_expected_value_regardless_of_trailing_d $this->assertEquals($expected, $this->filesystem->path('\\/directory/file.php/')); } - public function test_vendor_path() + public function test_vendor_path_method_exists() { + $this->assertTrue(method_exists(Filesystem::class, 'vendorPath')); + } + + public function test_vendor_path_method_returns_string() + { + $this->assertIsString($this->filesystem->vendorPath()); + } + + public function test_vendor_path_method_returns_qualified_file_path_when_supplied_with_argument() + { + $this->assertEquals($this->filesystem->vendorPath('file.php'), $this->filesystem->vendorPath().'/file.php'); + } + + public function test_vendor_path_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument() + { + $this->assertEquals($this->filesystem->vendorPath('\\/file.php/'), $this->filesystem->vendorPath().'/file.php'); + + $this->assertEquals($this->filesystem->vendorPath('directory/file.php'), $this->filesystem->vendorPath().'/directory/file.php'); + $this->assertEquals($this->filesystem->vendorPath('directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); + $this->assertEquals($this->filesystem->vendorPath('/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); + $this->assertEquals($this->filesystem->vendorPath('\\/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); + + $this->assertEquals($this->filesystem->vendorPath('\\/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); + $this->assertEquals($this->filesystem->vendorPath('/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); + $this->assertEquals($this->filesystem->vendorPath('\\/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); + $this->assertEquals($this->filesystem->vendorPath('\\/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); + $this->assertEquals($this->filesystem->vendorPath('/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); + $this->assertEquals($this->filesystem->vendorPath('\\/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); } public function test_copy() diff --git a/packages/framework/tests/Unit/HydeVendorPathHelperTest.php b/packages/framework/tests/Unit/HydeVendorPathHelperTest.php index e724fd15499..9dae92fe979 100644 --- a/packages/framework/tests/Unit/HydeVendorPathHelperTest.php +++ b/packages/framework/tests/Unit/HydeVendorPathHelperTest.php @@ -29,27 +29,4 @@ public function test_method_returns_path_to_the_vendor_directory() $this->assertFileExists(Hyde::vendorPath().'/composer.json'); $this->assertStringContainsString('"name": "hyde/framework",', file_get_contents(Hyde::vendorPath().'/composer.json')); } - - public function test_method_returns_qualified_file_path_when_supplied_with_argument() - { - $this->assertEquals(Hyde::vendorPath('file.php'), Hyde::vendorPath().'/file.php'); - } - - public function test_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument() - { - $this->assertEquals(Hyde::vendorPath('\\/file.php/'), Hyde::vendorPath().'/file.php'); - - $this->assertEquals(Hyde::vendorPath('directory/file.php'), Hyde::vendorPath().'/directory/file.php'); - $this->assertEquals(Hyde::vendorPath('directory/file.php/'), Hyde::vendorPath().'/directory/file.php'); - $this->assertEquals(Hyde::vendorPath('/directory/file.php/'), Hyde::vendorPath().'/directory/file.php'); - $this->assertEquals(Hyde::vendorPath('\\/directory/file.php/'), Hyde::vendorPath().'/directory/file.php'); - - $this->assertEquals(Hyde::vendorPath('\\/directory/file.php/'), Hyde::vendorPath().'/directory/file.php'); - $this->assertEquals(Hyde::vendorPath('/directory/file.php/'), Hyde::vendorPath().'/directory/file.php'); - $this->assertEquals(Hyde::vendorPath('\\/directory/file.php/'), Hyde::vendorPath().'/directory/file.php'); - - $this->assertEquals(Hyde::vendorPath('\\/directory/file.php/'), Hyde::vendorPath().'/directory/file.php'); - $this->assertEquals(Hyde::vendorPath('/directory/file.php/'), Hyde::vendorPath().'/directory/file.php'); - $this->assertEquals(Hyde::vendorPath('\\/directory/file.php/'), Hyde::vendorPath().'/directory/file.php'); - } } From bc480d44eb018df89e9750c44075a12b2b9d8b7f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 21:17:26 +0200 Subject: [PATCH 29/55] Fix mismatched expected/actual values --- .../Feature/Foundation/FilesystemTest.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index ad6e74face7..16ec9d45a17 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -96,18 +96,18 @@ public function test_vendor_path_method_returns_expected_value_regardless_of_tra { $this->assertEquals($this->filesystem->vendorPath('\\/file.php/'), $this->filesystem->vendorPath().'/file.php'); - $this->assertEquals($this->filesystem->vendorPath('directory/file.php'), $this->filesystem->vendorPath().'/directory/file.php'); - $this->assertEquals($this->filesystem->vendorPath('directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); - $this->assertEquals($this->filesystem->vendorPath('/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); - $this->assertEquals($this->filesystem->vendorPath('\\/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); - - $this->assertEquals($this->filesystem->vendorPath('\\/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); - $this->assertEquals($this->filesystem->vendorPath('/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); - $this->assertEquals($this->filesystem->vendorPath('\\/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); - - $this->assertEquals($this->filesystem->vendorPath('\\/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); - $this->assertEquals($this->filesystem->vendorPath('/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); - $this->assertEquals($this->filesystem->vendorPath('\\/directory/file.php/'), $this->filesystem->vendorPath().'/directory/file.php'); + $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('directory/file.php')); + $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('directory/file.php/')); + $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('/directory/file.php/')); + $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('\\/directory/file.php/')); + + $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('\\/directory/file.php/')); + $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('/directory/file.php/')); + $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('\\/directory/file.php/')); + + $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('\\/directory/file.php/')); + $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('/directory/file.php/')); + $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('\\/directory/file.php/')); } public function test_copy() From ebf5ab489744fa6513045b677b343b1ab4bc5dd5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 21:28:46 +0200 Subject: [PATCH 30/55] Clean up test code and use mocks --- .../Feature/Foundation/FilesystemTest.php | 46 +++++-------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 16ec9d45a17..a245834a135 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -11,21 +11,21 @@ */ class FilesystemTest extends TestCase { + // Filesystem with mocked Kernel path. Use the real one if you actually need to access the filesystem. protected Filesystem $filesystem; protected function setUp(): void { parent::setUp(); - $this->filesystem = new Filesystem(new HydeKernel()); + $kernel = $this->mock(HydeKernel::class); + $kernel->shouldReceive('getBasePath')->andReturn('/foo'); + $this->filesystem = new Filesystem($kernel); } public function test_get_base_path_returns_kernels_base_path() { - $kernel = $this->mock(HydeKernel::class); - $kernel->shouldReceive('getBasePath')->andReturn('/path/to/project'); - $filesystem = new Filesystem($kernel); - $this->assertEquals('/path/to/project', $filesystem->getBasePath()); + $this->assertEquals('/foo', $this->filesystem->getBasePath()); } public function test_path_method_exists() @@ -40,41 +40,32 @@ public function test_path_method_returns_string() public function test_path_method_returns_base_path_when_not_supplied_with_argument() { - $kernel = $this->mock(HydeKernel::class); - $kernel->shouldReceive('getBasePath')->andReturn('/path/to/project'); - $filesystem = new Filesystem($kernel); - $this->assertEquals('/path/to/project', $filesystem->path()); + $this->assertEquals('/foo', $this->filesystem->path()); } public function test_path_method_returns_path_relative_to_base_path_when_supplied_with_argument() { - $kernel = $this->mock(HydeKernel::class); - $kernel->shouldReceive('getBasePath')->andReturn('/path/to/project'); - $filesystem = new Filesystem($kernel); - $this->assertEquals('/path/to/project'.DIRECTORY_SEPARATOR.'foo/bar.php', $filesystem->path('foo/bar.php')); + $this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'foo/bar.php', $this->filesystem->path('foo/bar.php')); } public function test_path_method_returns_qualified_file_path_when_supplied_with_argument() { - $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('file.php')); + $this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('file.php')); } public function test_path_method_returns_expected_value_for_nested_path_arguments() { - $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('directory/file.php')); + $this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'directory/file.php', $this->filesystem->path('directory/file.php')); } public function test_path_method_strips_trailing_directory_separators_from_argument() { - $this->assertEquals($this->filesystem->path().DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('\\/file.php/')); + $this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'file.php', $this->filesystem->path('\\/file.php/')); } public function test_path_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument() { - $expected = $this->filesystem->path().DIRECTORY_SEPARATOR.'directory/file.php'; - $this->assertEquals($expected, $this->filesystem->path('directory/file.php/')); - $this->assertEquals($expected, $this->filesystem->path('/directory/file.php/')); - $this->assertEquals($expected, $this->filesystem->path('\\/directory/file.php/')); + $this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'bar/file.php', $this->filesystem->path('\\/bar/file.php/')); } public function test_vendor_path_method_exists() @@ -94,20 +85,7 @@ public function test_vendor_path_method_returns_qualified_file_path_when_supplie public function test_vendor_path_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument() { - $this->assertEquals($this->filesystem->vendorPath('\\/file.php/'), $this->filesystem->vendorPath().'/file.php'); - - $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('directory/file.php')); - $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('directory/file.php/')); - $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('/directory/file.php/')); - $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('\\/directory/file.php/')); - - $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('\\/directory/file.php/')); - $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('/directory/file.php/')); - $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('\\/directory/file.php/')); - - $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('\\/directory/file.php/')); - $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('/directory/file.php/')); - $this->assertEquals($this->filesystem->vendorPath().'/directory/file.php', $this->filesystem->vendorPath('\\/directory/file.php/')); + $this->assertEquals('/foo' . DIRECTORY_SEPARATOR . 'vendor/hyde/framework/file.php', $this->filesystem->vendorPath('\\//file.php/')); } public function test_copy() From 0a9a7fb9ed9a98a8a29f86f934ccd44c663c47d9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 21:48:20 +0200 Subject: [PATCH 31/55] Add tests for Filesystem::copy method --- .../Feature/Foundation/FilesystemTest.php | 26 ++++++++++++++++++- .../FilesystemSafeCopyHelperTest.php} | 7 +++-- 2 files changed, 30 insertions(+), 3 deletions(-) rename packages/framework/tests/Unit/{HydeSafeCopyHelperTest.php => Foundation/FilesystemSafeCopyHelperTest.php} (94%) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index a245834a135..edb02151581 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -88,9 +88,33 @@ public function test_vendor_path_method_returns_expected_value_regardless_of_tra $this->assertEquals('/foo' . DIRECTORY_SEPARATOR . 'vendor/hyde/framework/file.php', $this->filesystem->vendorPath('\\//file.php/')); } - public function test_copy() + /** @see \Hyde\Framework\Testing\Unit\Foundation\FilesystemSafeCopyHelperTest for more exensive tests */ + public function test_copy_method() { + $this->assertTrue(method_exists(Filesystem::class, 'copy')); + } + + public function test_copy_method_returns_404_when_file_does_not_exist() + { + $this->assertEquals(404, $this->filesystem->copy('foo/bar.php', 'foo/baz.php')); + } + public function test_copy_method_returns_409_when_destination_file_exists() + { + touch('foo'); + touch('bar'); + $this->assertEquals(409, $this->filesystem->copy('foo', 'bar')); + unlink('foo'); + unlink('bar'); + } + + public function test_copy_method_overwrites_destination_file_when_overwrite_is_true() + { + touch('foo'); + touch('bar'); + $this->assertTrue($this->filesystem->copy('foo', 'bar', true)); + unlink('foo'); + unlink('bar'); } public function test_get_model_source_path() diff --git a/packages/framework/tests/Unit/HydeSafeCopyHelperTest.php b/packages/framework/tests/Unit/Foundation/FilesystemSafeCopyHelperTest.php similarity index 94% rename from packages/framework/tests/Unit/HydeSafeCopyHelperTest.php rename to packages/framework/tests/Unit/Foundation/FilesystemSafeCopyHelperTest.php index 49254852476..185025b2abc 100644 --- a/packages/framework/tests/Unit/HydeSafeCopyHelperTest.php +++ b/packages/framework/tests/Unit/Foundation/FilesystemSafeCopyHelperTest.php @@ -1,12 +1,15 @@ Date: Sat, 30 Jul 2022 21:56:17 +0200 Subject: [PATCH 32/55] Test moved fluent path helpers --- .../FluentFilesystemModelPathHelpersTest.php} | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) rename packages/framework/tests/Unit/{FluentPathHelpersTest.php => Foundation/FluentFilesystemModelPathHelpersTest.php} (97%) diff --git a/packages/framework/tests/Unit/FluentPathHelpersTest.php b/packages/framework/tests/Unit/Foundation/FluentFilesystemModelPathHelpersTest.php similarity index 97% rename from packages/framework/tests/Unit/FluentPathHelpersTest.php rename to packages/framework/tests/Unit/Foundation/FluentFilesystemModelPathHelpersTest.php index b0e71b7c95c..18fbcbf74c0 100644 --- a/packages/framework/tests/Unit/FluentPathHelpersTest.php +++ b/packages/framework/tests/Unit/Foundation/FluentFilesystemModelPathHelpersTest.php @@ -1,6 +1,6 @@ Date: Sat, 30 Jul 2022 21:58:31 +0200 Subject: [PATCH 33/55] Link to unit tests at top of file --- .../framework/tests/Feature/Foundation/FilesystemTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index edb02151581..b84c8b412af 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -8,6 +8,9 @@ /** * @covers \Hyde\Framework\Foundation\Filesystem + * + * @see \Hyde\Framework\Testing\Unit\Foundation\FilesystemSafeCopyHelperTest + * @see \Hyde\Framework\Testing\Unit\Foundation\FluentFilesystemModelPathHelpersTest */ class FilesystemTest extends TestCase { @@ -88,7 +91,6 @@ public function test_vendor_path_method_returns_expected_value_regardless_of_tra $this->assertEquals('/foo' . DIRECTORY_SEPARATOR . 'vendor/hyde/framework/file.php', $this->filesystem->vendorPath('\\//file.php/')); } - /** @see \Hyde\Framework\Testing\Unit\Foundation\FilesystemSafeCopyHelperTest for more exensive tests */ public function test_copy_method() { $this->assertTrue(method_exists(Filesystem::class, 'copy')); From 6cf0d651886277a78d7ac09e1344b8b9263bd21e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 21:59:58 +0200 Subject: [PATCH 34/55] Test covers both Filesystem and Kernel --- .../tests/Unit/Foundation/FilesystemSafeCopyHelperTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/framework/tests/Unit/Foundation/FilesystemSafeCopyHelperTest.php b/packages/framework/tests/Unit/Foundation/FilesystemSafeCopyHelperTest.php index 185025b2abc..efca94b9068 100644 --- a/packages/framework/tests/Unit/Foundation/FilesystemSafeCopyHelperTest.php +++ b/packages/framework/tests/Unit/Foundation/FilesystemSafeCopyHelperTest.php @@ -2,12 +2,14 @@ namespace Hyde\Framework\Testing\Unit\Foundation; +use Hyde\Framework\Foundation\Filesystem; use Hyde\Framework\Hyde; use Hyde\Framework\HydeKernel; use Hyde\Testing\TestCase; /** * @covers \Hyde\Framework\Foundation\Filesystem::copy + * @covers \Hyde\Framework\HydeKernel::copy */ class FilesystemSafeCopyHelperTest extends TestCase { @@ -26,6 +28,7 @@ public function setUp(): void public function test_copy_method_exists() { $this->assertTrue(method_exists(HydeKernel::class, 'copy')); + $this->assertTrue(method_exists(Filesystem::class, 'copy')); } public function test_copy_method_returns404_if_source_file_does_not_exist() From 6c84fd2824aba45d6c21121cbad0a6aea29dacb6 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 30 Jul 2022 20:00:25 +0000 Subject: [PATCH 35/55] Apply fixes from StyleCI --- .../tests/Feature/Foundation/FilesystemTest.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index b84c8b412af..c0a0476701f 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -2,9 +2,9 @@ namespace Hyde\Framework\Testing\Feature\Foundation; +use Hyde\Framework\Foundation\Filesystem; use Hyde\Framework\HydeKernel; use Hyde\Testing\TestCase; -use Hyde\Framework\Foundation\Filesystem; /** * @covers \Hyde\Framework\Foundation\Filesystem @@ -88,7 +88,7 @@ public function test_vendor_path_method_returns_qualified_file_path_when_supplie public function test_vendor_path_method_returns_expected_value_regardless_of_trailing_directory_separators_in_argument() { - $this->assertEquals('/foo' . DIRECTORY_SEPARATOR . 'vendor/hyde/framework/file.php', $this->filesystem->vendorPath('\\//file.php/')); + $this->assertEquals('/foo'.DIRECTORY_SEPARATOR.'vendor/hyde/framework/file.php', $this->filesystem->vendorPath('\\//file.php/')); } public function test_copy_method() @@ -121,36 +121,29 @@ public function test_copy_method_overwrites_destination_file_when_overwrite_is_t public function test_get_model_source_path() { - } public function test_get_blade_page_path() { - } public function test_get_markdown_page_path() { - } public function test_get_markdown_post_path() { - } public function test_get_documentation_page_path() { - } public function test_get_site_output_path() { - } public function test_path_to_relative() { - } } From 1abad2c2018d7271d4ba5e5f1a517ec357abf6d7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 22:07:49 +0200 Subject: [PATCH 36/55] Remove test templates handled in unit test --- .../Feature/Foundation/FilesystemTest.php | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index c0a0476701f..595dcb0cd72 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -118,32 +118,4 @@ public function test_copy_method_overwrites_destination_file_when_overwrite_is_t unlink('foo'); unlink('bar'); } - - public function test_get_model_source_path() - { - } - - public function test_get_blade_page_path() - { - } - - public function test_get_markdown_page_path() - { - } - - public function test_get_markdown_post_path() - { - } - - public function test_get_documentation_page_path() - { - } - - public function test_get_site_output_path() - { - } - - public function test_path_to_relative() - { - } } From 91a72cb9a9ea1f4c982f9d3325abfe55e47e6619 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 22:08:14 +0200 Subject: [PATCH 37/55] Add todo --- .../Unit/Foundation/FluentFilesystemModelPathHelpersTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/tests/Unit/Foundation/FluentFilesystemModelPathHelpersTest.php b/packages/framework/tests/Unit/Foundation/FluentFilesystemModelPathHelpersTest.php index 18fbcbf74c0..b1efe9b3b98 100644 --- a/packages/framework/tests/Unit/Foundation/FluentFilesystemModelPathHelpersTest.php +++ b/packages/framework/tests/Unit/Foundation/FluentFilesystemModelPathHelpersTest.php @@ -12,6 +12,8 @@ /** * @covers \Hyde\Framework\HydeKernel * @covers \Hyde\Framework\Foundation\Filesystem + * + * @todo Merge into FilesystemTest */ class FluentFilesystemModelPathHelpersTest extends TestCase { From 6d6d1677051153157c7f3a0bd6e7e4de2b40793d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 22:08:49 +0200 Subject: [PATCH 38/55] Create HyperlinksTest.php --- packages/framework/src/Foundation/Hyperlinks.php | 2 ++ .../tests/Feature/Foundation/HyperlinksTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 packages/framework/tests/Feature/Foundation/HyperlinksTest.php diff --git a/packages/framework/src/Foundation/Hyperlinks.php b/packages/framework/src/Foundation/Hyperlinks.php index 0a514f35fb2..e044dc62635 100644 --- a/packages/framework/src/Foundation/Hyperlinks.php +++ b/packages/framework/src/Foundation/Hyperlinks.php @@ -10,6 +10,8 @@ * Contains helpers and logic for resolving web paths for compiled files. * * It's bound to the HydeKernel instance, and is an integral part of the framework. + * + * @see \Hyde\Framework\Testing\Feature\Foundation\HyperlinksTest */ class Hyperlinks { diff --git a/packages/framework/tests/Feature/Foundation/HyperlinksTest.php b/packages/framework/tests/Feature/Foundation/HyperlinksTest.php new file mode 100644 index 00000000000..ddeb6022ad8 --- /dev/null +++ b/packages/framework/tests/Feature/Foundation/HyperlinksTest.php @@ -0,0 +1,15 @@ + Date: Sat, 30 Jul 2022 22:12:06 +0200 Subject: [PATCH 39/55] Move unit test to foundation namespace --- packages/framework/src/Foundation/Hyperlinks.php | 2 +- .../HyperlinkFormatHtmlPathTest.php} | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) rename packages/framework/tests/Unit/{FileHelperPageLinkPrettyUrlTest.php => Foundation/HyperlinkFormatHtmlPathTest.php} (94%) diff --git a/packages/framework/src/Foundation/Hyperlinks.php b/packages/framework/src/Foundation/Hyperlinks.php index e044dc62635..ae9e7a57d4b 100644 --- a/packages/framework/src/Foundation/Hyperlinks.php +++ b/packages/framework/src/Foundation/Hyperlinks.php @@ -25,7 +25,7 @@ public function __construct(HydeKernel $kernel) /** * Format a link to an HTML file, allowing for pretty URLs, if enabled. * - * @see \Hyde\Framework\Testing\Unit\FileHelperPageLinkPrettyUrlTest + * @see \Hyde\Framework\Testing\Unit\Foundation\HyperlinkFormatHtmlPathTest */ public function formatHtmlPath(string $destination): string { diff --git a/packages/framework/tests/Unit/FileHelperPageLinkPrettyUrlTest.php b/packages/framework/tests/Unit/Foundation/HyperlinkFormatHtmlPathTest.php similarity index 94% rename from packages/framework/tests/Unit/FileHelperPageLinkPrettyUrlTest.php rename to packages/framework/tests/Unit/Foundation/HyperlinkFormatHtmlPathTest.php index 92bd875d5fa..2b581107144 100644 --- a/packages/framework/tests/Unit/FileHelperPageLinkPrettyUrlTest.php +++ b/packages/framework/tests/Unit/Foundation/HyperlinkFormatHtmlPathTest.php @@ -1,16 +1,14 @@ Date: Sat, 30 Jul 2022 22:14:24 +0200 Subject: [PATCH 40/55] Move unit test to foundation namespace --- packages/framework/src/Foundation/Hyperlinks.php | 2 +- .../HyperlinkFileHelperRelativeLinkTest.php} | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) rename packages/framework/tests/Unit/{FileHelperRelativeLinkTest.php => Foundation/HyperlinkFileHelperRelativeLinkTest.php} (96%) diff --git a/packages/framework/src/Foundation/Hyperlinks.php b/packages/framework/src/Foundation/Hyperlinks.php index ae9e7a57d4b..f2ba47b2f51 100644 --- a/packages/framework/src/Foundation/Hyperlinks.php +++ b/packages/framework/src/Foundation/Hyperlinks.php @@ -51,7 +51,7 @@ public function formatHtmlPath(string $destination): string * @param string $destination relative to output directory on compiled site * @return string * - * @see \Hyde\Framework\Testing\Unit\FileHelperRelativeLinkTest + * @see \Hyde\Framework\Testing\Unit\Foundation\HyperlinkFileHelperRelativeLinkTest */ public function relativeLink(string $destination): string { diff --git a/packages/framework/tests/Unit/FileHelperRelativeLinkTest.php b/packages/framework/tests/Unit/Foundation/HyperlinkFileHelperRelativeLinkTest.php similarity index 96% rename from packages/framework/tests/Unit/FileHelperRelativeLinkTest.php rename to packages/framework/tests/Unit/Foundation/HyperlinkFileHelperRelativeLinkTest.php index 2130d707465..ed0e68a9ad7 100644 --- a/packages/framework/tests/Unit/FileHelperRelativeLinkTest.php +++ b/packages/framework/tests/Unit/Foundation/HyperlinkFileHelperRelativeLinkTest.php @@ -1,16 +1,14 @@ Date: Sat, 30 Jul 2022 22:19:01 +0200 Subject: [PATCH 41/55] Set up Hyperlinks instance in setUp method --- .../tests/Feature/Foundation/HyperlinksTest.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Foundation/HyperlinksTest.php b/packages/framework/tests/Feature/Foundation/HyperlinksTest.php index ddeb6022ad8..b218a73343f 100644 --- a/packages/framework/tests/Feature/Foundation/HyperlinksTest.php +++ b/packages/framework/tests/Feature/Foundation/HyperlinksTest.php @@ -11,5 +11,12 @@ */ class HyperlinksTest extends TestCase { - // + protected Hyperlinks $class; + + protected function setUp(): void + { + parent::setUp(); + + $this->class = new Hyperlinks(HydeKernel::getInstance()); + } } From 0f5fa909ed26bc413613db1ed09ed152ac36dfa8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 22:20:58 +0200 Subject: [PATCH 42/55] Merge FileHelpersImageTest into new feature test --- .../Feature/Foundation/HyperlinksTest.php | 29 +++++++++++++ .../tests/Unit/FileHelpersImageTest.php | 41 ------------------- 2 files changed, 29 insertions(+), 41 deletions(-) delete mode 100644 packages/framework/tests/Unit/FileHelpersImageTest.php diff --git a/packages/framework/tests/Feature/Foundation/HyperlinksTest.php b/packages/framework/tests/Feature/Foundation/HyperlinksTest.php index b218a73343f..3491593877e 100644 --- a/packages/framework/tests/Feature/Foundation/HyperlinksTest.php +++ b/packages/framework/tests/Feature/Foundation/HyperlinksTest.php @@ -19,4 +19,33 @@ protected function setUp(): void $this->class = new Hyperlinks(HydeKernel::getInstance()); } + + public function test_image_helper_gets_relative_web_link_to_image_stored_in_site_media_folder() + { + $tests = [ + 'test.jpg' => 'media/test.jpg', + 'foo' => 'media/foo', + 'http://example.com/test.jpg' => 'http://example.com/test.jpg', + 'https://example.com/test.jpg' => 'https://example.com/test.jpg', + ]; + + foreach ($tests as $input => $expected) { + $this->assertEquals($this->class->image($input), $expected); + } + } + + public function test_image_helper_resolves_paths_for_nested_pages() + { + $tests = [ + 'test.jpg' => '../media/test.jpg', + 'foo' => '../media/foo', + 'http://example.com/test.jpg' => 'http://example.com/test.jpg', + 'https://example.com/test.jpg' => 'https://example.com/test.jpg', + ]; + + foreach ($tests as $input => $expected) { + $this->mockCurrentPage('foo/bar'); + $this->assertEquals($this->class->image($input), $expected); + } + } } diff --git a/packages/framework/tests/Unit/FileHelpersImageTest.php b/packages/framework/tests/Unit/FileHelpersImageTest.php deleted file mode 100644 index 0a04ee194eb..00000000000 --- a/packages/framework/tests/Unit/FileHelpersImageTest.php +++ /dev/null @@ -1,41 +0,0 @@ - 'media/test.jpg', - 'foo' => 'media/foo', - 'http://example.com/test.jpg' => 'http://example.com/test.jpg', - 'https://example.com/test.jpg' => 'https://example.com/test.jpg', - ]; - - foreach ($tests as $input => $expected) { - $this->assertEquals(Hyde::image($input), $expected); - } - } - - public function test_image_helper_resolves_paths_for_nested_pages() - { - $tests = [ - 'test.jpg' => '../media/test.jpg', - 'foo' => '../media/foo', - 'http://example.com/test.jpg' => 'http://example.com/test.jpg', - 'https://example.com/test.jpg' => 'https://example.com/test.jpg', - ]; - - foreach ($tests as $input => $expected) { - $this->mockCurrentPage('foo/bar'); - $this->assertEquals(Hyde::image($input), $expected); - } - } -} From eb0c9f866c01a94a15e84ae75ea17b2e135d572a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 22:22:12 +0200 Subject: [PATCH 43/55] Remove deprecated Hyde::uriPath() helper --- RELEASE_NOTES.md | 2 +- .../framework/src/Foundation/Hyperlinks.php | 17 -------------- .../tests/Unit/HydeUriPathHelperTest.php | 22 ------------------- 3 files changed, 1 insertion(+), 40 deletions(-) delete mode 100644 packages/framework/tests/Unit/HydeUriPathHelperTest.php diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index e3d1b60364c..0a6261ffe1b 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -19,7 +19,7 @@ This serves two purposes: - for soon-to-be removed features. ### Removed -- for now removed features. +- Remove deprecated Hyde::uriPath() helper ### Fixed - for any bug fixes. diff --git a/packages/framework/src/Foundation/Hyperlinks.php b/packages/framework/src/Foundation/Hyperlinks.php index f2ba47b2f51..35bb44527f9 100644 --- a/packages/framework/src/Foundation/Hyperlinks.php +++ b/packages/framework/src/Foundation/Hyperlinks.php @@ -81,23 +81,6 @@ public function image(string $name): string return $this->relativeLink('media/'.basename($name)); } - /** - * Return a qualified URI path, if SITE_URL is set in .env, else return false. - * - * @deprecated v0.53.0-beta - Use Hyde::url() or Hyde::hasSiteUrl() instead. - * - * @param string $path optional relative path suffix. Omit to return base url. - * @return string|false - */ - public function uriPath(string $path = ''): string|false - { - if (config('site.url', false)) { - return rtrim(config('site.url'), '/').'/'.(trim($path, '/') ?? ''); - } - - return false; - } - /** * Check if a site base URL has been set in config (or .env). */ diff --git a/packages/framework/tests/Unit/HydeUriPathHelperTest.php b/packages/framework/tests/Unit/HydeUriPathHelperTest.php deleted file mode 100644 index 2479329ad11..00000000000 --- a/packages/framework/tests/Unit/HydeUriPathHelperTest.php +++ /dev/null @@ -1,22 +0,0 @@ -assertFalse(Hyde::uriPath()); - } - - public function test_helper_returns_expected_string_when_site_url_is_set() - { - \Illuminate\Support\Facades\Config::set('site.url', 'https://example.com'); - $this->assertEquals('https://example.com/foo/bar.html', Hyde::uriPath('foo/bar.html')); - } -} From 9168517cce9b96d4178c70dd839501608eea808b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 22:24:16 +0200 Subject: [PATCH 44/55] Move unit test to foundation namespace --- .../HyperlinksUrlPathHelpersTest.php} | 53 +++++++++++-------- 1 file changed, 32 insertions(+), 21 deletions(-) rename packages/framework/tests/Unit/{HydeUrlPathHelpersTest.php => Foundation/HyperlinksUrlPathHelpersTest.php} (64%) diff --git a/packages/framework/tests/Unit/HydeUrlPathHelpersTest.php b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php similarity index 64% rename from packages/framework/tests/Unit/HydeUrlPathHelpersTest.php rename to packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php index 0ce6445fcfd..eac17ac3dda 100644 --- a/packages/framework/tests/Unit/HydeUrlPathHelpersTest.php +++ b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php @@ -1,72 +1,83 @@ class = new Hyperlinks(HydeKernel::getInstance()); + } + public function test_has_site_url_returns_false_when_no_site_url_is_set() { config(['site.url' => null]); - $this->assertFalse(Hyde::hasSiteUrl()); + $this->assertFalse($this->class->hasSiteUrl()); } public function test_has_site_url_returns_true_when_site_url_is_set() { config(['site.url' => 'https://example.com']); - $this->assertTrue(Hyde::hasSiteUrl()); + $this->assertTrue($this->class->hasSiteUrl()); } // test that url returns the site url when no path is given public function test_qualified_url_returns_site_url_when_no_path_is_given() { config(['site.url' => 'https://example.com']); - $this->assertEquals('https://example.com', Hyde::url()); + $this->assertEquals('https://example.com', $this->class->url()); } // test that url returns the site url plus the given path public function test_qualified_url_returns_site_url_plus_given_path() { config(['site.url' => 'https://example.com']); - $this->assertEquals('https://example.com/path', Hyde::url('path')); + $this->assertEquals('https://example.com/path', $this->class->url('path')); } // test that url returns the site url plus the given path with extension public function test_qualified_url_returns_site_url_plus_given_path_with_extension() { config(['site.url' => 'https://example.com']); - $this->assertEquals('https://example.com/path.html', Hyde::url('path.html')); + $this->assertEquals('https://example.com/path.html', $this->class->url('path.html')); } // test that url returns the site url plus the given path with extension and query string public function test_qualified_url_returns_site_url_plus_given_path_with_extension_and_query_string() { config(['site.url' => 'https://example.com']); - $this->assertEquals('https://example.com/path.html?query=string', Hyde::url('path.html?query=string')); + $this->assertEquals('https://example.com/path.html?query=string', $this->class->url('path.html?query=string')); } // test that url trims trailing slashes public function test_qualified_url_trims_trailing_slashes() { config(['site.url' => 'https://example.com/']); - $this->assertEquals('https://example.com', Hyde::url()); - $this->assertEquals('https://example.com', Hyde::url('/')); - $this->assertEquals('https://example.com/foo', Hyde::url('/foo/')); + $this->assertEquals('https://example.com', $this->class->url()); + $this->assertEquals('https://example.com', $this->class->url('/')); + $this->assertEquals('https://example.com/foo', $this->class->url('/foo/')); } // test that url accepts multiple schemes public function test_qualified_url_accepts_multiple_schemes() { config(['site.url' => 'http://example.com']); - $this->assertEquals('http://example.com', Hyde::url()); + $this->assertEquals('http://example.com', $this->class->url()); } // test that url throws an exception when no site url is set @@ -75,35 +86,35 @@ public function test_qualified_url_throws_exception_when_no_site_url_is_set() config(['site.url' => null]); $this->expectException(BaseUrlNotSetException::class); $this->expectExceptionMessage('No site URL has been set in config (or .env).'); - Hyde::url(); + $this->class->url(); } // test that url uses default parameter when supplied and no site url is set public function test_qualified_url_uses_default_parameter_when_no_site_url_is_set() { config(['site.url' => null]); - $this->assertEquals('bar/foo', Hyde::url('foo', 'bar')); + $this->assertEquals('bar/foo', $this->class->url('foo', 'bar')); } // test that url does not use default parameter when supplied and a site url is set public function test_qualified_url_does_not_use_default_parameter_when_site_url_is_set() { config(['site.url' => 'https://example.com']); - $this->assertEquals('https://example.com/foo', Hyde::url('foo', 'bar')); + $this->assertEquals('https://example.com/foo', $this->class->url('foo', 'bar')); } public function test_helper_returns_expected_string_when_site_url_is_set() { config(['site.url' => 'https://example.com']); - $this->assertEquals('https://example.com/foo/bar.html', Hyde::url('foo/bar.html')); + $this->assertEquals('https://example.com/foo/bar.html', $this->class->url('foo/bar.html')); } // test returned url uses pretty urls when enabled public function test_helper_returns_expected_string_when_pretty_urls_are_enabled() { config(['site.url' => 'https://example.com', 'site.pretty_urls' => true]); - $this->assertEquals('https://example.com', Hyde::url('index.html')); - $this->assertEquals('https://example.com/foo', Hyde::url('foo.html')); - $this->assertEquals('https://example.com/docs', Hyde::url('docs/index.html')); + $this->assertEquals('https://example.com', $this->class->url('index.html')); + $this->assertEquals('https://example.com/foo', $this->class->url('foo.html')); + $this->assertEquals('https://example.com/docs', $this->class->url('docs/index.html')); } } From 0f0161de4b9a1cfffe88d432217ffbc41759d609 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 30 Jul 2022 20:24:29 +0000 Subject: [PATCH 45/55] Apply fixes from StyleCI --- packages/framework/tests/Feature/Foundation/HyperlinksTest.php | 2 +- .../tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/HyperlinksTest.php b/packages/framework/tests/Feature/Foundation/HyperlinksTest.php index 3491593877e..2756c5bc85f 100644 --- a/packages/framework/tests/Feature/Foundation/HyperlinksTest.php +++ b/packages/framework/tests/Feature/Foundation/HyperlinksTest.php @@ -20,7 +20,7 @@ protected function setUp(): void $this->class = new Hyperlinks(HydeKernel::getInstance()); } - public function test_image_helper_gets_relative_web_link_to_image_stored_in_site_media_folder() + public function test_image_helper_gets_relative_web_link_to_image_stored_in_site_media_folder() { $tests = [ 'test.jpg' => 'media/test.jpg', diff --git a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php index eac17ac3dda..3e02e676c70 100644 --- a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php +++ b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php @@ -4,7 +4,6 @@ use Hyde\Framework\Exceptions\BaseUrlNotSetException; use Hyde\Framework\Foundation\Hyperlinks; -use Hyde\Framework\Hyde; use Hyde\Framework\HydeKernel; use Hyde\Testing\TestCase; From 8037971e63b5112312757795e67ba9ca80e07bf9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 22:33:34 +0200 Subject: [PATCH 46/55] Replace mock with actual instance --- .../Feature/Foundation/FilesystemTest.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 595dcb0cd72..a713929ee7e 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -3,7 +3,7 @@ namespace Hyde\Framework\Testing\Feature\Foundation; use Hyde\Framework\Foundation\Filesystem; -use Hyde\Framework\HydeKernel; +use Hyde\Framework\Hyde; use Hyde\Testing\TestCase; /** @@ -14,16 +14,24 @@ */ class FilesystemTest extends TestCase { - // Filesystem with mocked Kernel path. Use the real one if you actually need to access the filesystem. + protected string $originalBasePath; + protected Filesystem $filesystem; protected function setUp(): void { parent::setUp(); - $kernel = $this->mock(HydeKernel::class); - $kernel->shouldReceive('getBasePath')->andReturn('/foo'); - $this->filesystem = new Filesystem($kernel); + $this->originalBasePath = Hyde::getBasePath(); + $this->filesystem = new Filesystem(Hyde::getInstance()); + Hyde::getInstance()->setBasePath('/foo'); + } + + protected function tearDown(): void + { + Hyde::getInstance()->setBasePath($this->originalBasePath); + + parent::tearDown(); } public function test_get_base_path_returns_kernels_base_path() From cb22a15958b2edb7ae28ce07ae5c97fa7643a8a7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 22:42:25 +0200 Subject: [PATCH 47/55] Remove deprecated Hyde::uriPath() helper --- packages/framework/src/HydeKernel.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index cb7ef11e637..f4125e1b004 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -104,11 +104,6 @@ public function image(string $name): string return $this->hyperlinks->image($name); } - public function uriPath(string $path = ''): string|false - { - return $this->hyperlinks->uriPath($path); - } - public function hasSiteUrl(): bool { return $this->hyperlinks->hasSiteUrl(); From e91a443cd630d27ccc60f4afb6ea1ef0c0a14047 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 22:45:20 +0200 Subject: [PATCH 48/55] Clarify link annotation to facade class --- packages/framework/src/HydeKernel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/HydeKernel.php b/packages/framework/src/HydeKernel.php index f4125e1b004..4659452e81e 100644 --- a/packages/framework/src/HydeKernel.php +++ b/packages/framework/src/HydeKernel.php @@ -15,7 +15,7 @@ /** * Encapsulates a HydePHP project, providing helpful methods for interacting with it. * - * @see \Hyde\Framework\Hyde + * @see \Hyde\Framework\Hyde for the facade commonly used to access this class. * * @author Caen De Silva * @copyright 2022 Caen De Silva From d128d55c0e6168419afae0c1229187dc3d54d275 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 30 Jul 2022 22:46:45 +0200 Subject: [PATCH 49/55] Add test description --- packages/framework/tests/Feature/HydeKernelTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/framework/tests/Feature/HydeKernelTest.php b/packages/framework/tests/Feature/HydeKernelTest.php index 8f72ba4c65d..61ac56eed12 100644 --- a/packages/framework/tests/Feature/HydeKernelTest.php +++ b/packages/framework/tests/Feature/HydeKernelTest.php @@ -8,6 +8,9 @@ use Hyde\Testing\TestCase; /** + * This test class runs high-level tests on the HydeKernel class, + * as most of the logic actually resides in linked service classes. + * * @covers \Hyde\Framework\HydeKernel */ class HydeKernelTest extends TestCase From c2ec349defbfdae3fb1ea4c033f1405307940ef2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 31 Jul 2022 11:34:37 +0200 Subject: [PATCH 50/55] Add base method tests --- .../tests/Feature/HydeKernelTest.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/framework/tests/Feature/HydeKernelTest.php b/packages/framework/tests/Feature/HydeKernelTest.php index 61ac56eed12..2fd83fe7d10 100644 --- a/packages/framework/tests/Feature/HydeKernelTest.php +++ b/packages/framework/tests/Feature/HydeKernelTest.php @@ -34,4 +34,35 @@ public function test_kernel_singleton_can_be_accessed_by_helper_function() { $this->assertSame(app(HydeKernelContract::class), hyde()); } + + public function test_features_helper_returns_new_features_instance() + { + $this->assertInstanceOf(Features::class, Hyde::features()); + } + + public function test_has_feature_helper_calls_method_on_features_class() + { + $this->assertEquals(Features::enabled('foo'), Hyde::hasFeature('foo')); + } + + public function test_current_page_helper_returns_current_page_name() + { + View::share('currentPage', 'foo'); + $this->assertEquals('foo', Hyde::currentPage()); + } + + public function test_current_route_helper_returns_current_route_object() + { + $expected = new Route(new MarkdownPage()); + View::share('currentRoute', $expected); + $this->assertInstanceOf(RouteContract::class, Hyde::currentRoute()); + $this->assertEquals($expected, Hyde::currentRoute()); + $this->assertSame($expected, Hyde::currentRoute()); + } + + public function test_make_title_helper_returns_title_from_page_slug() + { + $this->assertEquals('Foo Bar', Hyde::makeTitle('foo-bar')); + } + } From 66060fcd76658640765f52c2e7287481abb4cb3d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 31 Jul 2022 11:34:57 +0200 Subject: [PATCH 51/55] Add Hyperlinks tests --- .../tests/Feature/HydeKernelTest.php | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/packages/framework/tests/Feature/HydeKernelTest.php b/packages/framework/tests/Feature/HydeKernelTest.php index 2fd83fe7d10..25d77df095d 100644 --- a/packages/framework/tests/Feature/HydeKernelTest.php +++ b/packages/framework/tests/Feature/HydeKernelTest.php @@ -3,15 +3,23 @@ namespace Hyde\Framework\Testing\Feature; use Hyde\Framework\Contracts\HydeKernelContract; +use Hyde\Framework\Contracts\RouteContract; +use Hyde\Framework\Helpers\Features; use Hyde\Framework\Hyde; use Hyde\Framework\HydeKernel; +use Hyde\Framework\Models\Pages\MarkdownPage; +use Hyde\Framework\Models\Route; use Hyde\Testing\TestCase; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\View; /** * This test class runs high-level tests on the HydeKernel class, * as most of the logic actually resides in linked service classes. * * @covers \Hyde\Framework\HydeKernel + * + * @see \Hyde\Framework\Testing\Unit\HydeHelperFacadeMakeTitleTest */ class HydeKernelTest extends TestCase { @@ -65,4 +73,58 @@ public function test_make_title_helper_returns_title_from_page_slug() $this->assertEquals('Foo Bar', Hyde::makeTitle('foo-bar')); } + public function test_format_html_path_helper_formats_path_according_to_config_rules() + { + Config::set('site.pretty_urls', false); + $this->assertEquals('foo.html', Hyde::formatHtmlPath('foo.html')); + $this->assertEquals('index.html', Hyde::formatHtmlPath('index.html')); + + Config::set('site.pretty_urls', true); + $this->assertEquals('foo', Hyde::formatHtmlPath('foo.html')); + $this->assertEquals('/', Hyde::formatHtmlPath('index.html')); + } + + public function test_relative_link_helper_returns_relative_link_to_destination() + { + View::share('currentPage', 'bar'); + $this->assertEquals('foo', Hyde::relativeLink('foo')); + + View::share('currentPage', 'foo/bar'); + $this->assertEquals('../foo', Hyde::relativeLink('foo')); + } + + public function test_image_helper_returns_image_path_for_given_name() + { + View::share('currentPage', 'foo'); + $this->assertEquals('media/foo.jpg', Hyde::image('foo.jpg')); + $this->assertEquals('https://example.com/foo.jpg', Hyde::image('https://example.com/foo.jpg')); + + View::share('currentPage', 'foo/bar'); + $this->assertEquals('../media/foo.jpg', Hyde::image('foo.jpg')); + $this->assertEquals('https://example.com/foo.jpg', Hyde::image('https://example.com/foo.jpg')); + } + + public function test_has_site_url_helper_returns_boolean_value_for_when_config_setting_is_set() + { + Config::set('site.url', 'https://example.com'); + $this->assertTrue(Hyde::hasSiteUrl()); + + Config::set('site.url', null); + $this->assertFalse(Hyde::hasSiteUrl()); + } + + public function test_url_returns_qualified_url_paths() + { + Config::set('site.url', 'https://example.com'); + $this->assertEquals('https://example.com', Hyde::url()); + $this->assertEquals('https://example.com/foo', Hyde::url('foo')); + + Config::set('site.pretty_urls', false); + $this->assertEquals('https://example.com/foo.html', Hyde::url('foo.html')); + $this->assertEquals('https://example.com/index.html', Hyde::url('index.html')); + + Config::set('site.pretty_urls', true); + $this->assertEquals('https://example.com/foo', Hyde::url('foo.html')); + $this->assertEquals('https://example.com', Hyde::url('index.html')); + } } From 7f3e67497c244521b8b9e07eba9b0835ef917f53 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 31 Jul 2022 11:58:33 +0200 Subject: [PATCH 52/55] Add Filesystem tests --- .../tests/Feature/HydeKernelTest.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/packages/framework/tests/Feature/HydeKernelTest.php b/packages/framework/tests/Feature/HydeKernelTest.php index 25d77df095d..08041914ac8 100644 --- a/packages/framework/tests/Feature/HydeKernelTest.php +++ b/packages/framework/tests/Feature/HydeKernelTest.php @@ -7,7 +7,10 @@ use Hyde\Framework\Helpers\Features; use Hyde\Framework\Hyde; use Hyde\Framework\HydeKernel; +use Hyde\Framework\Models\Pages\BladePage; +use Hyde\Framework\Models\Pages\DocumentationPage; use Hyde\Framework\Models\Pages\MarkdownPage; +use Hyde\Framework\Models\Pages\MarkdownPost; use Hyde\Framework\Models\Route; use Hyde\Testing\TestCase; use Illuminate\Support\Facades\Config; @@ -127,4 +130,44 @@ public function test_url_returns_qualified_url_paths() $this->assertEquals('https://example.com/foo', Hyde::url('foo.html')); $this->assertEquals('https://example.com', Hyde::url('index.html')); } + + public function test_path_returns_qualified_path_for_given_path() + { + $this->assertEquals(Hyde::getBasePath(), Hyde::path()); + $this->assertEquals(Hyde::getBasePath().DIRECTORY_SEPARATOR.'foo', Hyde::path('foo')); + } + + public function test_vendor_path_returns_qualified_path_for_given_path() + { + $this->assertEquals(Hyde::getBasePath().DIRECTORY_SEPARATOR.'vendor/hyde/framework', Hyde::vendorPath()); + $this->assertEquals(Hyde::getBasePath().DIRECTORY_SEPARATOR.'vendor/hyde/framework/foo', Hyde::vendorPath('foo')); + } + + public function test_copy_helper_copies_file_from_given_path_to_given_path() + { + touch('foo'); + $this->assertTrue(Hyde::copy('foo', 'bar')); + $this->assertFileExists('bar'); + unlink('foo'); + unlink('bar'); + } + + public function test_fluent_model_source_path_helpers() + { + $this->assertEquals(Hyde::path('_posts'), Hyde::getModelSourcePath(MarkdownPost::class)); + $this->assertEquals(Hyde::path('_pages'), Hyde::getModelSourcePath(MarkdownPage::class)); + $this->assertEquals(Hyde::path('_docs'), Hyde::getModelSourcePath(DocumentationPage::class)); + $this->assertEquals(Hyde::path('_pages'), Hyde::getModelSourcePath(BladePage::class)); + + $this->assertEquals( Hyde::path('_pages'), Hyde::getBladePagePath()); + $this->assertEquals( Hyde::path('_pages'), Hyde::getMarkdownPagePath()); + $this->assertEquals( Hyde::path('_posts'), Hyde::getMarkdownPostPath()); + $this->assertEquals( Hyde::path('_docs'), Hyde::getDocumentationPagePath()); + $this->assertEquals( Hyde::path('_site'), Hyde::getSiteOutputPath()); + } + + public function test_path_to_relative_helper_returns_relative_path_for_given_path() + { + $this->assertEquals('foo', Hyde::pathToRelative(Hyde::path('foo'))); + } } From cea6cac2be08f3f3cdde8712f7c5b063ac8c4f0a Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 31 Jul 2022 09:58:45 +0000 Subject: [PATCH 53/55] Apply fixes from StyleCI --- packages/framework/tests/Feature/HydeKernelTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/framework/tests/Feature/HydeKernelTest.php b/packages/framework/tests/Feature/HydeKernelTest.php index 08041914ac8..848fea06eac 100644 --- a/packages/framework/tests/Feature/HydeKernelTest.php +++ b/packages/framework/tests/Feature/HydeKernelTest.php @@ -159,11 +159,11 @@ public function test_fluent_model_source_path_helpers() $this->assertEquals(Hyde::path('_docs'), Hyde::getModelSourcePath(DocumentationPage::class)); $this->assertEquals(Hyde::path('_pages'), Hyde::getModelSourcePath(BladePage::class)); - $this->assertEquals( Hyde::path('_pages'), Hyde::getBladePagePath()); - $this->assertEquals( Hyde::path('_pages'), Hyde::getMarkdownPagePath()); - $this->assertEquals( Hyde::path('_posts'), Hyde::getMarkdownPostPath()); - $this->assertEquals( Hyde::path('_docs'), Hyde::getDocumentationPagePath()); - $this->assertEquals( Hyde::path('_site'), Hyde::getSiteOutputPath()); + $this->assertEquals(Hyde::path('_pages'), Hyde::getBladePagePath()); + $this->assertEquals(Hyde::path('_pages'), Hyde::getMarkdownPagePath()); + $this->assertEquals(Hyde::path('_posts'), Hyde::getMarkdownPostPath()); + $this->assertEquals(Hyde::path('_docs'), Hyde::getDocumentationPagePath()); + $this->assertEquals(Hyde::path('_site'), Hyde::getSiteOutputPath()); } public function test_path_to_relative_helper_returns_relative_path_for_given_path() From 086df1e18cdd890070fde4802caafa4becf3538e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 31 Jul 2022 12:12:11 +0200 Subject: [PATCH 54/55] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 0a6261ffe1b..86730c6dc57 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -13,7 +13,7 @@ This serves two purposes: - for new features. ### Changed -- for changes in existing functionality. +- internal: The HydeKernel has been refactored to move related logic to service classes. This does not change the end usage as the Hyde facade still works the same ### Deprecated - for soon-to-be removed features. From 32ae9e0325030c6c2269531a16975926e561d2c9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 31 Jul 2022 12:16:30 +0200 Subject: [PATCH 55/55] Update method annotations --- packages/framework/src/Hyde.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Hyde.php b/packages/framework/src/Hyde.php index 803371a7b28..702153204c8 100644 --- a/packages/framework/src/Hyde.php +++ b/packages/framework/src/Hyde.php @@ -2,6 +2,7 @@ namespace Hyde\Framework; +use Hyde\Framework\Contracts\HydeKernelContract; use Hyde\Framework\Contracts\RouteContract; use Hyde\Framework\Helpers\Features; use Illuminate\Support\Facades\Facade; @@ -20,29 +21,30 @@ * @method static string vendorPath(string $path = '') * @method static string getMarkdownPagePath(string $path = '') * @method static string getSiteOutputPath(string $path = '') - * @method static string formatHtmlPath(string $destination) * @method static string getBladePagePath(string $path = '') * @method static string pathToRelative(string $path) - * @method static string path(string $path = '') * @method static Features features() * @method static bool hasFeature(string $feature) + * @method static string path(string $path = '') * @method static string relativeLink(string $destination) * @method static string getMarkdownPostPath(string $path = '') * @method static bool|int copy(string $from, string $to, bool $force = false) * @method static string getModelSourcePath(string $model, string $path = '') * @method static string image(string $name) - * @method static void macro(string $name, callable|object $macro) * @method static RouteContract|null currentRoute() * @method static string currentPage() - * @method static false|string uriPath(null|string $path = '') - * @method static bool hasSiteUrl() - * @method static string url(string $path = '', ?string $default = null) - * @method static void setBasePath($basePath) + * @method static string url(string $path = '', null|string $default = null) + * @method static void setBasePath(string $basePath) * @method static void mixin(object $mixin, bool $replace = true) + * @method static string formatHtmlPath(string $destination) * @method static string makeTitle(string $slug) + * @method static bool hasSiteUrl() * @method static string getBasePath() - * @method static HydeKernel getInstance() + * @method static HydeKernelContract getInstance() * @method static string getDocumentationPagePath(string $path = '') + * @method static bool hasMacro(string $name) + * @method static void macro(string $name, callable|object $macro) + * @method static void flushMacros() */ class Hyde extends Facade {