From 743762fa8e5f6a2c614f5246d0cb1c1a55d911f6 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 6 Apr 2024 18:58:09 +0200 Subject: [PATCH] tests: improved tests --- ...ol.phpt => UIExtension.nonce.control.phpt} | 13 +++------ ....phpt => UIExtension.nonce.presenter.phpt} | 13 +++------ .../Bridges.Latte3/expected/isLinkCurrent.php | 2 +- tests/Bridges.Latte3/isLinkCurrent().phpt | 10 ++----- tests/Bridges.Latte3/{ifCurrent}.phpt | 8 ++---- tests/UI/Component.redirect().phpt | 27 ++++++++++++++----- ... ComponentReflection.getParameters().phpt} | 4 --- ... => Presenter.initGlobalParameters().phpt} | 0 8 files changed, 32 insertions(+), 45 deletions(-) rename tests/Bridges.Latte3/{TemplateFactory.nonce.control.phpt => UIExtension.nonce.control.phpt} (71%) rename tests/Bridges.Latte3/{TemplateFactory.nonce.presenter.phpt => UIExtension.nonce.presenter.phpt} (74%) rename tests/UI/{Presenter.getParameters.phpt => ComponentReflection.getParameters().phpt} (96%) rename tests/UI/{Presenter.parameters.phpt => Presenter.initGlobalParameters().phpt} (100%) diff --git a/tests/Bridges.Latte3/TemplateFactory.nonce.control.phpt b/tests/Bridges.Latte3/UIExtension.nonce.control.phpt similarity index 71% rename from tests/Bridges.Latte3/TemplateFactory.nonce.control.phpt rename to tests/Bridges.Latte3/UIExtension.nonce.control.phpt index b0db91521..0f19e5835 100644 --- a/tests/Bridges.Latte3/TemplateFactory.nonce.control.phpt +++ b/tests/Bridges.Latte3/UIExtension.nonce.control.phpt @@ -1,14 +1,13 @@ shouldReceive('create')->andReturn($latte); - $response = Mockery::mock(Nette\Http\IResponse::class); $response->shouldReceive('getHeader')->with('Content-Security-Policy')->andReturn("hello 'nonce-abcd123==' world"); @@ -29,10 +23,9 @@ $control = Mockery::mock(UI\Control::class); $control->shouldReceive('getPresenterIfExists')->andReturn(null); $control->shouldIgnoreMissing(); -$factory = new ApplicationLatte\TemplateFactory($latteFactory); -$factory->createTemplate($control); - +$latte = new Latte\Engine; $latte->setLoader(new Latte\Loaders\StringLoader); +$latte->addExtension(new Nette\Bridges\ApplicationLatte\UIExtension($control)); Assert::match( '', diff --git a/tests/Bridges.Latte3/TemplateFactory.nonce.presenter.phpt b/tests/Bridges.Latte3/UIExtension.nonce.presenter.phpt similarity index 74% rename from tests/Bridges.Latte3/TemplateFactory.nonce.presenter.phpt rename to tests/Bridges.Latte3/UIExtension.nonce.presenter.phpt index 05abfedca..ca0dace3a 100644 --- a/tests/Bridges.Latte3/TemplateFactory.nonce.presenter.phpt +++ b/tests/Bridges.Latte3/UIExtension.nonce.presenter.phpt @@ -1,14 +1,13 @@ shouldReceive('create')->andReturn($latte); - $response = Mockery::mock(Nette\Http\IResponse::class); $response->shouldReceive('getHeader')->with('Content-Security-Policy')->andReturn("hello 'nonce-abcd123==' world"); @@ -32,10 +26,9 @@ $presenter->shouldReceive('getPresenterIfExists')->andReturn($presenter); $presenter->shouldReceive('getHttpResponse')->andReturn($response); $presenter->shouldIgnoreMissing(); -$factory = new ApplicationLatte\TemplateFactory($latteFactory); -$factory->createTemplate($presenter); - +$latte = new Latte\Engine; $latte->setLoader(new Latte\Loaders\StringLoader); +$latte->addExtension(new Nette\Bridges\ApplicationLatte\UIExtension($presenter)); Assert::match( '', diff --git a/tests/Bridges.Latte3/expected/isLinkCurrent.php b/tests/Bridges.Latte3/expected/isLinkCurrent.php index 3871205c8..a86390a52 100644 --- a/tests/Bridges.Latte3/expected/isLinkCurrent.php +++ b/tests/Bridges.Latte3/expected/isLinkCurrent.php @@ -26,7 +26,7 @@ global->fn->isLinkCurrent)('default') ? 'current' : null])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line 9 */; + echo ($ʟ_tmp = array_filter([($this->global->fn->isLinkCurrent)(%a%'default') ? 'current' : null])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line 9 */; echo '>custom function '; %A% diff --git a/tests/Bridges.Latte3/isLinkCurrent().phpt b/tests/Bridges.Latte3/isLinkCurrent().phpt index bd101c6c1..fd567a251 100644 --- a/tests/Bridges.Latte3/isLinkCurrent().phpt +++ b/tests/Bridges.Latte3/isLinkCurrent().phpt @@ -17,20 +17,14 @@ if (version_compare(Latte\Engine::VERSION, '3', '<')) { Tester\Environment::bypassFinals(); -$latte = new Latte\Engine; - -$latteFactory = Mockery::mock(Nette\Bridges\ApplicationLatte\LatteFactory::class); -$latteFactory->shouldReceive('create')->andReturn($latte); - $presenter = Mockery::mock(Nette\Application\UI\Presenter::class); $presenter->shouldReceive('getPresenterIfExists')->andReturn($presenter); $presenter->shouldReceive('getHttpResponse')->andReturn((Mockery::mock(Nette\Http\IResponse::class))->shouldIgnoreMissing()); $presenter->shouldIgnoreMissing(); -$factory = new Nette\Bridges\ApplicationLatte\TemplateFactory($latteFactory); -$factory->createTemplate($presenter); - +$latte = new Latte\Engine; $latte->setLoader(new Latte\Loaders\StringLoader); +$latte->addExtension(new Nette\Bridges\ApplicationLatte\UIExtension($presenter)); Assert::matchFile( __DIR__ . '/expected/isLinkCurrent.php', diff --git a/tests/Bridges.Latte3/{ifCurrent}.phpt b/tests/Bridges.Latte3/{ifCurrent}.phpt index e2606d7fa..48fed1caa 100644 --- a/tests/Bridges.Latte3/{ifCurrent}.phpt +++ b/tests/Bridges.Latte3/{ifCurrent}.phpt @@ -16,18 +16,14 @@ Tester\Environment::bypassFinals(); $latte = new Latte\Engine; -$latteFactory = Mockery::mock(Nette\Bridges\ApplicationLatte\LatteFactory::class); -$latteFactory->shouldReceive('create')->andReturn($latte); - $presenter = Mockery::mock(Nette\Application\UI\Presenter::class); $presenter->shouldReceive('getPresenterIfExists')->andReturn($presenter); $presenter->shouldReceive('getHttpResponse')->andReturn((Mockery::mock(Nette\Http\IResponse::class))->shouldIgnoreMissing()); $presenter->shouldIgnoreMissing(); -$factory = new Nette\Bridges\ApplicationLatte\TemplateFactory($latteFactory); -$factory->createTemplate($presenter); - +$latte = new Latte\Engine; $latte->setLoader(new Latte\Loaders\StringLoader); +$latte->addExtension(new Nette\Bridges\ApplicationLatte\UIExtension($presenter)); Assert::matchFile( __DIR__ . '/expected/ifCurrent.php', diff --git a/tests/UI/Component.redirect().phpt b/tests/UI/Component.redirect().phpt index 2a749c432..cd0d2616d 100644 --- a/tests/UI/Component.redirect().phpt +++ b/tests/UI/Component.redirect().phpt @@ -26,7 +26,7 @@ class TestPresenter extends Application\UI\Presenter public function sendResponse(Application\Response $response): void { - $this->response = $response; + parent::sendResponse($this->response = $response); } } @@ -43,7 +43,10 @@ $presenter->injectPrimary( test('', function () use ($presenter) { - $presenter->redirect('foo'); + try { + $presenter->redirect('foo'); + } catch (Throwable) { + } Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(302, $presenter->response->getCode()); Assert::same('http://localhost/?action=foo&presenter=test', $presenter->response->getUrl()); @@ -51,7 +54,10 @@ test('', function () use ($presenter) { test('', function () use ($presenter) { - $presenter->redirect('foo', ['arg' => 1]); + try { + $presenter->redirect('foo', ['arg' => 1]); + } catch (Throwable) { + } Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(302, $presenter->response->getCode()); Assert::same('http://localhost/?arg=1&action=foo&presenter=test', $presenter->response->getUrl()); @@ -59,7 +65,10 @@ test('', function () use ($presenter) { test('', function () use ($presenter) { - $presenter->redirect('foo', 2); + try { + $presenter->redirect('foo', 2); + } catch (Throwable) { + } Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(302, $presenter->response->getCode()); Assert::same('http://localhost/?val=2&action=foo&presenter=test', $presenter->response->getUrl()); @@ -67,7 +76,10 @@ test('', function () use ($presenter) { test('', function () use ($presenter) { - $presenter->redirectPermanent('foo', 2); + try { + $presenter->redirectPermanent('foo', 2); + } catch (Throwable) { + } Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(301, $presenter->response->getCode()); Assert::same('http://localhost/?val=2&action=foo&presenter=test', $presenter->response->getUrl()); @@ -75,7 +87,10 @@ test('', function () use ($presenter) { test('', function () use ($presenter) { - $presenter->redirectPermanent('foo', ['arg' => 1]); + try { + $presenter->redirectPermanent('foo', ['arg' => 1]); + } catch (Throwable) { + } Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(301, $presenter->response->getCode()); Assert::same('http://localhost/?arg=1&action=foo&presenter=test', $presenter->response->getUrl()); diff --git a/tests/UI/Presenter.getParameters.phpt b/tests/UI/ComponentReflection.getParameters().phpt similarity index 96% rename from tests/UI/Presenter.getParameters.phpt rename to tests/UI/ComponentReflection.getParameters().phpt index 7a600e5f5..8c95daae0 100644 --- a/tests/UI/Presenter.getParameters.phpt +++ b/tests/UI/ComponentReflection.getParameters().phpt @@ -1,9 +1,5 @@