NelmioApiDocBundle/Tests/Render/Html/GetNelmioAssetTest.php
Zdeněk Drahoš 2c890ff93b Hotfix testing private service in Symfony < 5.3
https://github.com/nelmio/NelmioApiDocBundle/runs/3031148906?check_suite_focus=true#step:7:106
Error: Call to undefined method Nelmio\ApiDocBundle\Tests\Render\Html\GetNelmioAssetTest::getContainer()

https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing
In practice, tests based on WebTestCase and KernelTestCase now access to a special container via the static::$container property that allows fetching non-removed private services:

https://github.com/symfony/symfony/blob/5.3/CHANGELOG-5.3.md#changelog-for-53x
feature #40366 [FrameworkBundle] Add KernelTestCase::getContainer() (Nyholm)
static $container @deprecated since Symfony 5.3, use static::getContainer() instead
2021-07-10 18:00:11 +02:00

113 lines
3.5 KiB
PHP

<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Render\Html;
use Nelmio\ApiDocBundle\Render\Html\AssetsMode;
use Nelmio\ApiDocBundle\Render\Html\GetNelmioAsset;
use Nelmio\ApiDocBundle\Tests\Functional\WebTestCase;
class GetNelmioAssetTest extends WebTestCase
{
/** @dataProvider provideAsset */
public function test($mode, $asset, $expectedContent)
{
static::bootKernel();
/** @var GetNelmioAsset $getNelmioAsset */
$getNelmioAsset = static::$container->get('nelmio_api_doc.render_docs.html.asset');
$twigFunction = $getNelmioAsset->toTwigFunction($mode);
self::assertSame($expectedContent, $twigFunction->getCallable()->__invoke($asset, $mode));
}
public function provideAsset()
{
$cdnDir = 'https://cdn.jsdelivr.net/gh/nelmio/NelmioApiDocBundle/Resources/public';
$resourceDir = __DIR__.'/../../../Resources/public';
return $this->provideCss($cdnDir, $resourceDir)
+ $this->provideJs($cdnDir, $resourceDir)
+ $this->provideImage($cdnDir);
}
private function provideCss($cdnDir, $resourceDir)
{
return [
'bundled css' => [
AssetsMode::BUNDLE,
'style.css',
'<link rel="stylesheet" href="/bundles/nelmioapidoc/style.css">',
],
'cdn css' => [
AssetsMode::CDN,
'style.css',
'<link rel="stylesheet" href="'.$cdnDir.'/style.css">',
],
'offline css' => [
AssetsMode::OFFLINE,
'style.css',
'<style>'.file_get_contents($resourceDir.'/style.css').'</style>',
],
'external css' => [
AssetsMode::BUNDLE,
'https://cdn.com/my.css',
'<link rel="stylesheet" href="https://cdn.com/my.css">',
],
];
}
private function provideJs($cdnDir, $resourceDir)
{
return [
'bundled js' => [
AssetsMode::BUNDLE,
'init-swagger-ui.js',
'<script src="/bundles/nelmioapidoc/init-swagger-ui.js"></script>',
],
'cdn js' => [
AssetsMode::CDN,
'init-swagger-ui.js',
'<script src="'.$cdnDir.'/init-swagger-ui.js"></script>',
],
'offline js' => [
AssetsMode::OFFLINE,
'init-swagger-ui.js',
'<script>'.file_get_contents($resourceDir.'/init-swagger-ui.js').'</script>',
],
'external js' => [
AssetsMode::BUNDLE,
'https://cdn.com/my.js',
'<script src="https://cdn.com/my.js"></script>',
],
];
}
private function provideImage($cdnDir)
{
return [
'bundled image' => [
AssetsMode::BUNDLE,
'logo.png',
'/bundles/nelmioapidoc/logo.png',
],
'cdn image' => [
AssetsMode::CDN,
'logo.png',
$cdnDir.'/logo.png',
],
'offline image fallbacks to cdn' => [
AssetsMode::OFFLINE,
'logo.png',
$cdnDir.'/logo.png',
],
];
}
}