Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
x

x

x

x

y

x

test
  • Loading branch information
dg committed May 12, 2024
1 parent 743762f commit 780f9ba
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 108 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
10 changes: 5 additions & 5 deletions tests/UI/Component.redirect().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ $presenter->injectPrimary(
test('', function () use ($presenter) {
try {
$presenter->redirect('foo');
} catch (Throwable) {
} catch (Throwable $e) {
}
Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response);
Assert::same(302, $presenter->response->getCode());
Expand All @@ -56,7 +56,7 @@ test('', function () use ($presenter) {
test('', function () use ($presenter) {
try {
$presenter->redirect('foo', ['arg' => 1]);
} catch (Throwable) {
} catch (Throwable $e) {
}
Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response);
Assert::same(302, $presenter->response->getCode());
Expand All @@ -67,7 +67,7 @@ test('', function () use ($presenter) {
test('', function () use ($presenter) {
try {
$presenter->redirect('foo', 2);
} catch (Throwable) {
} catch (Throwable $e) {
}
Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response);
Assert::same(302, $presenter->response->getCode());
Expand All @@ -78,7 +78,7 @@ test('', function () use ($presenter) {
test('', function () use ($presenter) {
try {
$presenter->redirectPermanent('foo', 2);
} catch (Throwable) {
} catch (Throwable $e) {
}
Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response);
Assert::same(301, $presenter->response->getCode());
Expand All @@ -89,7 +89,7 @@ test('', function () use ($presenter) {
test('', function () use ($presenter) {
try {
$presenter->redirectPermanent('foo', ['arg' => 1]);
} catch (Throwable) {
} catch (Throwable $e) {
}
Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response);
Assert::same(301, $presenter->response->getCode());
Expand Down
196 changes: 94 additions & 102 deletions tests/UI/Presenter.storeRequest().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ declare(strict_types=1);

use Nette\Application;
use Nette\Http;
use Nette\Security;
use Tester\Assert;


Expand All @@ -23,148 +22,141 @@ class TestPresenter extends Application\UI\Presenter
}
}

class MockSession extends Http\Session
{
public $testSection;


public function __construct()
{
}


public function getSection(
string $section,
string $class = Nette\Http\SessionSection::class
): Nette\Http\SessionSection
{
return $this->testSection;
}
}

class MockSessionSection extends Nette\Http\SessionSection
{
public $testedKeyExistence;

public $storedKey;

public $storedValue;

public $testExpiration;

public $testExpirationVariables;


public function __construct()
{
}


public function __isset(string $name): bool
{
$this->testedKeyExistence = $name;
return false;
}


public function __set(string $name, $value): void
{
$this->storedKey = $name;
$this->storedValue = $value;
}


public function setExpiration($expiraton, $variables = null)
{
$this->testExpiration = $expiraton;
$this->testExpirationVariables = $variables;
}

test('ok', function () {
$testedKeyExistence = $storedKey = $storedValue = $testExpiration = $testExpirationVariables = null;

public function offsetExists($name): bool
{
return $this->__isset($name);
}
$sessionSectionMock = @Mockery::mock(Nette\Http\SessionSection::class);
$sessionSectionMock->shouldReceive('__isset')
->andReturnUsing(function ($name) use (&$testedKeyExistence) {
$testedKeyExistence = $name;
return false;
});

$sessionSectionMock->shouldReceive('__set')
->andReturnUsing(function ($name, $value) use (&$storedKey, &$storedValue) {
$storedKey = $name;
$storedValue = $value;
});

public function offsetSet($name, $value): void
{
$this->__set($name, $value);
}
$sessionSectionMock->shouldReceive('setExpiration')
->andReturnUsing(function ($expiration, $variables = null) use (&$testExpiration, &$testExpirationVariables, $sessionSectionMock) {
$testExpiration = $expiration;
$testExpirationVariables = $variables;
return $sessionSectionMock;
});

$sessionSectionMock->shouldReceive('offsetExists')
->andReturnUsing(function ($name) use (&$testedKeyExistence) {
$testedKeyExistence = $name;
return false;
});

public function offsetGet($name)
{
}
$sessionSectionMock->shouldReceive('offsetSet')
->andReturnUsing(function ($name, $value) use (&$storedKey, &$storedValue) {
$storedKey = $name;
$storedValue = $value;
});

//$sessionSectionMock->shouldReceive('offsetGet');
//$sessionSectionMock->shouldReceive('offsetUnset');

public function offsetUnset($name): void
{
}
}

class MockUser extends Security\User
{
public function __construct()
{
}
$sessionMock = Mockery::mock(Nette\Http\Session::class);
$sessionMock->shouldReceive('getSection')
->andReturn($sessionSectionMock);

$userMock = Mockery::mock(Nette\Security\User::class);
$userMock->shouldReceive('getId')
->andReturn('test_id');

public function getId()
{
return 'test_id';
}
}

test('', function () {
$presenter = new TestPresenter;
$presenter->injectPrimary(
null,
null,
new Application\Routers\SimpleRouter,
new Http\Request(new Http\UrlScript),
new Http\Response,
$session = new MockSession,
$user = new MockUser
$sessionMock,
$userMock
);

$section = $session->testSection = new MockSessionSection($session);

$applicationRequest = new Application\Request('', '', []);
$presenter->run($applicationRequest);

$expiration = '+1 year';
$key = $presenter->storeRequest($expiration);

Assert::same($expiration, $section->testExpiration);
Assert::same($key, $section->testExpirationVariables);
Assert::same($key, $section->testedKeyExistence);
Assert::same($key, $section->storedKey);
Assert::same([$user->getId(), $applicationRequest], $section->storedValue);
Assert::same($expiration, $testExpiration);
Assert::same($key, $testExpirationVariables);
Assert::same($key, $testedKeyExistence);
Assert::same($key, $storedKey);
Assert::same([$userMock->getId(), $applicationRequest], $storedValue);
});

test('', function () {

test('no user', function () {
$testedKeyExistence = $storedKey = $storedValue = $testExpiration = $testExpirationVariables = null;

$sessionSectionMock = Mockery::mock(Nette\Http\SessionSection::class);
$sessionSectionMock->shouldReceive('__isset')
->andReturnUsing(function ($name) use (&$testedKeyExistence) {
$testedKeyExistence = $name;
return false;
});

$sessionSectionMock->shouldReceive('__set')
->andReturnUsing(function ($name, $value) use (&$storedKey, &$storedValue) {
$storedKey = $name;
$storedValue = $value;
});

$sessionSectionMock->shouldReceive('setExpiration')
->andReturnUsing(function ($expiration, $variables = null) use (&$testExpiration, &$testExpirationVariables, $sessionSectionMock) {
$testExpiration = $expiration;
$testExpirationVariables = $variables;
return $sessionSectionMock;
});

$sessionSectionMock->shouldReceive('offsetExists')
->andReturnUsing(function ($name) use (&$testedKeyExistence) {
$testedKeyExistence = $name;
return false;
});

$sessionSectionMock->shouldReceive('offsetSet')
->andReturnUsing(function ($name, $value) use (&$storedKey, &$storedValue) {
$storedKey = $name;
$storedValue = $value;
});

//$sessionSectionMock->shouldReceive('offsetGet');
//$sessionSectionMock->shouldReceive('offsetUnset');

$sessionMock = Mockery::mock(Nette\Http\Session::class);
$sessionMock->shouldReceive('getSection')
->andReturn($sessionSectionMock);


$presenter = new TestPresenter;
$presenter->injectPrimary(
null,
null,
new Application\Routers\SimpleRouter,
new Http\Request(new Http\UrlScript),
new Http\Response,
$session = new MockSession
$sessionMock
);

$section = $session->testSection = new MockSessionSection($session);

$applicationRequest = new Application\Request('', '', []);
$presenter->run($applicationRequest);

$expiration = '+1 year';
$key = $presenter->storeRequest($expiration);

Assert::same($expiration, $section->testExpiration);
Assert::same($key, $section->testExpirationVariables);
Assert::same($key, $section->testedKeyExistence);
Assert::same($key, $section->storedKey);
Assert::same([null, $applicationRequest], $section->storedValue);
Assert::same($expiration, $testExpiration);
Assert::same($key, $testExpirationVariables);
Assert::same($key, $testedKeyExistence);
Assert::same($key, $storedKey);
Assert::same([null, $applicationRequest], $storedValue);
});
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// configure environment
Tester\Environment::setup();
date_default_timezone_set('Europe/Prague');
Mockery::setLoader(new Mockery\Loader\RequireLoader(getTempDir()));


// output buffer level check
Expand Down

0 comments on commit 780f9ba

Please sign in to comment.