diff --git a/src/Annotation/Cookie.php b/src/Annotation/Cookie.php index 1565f7a..e4254c2 100644 --- a/src/Annotation/Cookie.php +++ b/src/Annotation/Cookie.php @@ -7,7 +7,8 @@ use Attribute; use Ray\Di\Di\Qualifier; -#[Attribute, Qualifier] +#[Attribute] +#[Qualifier] final class Cookie { } diff --git a/src/Annotation/DeleteCookie.php b/src/Annotation/DeleteCookie.php index 4d411a9..2e69ef6 100644 --- a/src/Annotation/DeleteCookie.php +++ b/src/Annotation/DeleteCookie.php @@ -7,7 +7,8 @@ use Attribute; use Ray\Di\Di\Qualifier; -#[Attribute, Qualifier] +#[Attribute] +#[Qualifier] final class DeleteCookie { } diff --git a/src/AuraSessionInject.php b/src/AuraSessionInject.php index d64c78a..b3e6687 100644 --- a/src/AuraSessionInject.php +++ b/src/AuraSessionInject.php @@ -6,15 +6,13 @@ use Aura\Session\Session; -/** - * @deprecated Use PHP 8.0: Class constructor property promotion instead - */ +/** @deprecated Use PHP 8.0: Class constructor property promotion instead */ trait AuraSessionInject { /** @var Session */ protected $session; - public function setSession(Session $session) + public function setSession(Session $session): void { $this->session = $session; } diff --git a/src/AuraSessionModule.php b/src/AuraSessionModule.php index bd40b4d..06bc2f3 100644 --- a/src/AuraSessionModule.php +++ b/src/AuraSessionModule.php @@ -22,7 +22,7 @@ class AuraSessionModule extends AbstractModule { /** - * {@inheritdoc} + * {@inheritDoc} */ protected function configure() { diff --git a/src/CookieProvider.php b/src/CookieProvider.php index d7c6fe9..d0e1320 100644 --- a/src/CookieProvider.php +++ b/src/CookieProvider.php @@ -13,9 +13,10 @@ class CookieProvider implements ProviderInterface { /** - * {@inheritdoc} + * {@inheritDoc} * * @SuppressWarnings(PHPMD.Superglobals) + * @psalm-taint-source input */ public function get() { diff --git a/src/DeleteCookieInvoker.php b/src/DeleteCookieInvoker.php index 2bf2658..1d121fe 100644 --- a/src/DeleteCookieInvoker.php +++ b/src/DeleteCookieInvoker.php @@ -8,11 +8,10 @@ namespace Ray\AuraSessionModule; -use function setcookie; -use function time; - final class DeleteCookieInvoker { + public const EXPIRE_OFFSET = 42000; + /** * Delete a cookie by setting its expiration time to a past value * @@ -23,9 +22,9 @@ public function __invoke(string $name, array $params): void setcookie( $name, '', - time() - 42000, + time() - self::EXPIRE_OFFSET, $params['path'], - $params['domain'] + $params['domain'], ); } } diff --git a/src/SessionProvider.php b/src/SessionProvider.php index 9997031..37c195f 100644 --- a/src/SessionProvider.php +++ b/src/SessionProvider.php @@ -1,26 +1,27 @@ newInstance($_COOKIE); + return (new SessionFactory())->newInstance($_COOKIE); } } diff --git a/tests/AuraSessionInjectTest.php b/tests/AuraSessionInjectTest.php new file mode 100644 index 0000000..af39244 --- /dev/null +++ b/tests/AuraSessionInjectTest.php @@ -0,0 +1,30 @@ +session; + } +} + +class AuraSessionInjectTest extends TestCase +{ + public function testSetSession(): void + { + $session = $this->createMock(Session::class); + $consumer = new FakeSessionConsumer(); + $consumer->setSession($session); + + $this->assertSame($session, $consumer->getSession()); + } +} diff --git a/tests/DeleteCookieInvokerTest.php b/tests/DeleteCookieInvokerTest.php new file mode 100644 index 0000000..b475ec6 --- /dev/null +++ b/tests/DeleteCookieInvokerTest.php @@ -0,0 +1,63 @@ + */ +$setCookieCalls = []; + +/** + * Override time function for testing + */ +function time(): int +{ + return 1000000; +} + +/** + * Override setcookie function for testing + * + * @return bool + */ +function setcookie(string $name, string $value = '', int $expires = 0, string $path = '', string $domain = '') +{ + global $setCookieCalls; + + $setCookieCalls[] = [ + 'name' => $name, + 'value' => $value, + 'expires' => $expires, + 'path' => $path, + 'domain' => $domain, + ]; + + return true; +} + +class DeleteCookieInvokerTest extends TestCase +{ + protected function setUp(): void + { + global $setCookieCalls; + + $setCookieCalls = []; + } + + public function testInvoke(): void + { + global $setCookieCalls; + + $invoker = new DeleteCookieInvoker(); + $invoker('test_cookie', ['path' => '/app', 'domain' => 'example.com']); + + $this->assertCount(1, $setCookieCalls); + $this->assertSame('test_cookie', $setCookieCalls[0]['name']); + $this->assertSame('', $setCookieCalls[0]['value']); + $this->assertSame(1000000 - DeleteCookieInvoker::EXPIRE_OFFSET, $setCookieCalls[0]['expires']); + $this->assertSame('/app', $setCookieCalls[0]['path']); + $this->assertSame('example.com', $setCookieCalls[0]['domain']); + } +} diff --git a/tests/SessionProviderTest.php b/tests/SessionProviderTest.php new file mode 100644 index 0000000..ed50373 --- /dev/null +++ b/tests/SessionProviderTest.php @@ -0,0 +1,18 @@ +get(); + $this->assertInstanceOf(Session::class, $session); + } +}