-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
672 additions
and
9 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,49 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: ["main"] | ||
pull_request: | ||
branches: ["main"] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
tests: | ||
name: Build | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
php: | ||
- 8.2 | ||
- 8.3 | ||
- 8.4 | ||
|
||
steps: | ||
- name: Set up PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
ini-values: zend.assertions=1 | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Validate composer.json and composer.lock | ||
run: composer validate --strict | ||
|
||
- name: Cache Composer packages | ||
id: composer-cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: vendor | ||
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-php- | ||
- name: Install dependencies | ||
run: composer install --classmap-authoritative | ||
|
||
- name: Run tests | ||
run: vendor/bin/phpunit tests |
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,16 @@ | ||
<?php | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use Yceruto\DecoratorBundle\Decorator\Serializer\SerializerDecorator; | ||
|
||
use function Symfony\Component\DependencyInjection\Loader\Configurator\service; | ||
|
||
return static function (ContainerConfigurator $container): void { | ||
$container->services() | ||
->set(SerializerDecorator::class) | ||
->args([ | ||
service('serializer'), | ||
service('mime_types'), | ||
]) | ||
->tag('decorator'); | ||
}; |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Decorator Bundle package. | ||
* | ||
* (c) Yonel Ceruto <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Yceruto\DecoratorBundle\Decorator\Serializer; | ||
|
||
use Yceruto\Decorator\Attribute\Decorate; | ||
|
||
#[\Attribute(\Attribute::TARGET_METHOD)] | ||
final class Serialize extends Decorate | ||
{ | ||
public function __construct( | ||
string $format = 'json', | ||
array $context = [], | ||
int $status = 200, | ||
array $headers = [], | ||
) { | ||
parent::__construct(SerializerDecorator::class, [ | ||
'format' => $format, | ||
'context' => $context, | ||
'status' => $status, | ||
'headers' => $headers, | ||
]); | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Decorator Bundle package. | ||
* | ||
* (c) Yonel Ceruto <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Yceruto\DecoratorBundle\Decorator\Serializer; | ||
|
||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Mime\MimeTypesInterface; | ||
use Symfony\Component\Serializer\Exception\UnsupportedFormatException; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
use Yceruto\Decorator\DecoratorInterface; | ||
|
||
final readonly class SerializerDecorator implements DecoratorInterface | ||
{ | ||
public function __construct( | ||
private SerializerInterface $serializer, | ||
private MimeTypesInterface $mimeTypes, | ||
) { | ||
} | ||
|
||
public function decorate(\Closure $func, string $format = 'json', array $context = [], int $status = 200, array $headers = []): \Closure | ||
{ | ||
$headers['Content-Type'] ??= current($this->mimeTypes->getMimeTypes($format)) ?: throw new UnsupportedFormatException(sprintf('Format "%s" is not supported.', $format)); | ||
|
||
return function (mixed ...$args) use ($func, $format, $context, $status, $headers): Response { | ||
$result = $func(...$args); | ||
|
||
if ($result instanceof RedirectResponse) { | ||
return $result; | ||
} | ||
|
||
if (null === $result || '' === $result) { | ||
return new Response(null, 204, $headers); | ||
} | ||
|
||
$content = $this->serializer->serialize($result, $format, $context); | ||
|
||
return new Response($content, $status, $headers); | ||
}; | ||
} | ||
} |
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,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Decorator Bundle package. | ||
* | ||
* (c) Yonel Ceruto <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Yceruto\DecoratorBundle\Tests\Integration; | ||
|
||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Yceruto\DecoratorBundle\Tests\Integration\App\AppKernel; | ||
|
||
class AbstractWebTestCase extends WebTestCase | ||
{ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
static::deleteTmpDir(); | ||
} | ||
|
||
public static function tearDownAfterClass(): void | ||
{ | ||
static::deleteTmpDir(); | ||
} | ||
|
||
protected static function deleteTmpDir(): void | ||
{ | ||
if (!file_exists($dir = sys_get_temp_dir().'/'.static::getVarDir())) { | ||
return; | ||
} | ||
|
||
$fs = new Filesystem(); | ||
$fs->remove($dir); | ||
} | ||
|
||
protected static function getKernelClass(): string | ||
{ | ||
require_once __DIR__.'/App/AppKernel.php'; | ||
|
||
return AppKernel::class; | ||
} | ||
|
||
protected static function createClient(array $options = [], array $server = []): KernelBrowser | ||
{ | ||
if (!isset($options['test_case'])) { | ||
$options['test_case'] = static::getTestCase(); | ||
} | ||
|
||
return parent::createClient($options, $server); | ||
} | ||
|
||
protected static function createKernel(array $options = []): KernelInterface | ||
{ | ||
$class = self::getKernelClass(); | ||
|
||
if (!isset($options['test_case'])) { | ||
$options['test_case'] = static::getTestCase(); | ||
} | ||
|
||
return new $class( | ||
static::getVarDir(), | ||
$options['test_case'], | ||
$options['root_config'] ?? 'config.yaml', | ||
$options['environment'] ?? 'test', | ||
$options['debug'] ?? true, | ||
); | ||
} | ||
|
||
protected static function getVarDir(): string | ||
{ | ||
return 'Decorator'.substr(strrchr(static::class, '\\'), 1); | ||
} | ||
|
||
protected static function getTestCase(): string | ||
{ | ||
$parts = explode('\\', static::class); | ||
|
||
return substr(end($parts), 0, -4); | ||
} | ||
} |
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,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Decorator Bundle package. | ||
* | ||
* (c) Yonel Ceruto <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Yceruto\DecoratorBundle\Tests\Integration\App; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; | ||
use Symfony\Component\Config\Loader\LoaderInterface; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
|
||
class AppKernel extends Kernel | ||
{ | ||
use MicroKernelTrait; | ||
|
||
private string $varDir; | ||
private string $testCase; | ||
private string $rootConfig; | ||
|
||
public function __construct($varDir, $testCase, $rootConfig, $environment, $debug) | ||
{ | ||
if (!is_dir(__DIR__.'/'.$testCase)) { | ||
throw new \InvalidArgumentException(sprintf('The test case "%s" does not exist.', $testCase)); | ||
} | ||
$this->varDir = $varDir; | ||
$this->testCase = $testCase; | ||
|
||
$fs = new Filesystem(); | ||
if (!$fs->isAbsolutePath($rootConfig) && !file_exists($rootConfig = __DIR__.'/'.$testCase.'/'.$rootConfig)) { | ||
$rootConfig = __DIR__.'/config.yaml'; | ||
} | ||
$this->rootConfig = $rootConfig; | ||
|
||
parent::__construct($environment, $debug); | ||
} | ||
|
||
public function registerBundles(): iterable | ||
{ | ||
if (!file_exists($filename = $this->getProjectDir().'/'.$this->testCase.'/bundles.php')) { | ||
$filename = $this->getProjectDir().'/bundles.php'; | ||
} | ||
|
||
return include $filename; | ||
} | ||
|
||
public function getProjectDir(): string | ||
{ | ||
return __DIR__; | ||
} | ||
|
||
public function getCacheDir(): string | ||
{ | ||
return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->environment; | ||
} | ||
|
||
public function getLogDir(): string | ||
{ | ||
return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/logs'; | ||
} | ||
|
||
public function registerContainerConfiguration(LoaderInterface $loader): void | ||
{ | ||
$loader->load($this->rootConfig); | ||
} | ||
|
||
public function __sleep(): array | ||
{ | ||
return ['varDir', 'testCase', 'rootConfig', 'environment', 'debug']; | ||
} | ||
|
||
public function __wakeup(): void | ||
{ | ||
$this->__construct($this->varDir, $this->testCase, $this->rootConfig, $this->environment, $this->debug); | ||
} | ||
|
||
protected function getKernelParameters(): array | ||
{ | ||
$parameters = parent::getKernelParameters(); | ||
$parameters['kernel.test_case'] = $this->testCase; | ||
|
||
return $parameters; | ||
} | ||
} |
Oops, something went wrong.