-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): New package helper
Viewer
.
- Loading branch information
1 parent
a5d98e4
commit 9502ebe
Showing
9 changed files
with
166 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Core\Helpers; | ||
|
||
use Illuminate\Contracts\View\Factory as ViewFactoryContract; | ||
use Illuminate\Contracts\View\View as ViewContract; | ||
|
||
/** | ||
* Special wrapper around {@see ViewFactoryContract} to help render package's views. | ||
*/ | ||
abstract class Viewer { | ||
public function __construct( | ||
protected readonly Translator $translator, | ||
protected readonly ViewFactoryContract $factory, | ||
protected readonly string $package, | ||
) { | ||
// empty | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $data | ||
*/ | ||
public function get(string $view, array $data = []): ViewContract { | ||
return $this->factory->make( | ||
"{$this->package}::{$view}", | ||
$this->getDefaultData() + $data, | ||
); | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $data | ||
*/ | ||
public function render(string $view, array $data = []): string { | ||
return $this->get($view, $data)->render(); | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
protected function getDefaultData(): array { | ||
return [ | ||
'translator' => $this->translator, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Core\Helpers; | ||
|
||
use Illuminate\Contracts\View\Factory as ViewFactory; | ||
use Illuminate\Contracts\View\View as ViewContract; | ||
use LastDragon_ru\LaraASP\Core\Testing\Package\TestCase; | ||
use Mockery; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[CoversClass(Viewer::class)] | ||
class ViewerTest extends TestCase { | ||
public function testGet(): void { | ||
$view = 'view'; | ||
$data = ['a' => 123]; | ||
$package = 'package'; | ||
$translator = Mockery::mock(Translator::class); | ||
$factory = Mockery::mock(ViewFactory::class); | ||
$factory | ||
->shouldReceive('make') | ||
->with("{$package}::{$view}", ['translator' => $translator] + $data) | ||
->once() | ||
->andReturn( | ||
Mockery::mock(ViewContract::class), | ||
); | ||
|
||
$viewer = new class($translator, $factory, $package) extends Viewer { | ||
// empty | ||
}; | ||
|
||
$viewer->get($view, $data); | ||
} | ||
|
||
public function testRender(): void { | ||
$view = 'view'; | ||
$data = ['a' => 123]; | ||
$package = 'package'; | ||
$content = 'content'; | ||
$translator = Mockery::mock(Translator::class); | ||
$factory = Mockery::mock(ViewFactory::class); | ||
$template = Mockery::mock(ViewContract::class); | ||
$template | ||
->shouldReceive('render') | ||
->once() | ||
->andReturn($content); | ||
|
||
$viewer = Mockery::mock(Viewer::class, [$translator, $factory, $package]); | ||
$viewer->makePartial(); | ||
$viewer | ||
->shouldReceive('get') | ||
->with($view, $data) | ||
->once() | ||
->andReturn($template); | ||
|
||
self::assertEquals($content, $viewer->render($view, $data)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator; | ||
|
||
use Illuminate\Contracts\Translation\Translator as TranslatorContract; | ||
use LastDragon_ru\LaraASP\Core\Helpers\Translator; | ||
use LastDragon_ru\LaraASP\Formatter\Package; | ||
|
||
class PackageTranslator extends Translator { | ||
public function __construct(TranslatorContract $translator) { | ||
parent::__construct($translator, Package::Name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator; | ||
|
||
use Illuminate\Contracts\View\Factory as ViewFactory; | ||
use LastDragon_ru\LaraASP\Core\Helpers\Viewer; | ||
|
||
class PackageViewer extends Viewer { | ||
public function __construct(PackageTranslator $translator, ViewFactory $factory) { | ||
parent::__construct($translator, $factory, Package::Name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters