From a52e433031dc6753d98ec7957979cdd28a1a878c Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Thu, 21 Nov 2019 21:50:56 +0100 Subject: [PATCH] add minimal functional tests --- composer.json | 25 ++++----- tests/App/AppKernel.php | 72 +++++++++++++++++++++++++ tests/App/Block/DemoBlockService.php | 27 ++++++++++ tests/App/Controller/DemoController.php | 28 ++++++++++ tests/App/config.yml | 18 +++++++ tests/App/templates/block.html.twig | 1 + tests/App/templates/index.html.twig | 14 +++++ tests/Functional/FunctionalTest.php | 30 +++++++++++ 8 files changed, 203 insertions(+), 12 deletions(-) create mode 100644 tests/App/AppKernel.php create mode 100644 tests/App/Block/DemoBlockService.php create mode 100644 tests/App/Controller/DemoController.php create mode 100644 tests/App/config.yml create mode 100644 tests/App/templates/block.html.twig create mode 100644 tests/App/templates/index.html.twig create mode 100644 tests/Functional/FunctionalTest.php diff --git a/composer.json b/composer.json index c4c57377..cbd74a4b 100644 --- a/composer.json +++ b/composer.json @@ -27,25 +27,26 @@ "sonata-project/doctrine-extensions": "^1.1", "sonata-project/form-extensions": "^1.0", "sonata-project/twig-extensions": "^1.0", - "symfony/asset": "^4.3 || 5.0", - "symfony/config": "^4.3 || 5.0", - "symfony/console": "^4.3 || 5.0", - "symfony/dependency-injection": "^4.3 || 5.0", + "symfony/asset": "^4.3 || ^5.0", + "symfony/config": "^4.3 || ^5.0", + "symfony/console": "^4.3 || ^5.0", + "symfony/dependency-injection": "^4.3 || ^5.0", "symfony/event-dispatcher-contracts": "^1.1", - "symfony/form": "^4.3 || 5.0", - "symfony/framework-bundle": "^4.3 || 5.0", - "symfony/http-foundation": "^4.3 || 5.0", - "symfony/http-kernel": "^4.3 || 5.0", - "symfony/options-resolver": "^4.3 || 5.0", - "symfony/twig-bundle": "^4.3 || 5.0", + "symfony/form": "^4.3 || ^5.0", + "symfony/framework-bundle": "^4.3 || ^5.0", + "symfony/http-foundation": "^4.3 || ^5.0", + "symfony/http-kernel": "^4.3 || ^5.0", + "symfony/options-resolver": "^4.3 || ^5.0", + "symfony/twig-bundle": "^4.3 || ^5.0", "twig/twig": "^2.11 || ^3.0" }, "require-dev": { "knplabs/knp-menu-bundle": "^2.0", "matthiasnoback/symfony-dependency-injection-test": "^4.0", - "symfony/debug": "^4.3 || 5.0", + "symfony/browser-kit": "^4.3 || ^5.0", + "symfony/debug": "^4.3 || ^5.0", "symfony/phpunit-bridge": "^5.0", - "symfony/stopwatch": "^4.3 || 5.0" + "symfony/stopwatch": "^4.3 || ^5.0" }, "suggest": { "knplabs/knp-menu-bundle": "^2.0", diff --git a/tests/App/AppKernel.php b/tests/App/AppKernel.php new file mode 100644 index 00000000..073a7ea4 --- /dev/null +++ b/tests/App/AppKernel.php @@ -0,0 +1,72 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\BlockBundle\Tests\App; + +use Sonata\BlockBundle\SonataBlockBundle; +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; +use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; +use Symfony\Bundle\TwigBundle\TwigBundle; +use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\Routing\RouteCollectionBuilder; + +final class AppKernel extends Kernel +{ + use MicroKernelTrait; + + public function __construct() + { + parent::__construct('test', false); + } + + public function registerBundles() + { + return [ + new FrameworkBundle(), + new TwigBundle(), + new SonataBlockBundle(), + ]; + } + + public function getCacheDir(): string + { + return $this->getBaseDir().'cache'; + } + + public function getLogDir(): string + { + return $this->getBaseDir().'log'; + } + + public function getProjectDir(): string + { + return __DIR__; + } + + protected function configureRoutes(RouteCollectionBuilder $routes) + { + $routes->import(__DIR__.'/Controller/', '/', 'annotation'); + } + + protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader) + { + $loader->load(__DIR__.'/config.yml'); + } + + private function getBaseDir(): string + { + return sys_get_temp_dir().'/sonata-block-bundle/var/'; + } +} diff --git a/tests/App/Block/DemoBlockService.php b/tests/App/Block/DemoBlockService.php new file mode 100644 index 00000000..ef011881 --- /dev/null +++ b/tests/App/Block/DemoBlockService.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\BlockBundle\Tests\App\Block; + +use Sonata\BlockBundle\Block\Service\AbstractBlockService; +use Symfony\Component\OptionsResolver\OptionsResolver; + +final class DemoBlockService extends AbstractBlockService +{ + public function configureSettings(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'template' => 'block.html.twig', + ]); + } +} diff --git a/tests/App/Controller/DemoController.php b/tests/App/Controller/DemoController.php new file mode 100644 index 00000000..1781ac11 --- /dev/null +++ b/tests/App/Controller/DemoController.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\BlockBundle\Tests\App\Controller; + +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\Routing\Annotation\Route; + +final class DemoController extends AbstractController +{ + /** + * @Route("/", name="home") + */ + public function index() + { + return $this->render('index.html.twig'); + } +} diff --git a/tests/App/config.yml b/tests/App/config.yml new file mode 100644 index 00000000..e277405e --- /dev/null +++ b/tests/App/config.yml @@ -0,0 +1,18 @@ +framework: + secret: secret + +twig: + paths: + - '%kernel.project_dir%/templates' + +services: + _defaults: + autowire: true + autoconfigure: true + + Sonata\BlockBundle\Tests\App\Block\DemoBlockService: + tags: ['sonata.block'] + + Sonata\BlockBundle\Tests\App\Controller\DemoController: + tags: + - controller.service_arguments diff --git a/tests/App/templates/block.html.twig b/tests/App/templates/block.html.twig new file mode 100644 index 00000000..bc56c4d8 --- /dev/null +++ b/tests/App/templates/block.html.twig @@ -0,0 +1 @@ +Foo diff --git a/tests/App/templates/index.html.twig b/tests/App/templates/index.html.twig new file mode 100644 index 00000000..cbe44122 --- /dev/null +++ b/tests/App/templates/index.html.twig @@ -0,0 +1,14 @@ + + + + + Welcome! + + +{{ sonata_block_render({ 'type': 'Sonata\\BlockBundle\\Tests\\App\\Block\\DemoBlockService' }, { +}) }} + +{{ sonata_block_render({ 'type': 'sonata.block.service.text' }, { +}) }} + + diff --git a/tests/Functional/FunctionalTest.php b/tests/Functional/FunctionalTest.php new file mode 100644 index 00000000..0cd7646a --- /dev/null +++ b/tests/Functional/FunctionalTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\BlockBundle\Tests\Functional; + +use Sonata\BlockBundle\Tests\App\AppKernel; +use Symfony\Bundle\FrameworkBundle\KernelBrowser; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + +final class FunctionalTest extends WebTestCase +{ + public function testRenderBlock(): void + { + $kernel = new AppKernel(); + $client = new KernelBrowser($kernel); + $client->request('GET', '/'); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + } +}