Skip to content

Commit

Permalink
Merge pull request #32 from musimana/feature/AppVersionFormatters
Browse files Browse the repository at this point in the history
App Version Formatters
  • Loading branch information
musimana authored Mar 30, 2024
2 parents fff6751 + 0c36af8 commit 71ba4b8
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Resources\Views\Public\Formatters;
namespace App\Http\Resources\Formatters;

use App\Interfaces\Resources\Formatters\ConstantStringFormatterInterface;

Expand Down
19 changes: 19 additions & 0 deletions app/Http/Resources/Formatters/LaravelVersionFormatterResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources\Formatters;

use App\Interfaces\Resources\Formatters\ConstantStringFormatterInterface;
use Illuminate\Foundation\Application;

final class LaravelVersionFormatterResource implements ConstantStringFormatterInterface
{
/** Get the formatted string of the app's Laravel version. */
public function getValue(): string
{
$delimiter_position = strpos(Application::VERSION, '.');

return $delimiter_position
? substr(Application::VERSION, 0, $delimiter_position) . '.x'
: Application::VERSION;
}
}
18 changes: 18 additions & 0 deletions app/Http/Resources/Formatters/PhpVersionFormatterResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Resources\Formatters;

use App\Interfaces\Resources\Formatters\ConstantStringFormatterInterface;

final class PhpVersionFormatterResource implements ConstantStringFormatterInterface
{
/** Get the formatted string of the app's PHP version. */
public function getValue(): string
{
$delimiter_position = strrpos(PHP_VERSION, '.');

return $delimiter_position
? substr(PHP_VERSION, 0, $delimiter_position) . '.x'
: PHP_VERSION;
}
}
12 changes: 6 additions & 6 deletions app/Http/Resources/Views/Blocks/StackBlockResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace App\Http\Resources\Views\Blocks;

use App\Http\Resources\Formatters\LaravelVersionFormatterResource;
use App\Http\Resources\Formatters\PhpVersionFormatterResource;
use App\Interfaces\Resources\Items\ConstantItemInterface;
use Illuminate\Foundation\Application;

final class StackBlockResource implements ConstantItemInterface
{
Expand All @@ -14,10 +15,9 @@ final class StackBlockResource implements ConstantItemInterface
*/
public function getItem(): array
{
$laravel_delimiter_position = strpos(Application::VERSION, '.');
$laravel_version = $laravel_delimiter_position
? substr(Application::VERSION, 0, $laravel_delimiter_position) . '.x'
: Application::VERSION;
$laravel_version = (new LaravelVersionFormatterResource)->getValue();

$php_version = (new PhpVersionFormatterResource)->getValue();

$php_delimiter_position = strrpos(PHP_VERSION, '.');
$php_version = $php_delimiter_position
Expand Down Expand Up @@ -58,7 +58,7 @@ public function getItem(): array
'html' => implode('', [
'<h3 class="w-full mb-4 font-semibold text-sm text-gray-950 dark:text-gray-100 uppercase tracking-widest">Stack</h3><ul class="list-disc ml-8 mb-8">',
$li_elements_string,
'</ul><p>For PHP v' . $php_version . '</p>',
'</ul><p>Running on PHP v' . $php_version . '</p>',
]),
];
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Views/MetadataViewResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Http\Resources\Views\Navbars\DesktopNavbarResource;
use App\Http\Resources\Views\Navbars\MobileNavbarResource;
use App\Http\Resources\Views\Public\Formatters\CopyrightFormatterResource;
use App\Http\Resources\Formatters\CopyrightFormatterResource;
use App\Interfaces\Resources\Items\ArrayToItemInterface;
use Illuminate\Support\Facades\Route;

Expand Down
11 changes: 5 additions & 6 deletions tests/Datasets/Blocks.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php

use App\Http\Resources\Formatters\LaravelVersionFormatterResource;
use App\Http\Resources\Formatters\PhpVersionFormatterResource;
use App\Models\Block;
use Illuminate\Foundation\Application;

$laravel_delimiter_position = strpos(Application::VERSION, '.');
$laravel_version = $laravel_delimiter_position
? substr(Application::VERSION, 0, $laravel_delimiter_position) . '.x'
: Application::VERSION;
$laravel_version = (new LaravelVersionFormatterResource)->getValue();
$php_version = (new PhpVersionFormatterResource)->getValue();

$stack_html = '<h3 class="w-full mb-4 font-semibold text-sm text-gray-950 dark:text-gray-100 uppercase tracking-widest">Stack</h3><ul class="list-disc ml-8 mb-8">'
. '<li><a href="https://vuejs.org/" target="_blank" rel="noopener noreferrer" class="group inline-flex items-center hover:text-gray-900 dark:hover:text-gray-50 focus:outline focus:outline-2 focus:rounded-sm focus:outline-gray-100"><span style="display: inline-block;" class="font-mono w-48"><strong>V</strong>ue v3.x</span></a><em>Client-side framework</em></li>'
. '<li><a href="https://inertiajs.com/" target="_blank" rel="noopener noreferrer" class="group inline-flex items-center hover:text-gray-900 dark:hover:text-gray-50 focus:outline focus:outline-2 focus:rounded-sm focus:outline-gray-100"><span style="display: inline-block;" class="font-mono w-48"><strong>I</strong>nertia v1.x</span></a><em>Build tool</em></li>'
. '<li><a href="https://laravel.com/" target="_blank" rel="noopener noreferrer" class="group inline-flex items-center hover:text-gray-900 dark:hover:text-gray-50 focus:outline focus:outline-2 focus:rounded-sm focus:outline-gray-100"><span style="display: inline-block;" class="font-mono w-48"><strong>L</strong>aravel v' . $laravel_version . '</span></a><em>Server-side framework</em></li>'
. '<li><a href="https://tailwindcss.com/docs" target="_blank" rel="noopener noreferrer" class="group inline-flex items-center hover:text-gray-900 dark:hover:text-gray-50 focus:outline focus:outline-2 focus:rounded-sm focus:outline-gray-100"><span style="display: inline-block;" class="font-mono w-48"><strong>T</strong>ailwind v3.x</span></a><em>CSS framework</em></li>'
. '</ul><p>For PHP v8.2.x</p>';
. '</ul><p>Running on PHP v' . $php_version . '</p>';

$tabs = ['tabs' => ['Tab One', 'Tab Two']];

Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/App/Http/HttpArchitectureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
->expect('App\Http\Resources\Files')
->toHaveSuffix('FileResource');

arch('app/Http/Resources/Views/Public/Formatters has valid architecture')
->expect('App\Http\Resources\Formatters')
->toHaveSuffix('FormatterResource')
->toBeUsedIn('App\Http')
->tohaveMethod('getValue');

arch('app/Http/Resources/Models has valid architecture')
->expect('App\Http\Resources\Models')
->toHaveSuffix('ModelResource')
Expand Down Expand Up @@ -62,12 +68,6 @@
->toBeUsedIn('App\Http')
->tohaveMethod('getItem');

arch('app/Http/Resources/Views/Public/Formatters has valid architecture')
->expect('App\Http\Resources\Views\Public\Formatters')
->toHaveSuffix('FormatterResource')
->toBeUsedIn('App\Http')
->tohaveMethod('getValue');

arch('app/Http/Resources/Views/Public/Metadata has valid architecture')
->expect('App\Http\Resources\Views\Public\Metadata')
->toHaveSuffix('MetadataResource')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use App\Http\Resources\Views\Public\Formatters\CopyrightFormatterResource;
use App\Http\Resources\Formatters\CopyrightFormatterResource;
use App\Interfaces\Resources\Formatters\ConstantStringFormatterInterface;

arch('it implements the expected interface')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use App\Http\Resources\Formatters\LaravelVersionFormatterResource;
use App\Interfaces\Resources\Formatters\ConstantStringFormatterInterface;
use Illuminate\Foundation\Application;

arch('it implements the expected interface')
->expect(LaravelVersionFormatterResource::class)
->toImplement(ConstantStringFormatterInterface::class);

test('getValue returns ok with valid inputs', function () {
$actual = (new LaravelVersionFormatterResource())->getValue();

expect($actual)
->toBeString()
->toEqual(substr(Application::VERSION, 0, strpos(Application::VERSION, '.') ?: null) . '.x');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use App\Http\Resources\Formatters\PhpVersionFormatterResource;
use App\Interfaces\Resources\Formatters\ConstantStringFormatterInterface;

arch('it implements the expected interface')
->expect(PhpVersionFormatterResource::class)
->toImplement(ConstantStringFormatterInterface::class);

test('getValue returns ok with valid inputs', function () {
$actual = (new PhpVersionFormatterResource())->getValue();

expect($actual)
->toBeString()
->toEqual(substr(PHP_VERSION, 0, strrpos(PHP_VERSION, '.') ?: null) . '.x');
});

0 comments on commit 71ba4b8

Please sign in to comment.