Skip to content

Commit

Permalink
Fix session auth check
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Apr 26, 2023
1 parent 1b18de7 commit 9bdb188
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/Session/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(RequestStack $requestStack)

public function isAuthenticated(): bool
{
return (bool) $this->getHttpSession()->get(static::SESSION_LASTFM_TOKEN);
return $this->getHttpSession()->has(static::SESSION_LASTFM_TOKEN);
}

public function getUsername(): ?string
Expand Down
63 changes: 14 additions & 49 deletions tests/Session/SessionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@

use Nucleos\LastFm\Session\Session as LastFmSession;
use Nucleos\LastFmBundle\Session\SessionManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;

final class SessionManagerTest extends TestCase
{
/**
* @var MockObject&Session
*/
private Session $session;

private RequestStack $requestStack;

protected function setUp(): void
{
$this->session = $this->createMock(Session::class);
$this->session = new Session(new MockArraySessionStorage());

$request = new Request();
$request->setSession($this->session);
Expand All @@ -41,41 +38,28 @@ protected function setUp(): void

public function testIsAuthenticated(): void
{
$this->session->method('get')->with('LASTFM_TOKEN')
->willReturn(true)
;
$this->session->set('LASTFM_TOKEN', 'TheToken');

$manager = new SessionManager($this->requestStack);
static::assertTrue($manager->isAuthenticated());
}

public function testIsNotAuthenticated(): void
{
$this->session->method('get')->with('LASTFM_TOKEN')
->willReturn(false)
;

$manager = new SessionManager($this->requestStack);
static::assertFalse($manager->isAuthenticated());
}

public function testGetUsername(): void
{
$this->session->method('get')->with('LASTFM_NAME')
->willReturn('MyUser')
;
$this->session->set('LASTFM_NAME', 'MyUser');

$manager = new SessionManager($this->requestStack);
static::assertSame('MyUser', $manager->getUsername());
}

public function testGetUsernameNotExist(): void
{
$this->session = $this->createMock(Session::class);
$this->session->method('get')->with('LASTFM_NAME')
->willReturn(null)
;

$manager = new SessionManager($this->requestStack);
static::assertNull($manager->getUsername());
}
Expand All @@ -84,44 +68,29 @@ public function testStore(): void
{
$lastfmSession = new LastFmSession('YourName', 'YourToken');

$this->session->expects(static::exactly(2))->method('set')
->withConsecutive(
['LASTFM_NAME', 'YourName'],
['LASTFM_TOKEN', 'YourToken'],
)
;

$manager = new SessionManager($this->requestStack);
$manager->store($lastfmSession);

static::assertTrue($this->session->has('LASTFM_TOKEN'));
static::assertTrue($this->session->has('LASTFM_NAME'));
}

public function testClear(): void
{
$this->session->expects(static::exactly(2))->method('remove')
->withConsecutive(
['LASTFM_NAME'],
['LASTFM_TOKEN'],
)
;
$this->session->set('LASTFM_TOKEN', 'TheToken');
$this->session->set('LASTFM_NAME', 'MyUser');

$manager = new SessionManager($this->requestStack);
$manager->clear();

static::assertFalse($this->session->has('LASTFM_TOKEN'));
static::assertFalse($this->session->has('LASTFM_NAME'));
}

public function testGetSession(): void
{
$this->session->expects(static::exactly(3))->method('get')
->withConsecutive(
['LASTFM_TOKEN'],
['LASTFM_NAME'],
['LASTFM_TOKEN'],
)
->willReturn(
'TheToken',
'MyUser',
'TheToken'
)
;
$this->session->set('LASTFM_TOKEN', 'TheToken');
$this->session->set('LASTFM_NAME', 'MyUser');

$manager = new SessionManager($this->requestStack);

Expand All @@ -134,10 +103,6 @@ public function testGetSession(): void

public function testGetSessionWithNoAuth(): void
{
$this->session->method('get')->with('LASTFM_TOKEN')
->willReturn(null)
;

$manager = new SessionManager($this->requestStack);

static::assertNull($manager->getSession());
Expand Down

0 comments on commit 9bdb188

Please sign in to comment.