diff --git a/composer.json b/composer.json index eb6c93e17..f1e927258 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/tests/UI/Component.redirect().phpt b/tests/UI/Component.redirect().phpt index cd0d2616d..46be12e32 100644 --- a/tests/UI/Component.redirect().phpt +++ b/tests/UI/Component.redirect().phpt @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); diff --git a/tests/UI/Presenter.storeRequest().phpt b/tests/UI/Presenter.storeRequest().phpt index c61482497..c3f5da637 100644 --- a/tests/UI/Presenter.storeRequest().phpt +++ b/tests/UI/Presenter.storeRequest().phpt @@ -8,7 +8,6 @@ declare(strict_types=1); use Nette\Application; use Nette\Http; -use Nette\Security; use Tester\Assert; @@ -23,100 +22,54 @@ 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, @@ -124,26 +77,67 @@ test('', function () { 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, @@ -151,20 +145,18 @@ test('', function () { 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); }); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 91c483705..4861eb612 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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