Skip to content

Commit

Permalink
tests: improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 12, 2024
1 parent dafa250 commit f503641
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 151 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"nette/security": "^3.0",
"latte/latte": "^2.10.2 || ^3.0.12",
"tracy/tracy": "^2.6",
"mockery/mockery": "^1.0",
"mockery/mockery": "^1.0 || ^2.0",
"phpstan/phpstan-nette": "^0.12",
"jetbrains/phpstorm-attributes": "dev-master"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

/**
* Test: TemplateFactory nonce
* Test: UIExtension nonce
* @phpVersion 8.0
*/

declare(strict_types=1);

use Nette\Application\UI;
use Nette\Bridges\ApplicationLatte;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';
Expand All @@ -17,22 +16,16 @@ if (version_compare(Latte\Engine::VERSION, '3', '<')) {
Tester\Environment::skip('Test for Latte 3');
}

$latte = new Latte\Engine;

$latteFactory = Mockery::mock(ApplicationLatte\LatteFactory::class);
$latteFactory->shouldReceive('create')->andReturn($latte);

$response = Mockery::mock(Nette\Http\IResponse::class);
$response->shouldReceive('getHeader')->with('Content-Security-Policy')->andReturn("hello 'nonce-abcd123==' world");

$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(
'<script></script>',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

/**
* Test: TemplateFactory nonce
* Test: UIExtension nonce
* @phpVersion 8.0
*/

declare(strict_types=1);

use Nette\Application\UI;
use Nette\Bridges\ApplicationLatte;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';
Expand All @@ -19,11 +18,6 @@ if (version_compare(Latte\Engine::VERSION, '3', '<')) {

Tester\Environment::bypassFinals();

$latte = new Latte\Engine;

$latteFactory = Mockery::mock(ApplicationLatte\LatteFactory::class);
$latteFactory->shouldReceive('create')->andReturn($latte);

$response = Mockery::mock(Nette\Http\IResponse::class);
$response->shouldReceive('getHeader')->with('Content-Security-Policy')->andReturn("hello 'nonce-abcd123==' world");

Expand All @@ -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(
'<script nonce="abcd123=="></script>',
Expand Down
2 changes: 1 addition & 1 deletion tests/Bridges.Latte3/expected/isLinkCurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<a href="';
echo LR\Filters::escapeHtmlAttr($this->global->uiControl->link('default')) /* line 9 */;
echo '"';
echo ($ʟ_tmp = array_filter([($this->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>
';
%A%
10 changes: 2 additions & 8 deletions tests/Bridges.Latte3/isLinkCurrent().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 2 additions & 6 deletions tests/Bridges.Latte3/{ifCurrent}.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
27 changes: 21 additions & 6 deletions tests/UI/Component.redirect().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -43,39 +43,54 @@ $presenter->injectPrimary(


test('', function () use ($presenter) {
$presenter->redirect('foo');
try {
$presenter->redirect('foo');
} catch (Throwable $e) {
}
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());
});


test('', function () use ($presenter) {
$presenter->redirect('foo', ['arg' => 1]);
try {
$presenter->redirect('foo', ['arg' => 1]);
} catch (Throwable $e) {
}
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());
});


test('', function () use ($presenter) {
$presenter->redirect('foo', 2);
try {
$presenter->redirect('foo', 2);
} catch (Throwable $e) {
}
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());
});


test('', function () use ($presenter) {
$presenter->redirectPermanent('foo', 2);
try {
$presenter->redirectPermanent('foo', 2);
} catch (Throwable $e) {
}
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());
});


test('', function () use ($presenter) {
$presenter->redirectPermanent('foo', ['arg' => 1]);
try {
$presenter->redirectPermanent('foo', ['arg' => 1]);
} catch (Throwable $e) {
}
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());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

/**
* Test: Nette\Application\UI\Presenter::getRequestParams
*/

declare(strict_types=1);

use Nette\Application\Attributes\Parameter;
Expand Down
File renamed without changes.

0 comments on commit f503641

Please sign in to comment.